<commit_before>package org.lantern; 

import java.util.Collection;
import java.util.Set;
import static org.junit.Assert.*;
import org.junit.Test;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.http.CookieEncoder;
import org.jboss.netty.handler.codec.http.Cookie;
import org.jboss.netty.handler.codec.http.HttpHeaders; 
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.lantern.cookie.SetCookieObserver;
import org.lantern.cookie.SetCookieObserverHandler;
import static org.lantern.TestingUtils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SetCookieObserverHandlerTest {
    
    @Test
    public void testBasicNotification() {
        
        
        final String requestUri = "http://www.example.org/foo";
        
        class ObserverTest implements SetCookieObserver {
            
            public boolean success = false; 
            
            @Override
            public void setCookies(Collection<Cookie> cookies, HttpRequest context) {
                assertFalse(success);
                assertTrue(cookies.size() == 4);
                assertTrue(hasCookieNamed("good", cookies));
                assertTrue(hasCookieNamed("bad", cookies));
                assertTrue(hasCookieNamed("ugly", cookies));
                assertTrue(hasCookieNamed("toadface", cookies));
                assertFalse(hasCookieNamed("normalface", cookies));                
                assertTrue(context.getUri().equals(requestUri));
                success = true; 
            }
        }
        
        final ObserverTest observer = new ObserverTest(); 
        final ChannelHandler handler = new SetCookieObserverHandler(observer);

        final ChannelPipeline pipeline = Channels.pipeline();
        final Channel chan = createDummyChannel(pipeline); 
        pipeline.addLast("set_cookie_observer", handler);
        
        // we have to send an httprequest down the pipeline first
        // to correlate with our response...
        final HttpRequest req = createGetRequest(requestUri);

        // create an HttpResponse with several Set-Cookies in it...
        final HttpResponse res = createResponse();
        final CookieEncoder enc = new CookieEncoder(true);
        enc.addCookie("bad", "0");
        enc.addCookie("good", "1");
        enc.addCookie("ugly", "2");
        enc.addCookie("toadface", "3");
        res.setHeader(HttpHeaders.Names.SET_COOKIE, enc.encode());
        
        // make sure everything is there when we start...
        final Set<Cookie> startCookies = extractSetCookies(res);
        assertTrue(startCookies.size() == 4);
        assertTrue(hasCookieNamed("good", startCookies));
        assertTrue(hasCookieNamed("bad", startCookies));
        assertTrue(hasCookieNamed("ugly", startCookies));
        assertTrue(hasCookieNamed("toadface", startCookies));
        assertFalse(hasCookieNamed("normalface", startCookies));

        assertFalse(observer.success);

        // send the fake request...
        pipeline.sendUpstream(createDummyMessageEvent(req));
        
        // XXX should we wait for completion? 
        // then the fake reply...
        pipeline.sendDownstream(createDummyMessageEvent(res));

        // this should trigger the observer with the <commit_msg>Fixed test to work with newew netty<commit_after>package org.lantern; 

import java.util.Collection;
import java.util.Set;
import static org.junit.Assert.*;
import org.junit.Test;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.codec.http.CookieEncoder;
import org.jboss.netty.handler.codec.http.Cookie;
import org.jboss.netty.handler.codec.http.HttpHeaders; 
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.lantern.cookie.SetCookieObserver;
import org.lantern.cookie.SetCookieObserverHandler;
import static org.lantern.TestingUtils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SetCookieObserverHandlerTest {
    
    @Test
    public void testBasicNotification() {
        
        
        final String requestUri = "http://www.example.org/foo";
        
        class ObserverTest implements SetCookieObserver {
            
            public boolean success = false; 
            
            @Override
            public void setCookies(Collection<Cookie> cookies, HttpRequest context) {
                assertFalse(success);
                assertTrue(cookies.size() == 4);
                assertTrue(hasCookieNamed("good", cookies));
                assertTrue(hasCookieNamed("bad", cookies));
                assertTrue(hasCookieNamed("ugly", cookies));
                assertTrue(hasCookieNamed("toadface", cookies));
                assertFalse(hasCookieNamed("normalface", cookies));                
                assertTrue(context.getUri().equals(requestUri));
                success = true; 
            }
        }
        
        final ObserverTest observer = new ObserverTest(); 
        final ChannelHandler handler = new SetCookieObserverHandler(observer);

        final ChannelPipeline pipeline = Channels.pipeline();
        final Channel chan = createDummyChannel(pipeline); 
        pipeline.addLast("set_cookie_observer", handler);
        
        // we have to send an httprequest down the pipeline first
        // to correlate with our response...
        final HttpRequest req = createGetRequest(requestUri);

        // create an HttpResponse with several Set-Cookies in it...
        final HttpResponse res = createResponse();
        CookieEncoder enc = new CookieEncoder(true);
        enc.addCookie("bad", "0");
        String cook = enc.encode();
        res.addHeader(HttpHeaders.Names.SET_COOKIE, cook);
        enc = new CookieEncoder(true);
        enc.addCookie("good", "1");
        cook = enc.encode();
        res.addHeader(HttpHeaders.Names.SET_COOKIE, cook);
        enc = new CookieEncoder(true);
        enc.addCookie("ugly", "2");
        cook = enc.encode();
        res.addHeader(HttpHeaders.Names.SET_COOKIE, cook);
        enc = new CookieEncoder(true);
        enc.addCookie("toadface", "3");
        cook = enc.encode();
        res.addHeader(HttpHeaders.Names.SET_COOKIE, cook);
        //res.setHeader(HttpHeaders.Names.SET_COOKIE, enc.encode());
        
        // make sure everything is there when we start...
        final Set<Cookie> startCookies = extractSetCookies(res);
        assertTrue(startCookies.size() == 4);
        assertTrue(hasCookieNamed("good", startCookies));
        assertTrue(hasCookieNamed("bad", startCookies));
        assertTrue(hasCookieNamed("ugly", startCookies));
        assertTrue(hasCookieNamed("toadface", startCookies));
        assertFalse(hasCookieNamed("normalface", startCookies));

        assertFalse(observer.success);

        // send the fake request...
        pipeline.sendUpstream(createDummyMessageEvent(req));
        
        // XXX should we wait for completion? 
        // then the fake reply...
        pipeline.sendDownstream(createDummyMessageEvent(res));

        // this should trigger the observer with the 
{'fraction_non_alphanumeric': 0.10618996798292422, 'fraction_numerical': 0.0021344717182497333, 'mean_word_length': 2.691285081240768, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2401937', 'n_tokens_mistral': 2032, 'n_tokens_neox': 1957, 'n_words': 423}