<commit_before>package org.littleshoot.proxy;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Tests the default HTTP proxy.
 */
public class HttpProxyTest {

    private final Logger log = LoggerFactory.getLogger(getClass());
    
    private static final String HOST = "http://opsgenie.com/status/ping";
    
    /**
     * Tests the proxy both with chunking and without to make sure it's working
     * identically with both.
     *
     * @throws Exception If any unexpected error occurs.
     */
    public void testProxyChunkAndNo() throws Exception {
        final HttpProxyServer server = startHttpProxy();
        try {
            final byte[] baseResponse = rawResponse("i.i.com.com", 80, true, HttpVersion.HTTP_1_0);
            final byte[] proxyResponse = rawResponse("127.0.0.1", 8080, false, HttpVersion.HTTP_1_1);
            final ChannelBuffer wrappedBase = ChannelBuffers.wrappedBuffer(baseResponse);
            final ChannelBuffer wrappedProxy = ChannelBuffers.wrappedBuffer(proxyResponse);
    
            assertEquals("Lengths not equal", wrappedBase.capacity(), wrappedProxy.capacity());
            assertEquals("Not equal:\n"+
                ChannelBuffers.hexDump(wrappedBase)+"\n\n\n"+
                ChannelBuffers.hexDump(wrappedProxy), wrappedBase, wrappedProxy);
    
            final ByteArrayInputStream baseBais = new ByteArrayInputStream(baseResponse);
            //final String baseStr = IOUtils.toString(new GZIPInputStream(baseBais));
            final String baseStr = IOUtils.toString(baseBais);
            final File baseFile = new File("base_sandberg.jpg");
            baseFile.deleteOnExit();
            final FileWriter baseFileWriter = new FileWriter(baseFile);
            baseFileWriter.write(baseStr);
            baseFileWriter.close();
            //System.out.println("RESPONSE:\n"+baseStr);
    
            final ByteArrayInputStream proxyBais = new ByteArrayInputStream(proxyResponse);
            //final String proxyStr = IOUtils.toString(new GZIPInputStream(proxyBais));
            final String proxyStr = IOUtils.toString(proxyBais);
            final File proxyFile = new File("proxy_sandberg.jpg");
            proxyFile.deleteOnExit();
            final FileWriter proxyFileWriter = new FileWriter(proxyFile);
            proxyFileWriter.write(proxyStr);
            proxyFileWriter.close();
            //System.out.println("RESPONSE:\n"+proxyStr);
            
            assertEquals("Decoded proxy string does not equal expected", baseStr, proxyStr);
        } finally {
            server.stop();
        }
    }

    @Test
    public void testProxyWithApacheHttpClientChunkedRequests() throws Exception {
        final HttpProxyServer server = startHttpProxy();

        try {
            String baseResponse = httpPostWithApacheClient(false);
            String proxyResponse = httpPostWithApacheClient(true);
            assertEquals(baseResponse, proxyResponse);
        } finally {
            server.stop();
        }
    }
    
    @Test
    public void testProxyWithApacheHttpClientChunkedRequestsBadAddress() throws Exception {
        final HttpProxyServer server = startHttpProxy();
        
        try {
            final String response =
                httpPostWithApacheClient(true, "http://test.localhost");
            assertTrue(response.startsWith("Bad Gateway"));
        } finally {
            server.stop();
        }
    }

    @Test
    public void testProxyAuthorizationWithPostRequests() throws Exception{
        String username = "user1";
        String password = "<PASSWORD>";

        final HttpProxyServer server = 
                startHttpProxyWithCredentials(username, password);

        try {
            String baseResponse = httpPostWithApacheClient(false);
            String proxyResponse = 
                httpPostWithApacheClient(true, username, password);
            assertEquals(baseResponse, proxyResponse);<commit_msg>Fixed test for squid proxies<commit_after>package org.littleshoot.proxy;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Tests the default HTTP proxy.
 */
public class HttpProxyTest {

    private final Logger log = LoggerFactory.getLogger(getClass());
    
    private static final String HOST = "http://opsgenie.com/status/ping";
    
    /**
     * Tests the proxy both with chunking and without to make sure it's working
     * identically with both.
     *
     * @throws Exception If any unexpected error occurs.
     */
    public void testProxyChunkAndNo() throws Exception {
        final HttpProxyServer server = startHttpProxy();
        try {
            final byte[] baseResponse = rawResponse("i.i.com.com", 80, true, HttpVersion.HTTP_1_0);
            final byte[] proxyResponse = rawResponse("127.0.0.1", 8080, false, HttpVersion.HTTP_1_1);
            final ChannelBuffer wrappedBase = ChannelBuffers.wrappedBuffer(baseResponse);
            final ChannelBuffer wrappedProxy = ChannelBuffers.wrappedBuffer(proxyResponse);
    
            assertEquals("Lengths not equal", wrappedBase.capacity(), wrappedProxy.capacity());
            assertEquals("Not equal:\n"+
                ChannelBuffers.hexDump(wrappedBase)+"\n\n\n"+
                ChannelBuffers.hexDump(wrappedProxy), wrappedBase, wrappedProxy);
    
            final ByteArrayInputStream baseBais = new ByteArrayInputStream(baseResponse);
            //final String baseStr = IOUtils.toString(new GZIPInputStream(baseBais));
            final String baseStr = IOUtils.toString(baseBais);
            final File baseFile = new File("base_sandberg.jpg");
            baseFile.deleteOnExit();
            final FileWriter baseFileWriter = new FileWriter(baseFile);
            baseFileWriter.write(baseStr);
            baseFileWriter.close();
            //System.out.println("RESPONSE:\n"+baseStr);
    
            final ByteArrayInputStream proxyBais = new ByteArrayInputStream(proxyResponse);
            //final String proxyStr = IOUtils.toString(new GZIPInputStream(proxyBais));
            final String proxyStr = IOUtils.toString(proxyBais);
            final File proxyFile = new File("proxy_sandberg.jpg");
            proxyFile.deleteOnExit();
            final FileWriter proxyFileWriter = new FileWriter(proxyFile);
            proxyFileWriter.write(proxyStr);
            proxyFileWriter.close();
            //System.out.println("RESPONSE:\n"+proxyStr);
            
            assertEquals("Decoded proxy string does not equal expected", baseStr, proxyStr);
        } finally {
            server.stop();
        }
    }

    @Test
    public void testProxyWithApacheHttpClientChunkedRequests() throws Exception {
        final HttpProxyServer server = startHttpProxy();

        try {
            String baseResponse = httpPostWithApacheClient(false);
            String proxyResponse = httpPostWithApacheClient(true);
            assertEquals(baseResponse, proxyResponse);
        } finally {
            server.stop();
        }
    }
    
    @Test
    public void testProxyWithApacheHttpClientChunkedRequestsBadAddress() throws Exception {
        final HttpProxyServer server = startHttpProxy();
        
        try {
            final String response =
                httpPostWithApacheClient(true, "http://test.localhost");
            
            // The second expected response is what squid returns here.
            assertTrue("Received: "+response, response.startsWith("Bad Gateway") ||
                    response.contains("The requested URL could not be retrieved"));
        } finally {
            server.stop();
        }
    }

    @Test
    public void testProxyAuthorizationWithPostRequests() throws Exception{
        String username = "user1";
        String password = "<PASSWORD>";

        final HttpProxyServer server = 
                startHttpProxyWithCredentials(username, password);

        try {
            String baseResponse = httpPostWithApacheClient(false);
            String proxyResponse = 
                httpPostWithApacheClient(true, username, password);
            assertEquals(baseResponse, proxyResponse);
{'fraction_non_alphanumeric': 0.09431930359867181, 'fraction_numerical': 0.0035896975679798977, 'mean_word_length': 3.03768115942029, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '3310862', 'n_tokens_mistral': 2956, 'n_tokens_neox': 2940, 'n_words': 645}