package com.bugsnag.http;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.bugsnag.Configuration;

import static org.junit.Assert.assertEquals;

public class HttpClientTest {

  Configuration configuration = new Configuration();
  HttpClient httpClient = new HttpClient(configuration);

  ServerSocket serverSocket;

  @Before
  public void setUp() throws IOException {
    // create server with random port and room for single connection
    serverSocket = new ServerSocket(0, 1);
  }

  @After
  public void tearDown() throws Exception {
    serverSocket.close();
  }

  @Ignore("behavior simulating read timeout isn't consistent across platforms")
  @Test
  public void testConnectionTimeout() throws Exception {
    configuration.setConnectionTimeout(10);

    // reserve the only available connection
    new Socket().connect(serverSocket.getLocalSocketAddress());

    try {
      httpClient.post("http://localhost:" + serverSocket.getLocalPort(), new ByteArrayInputStream("foo".getBytes()));
    } catch (NetworkException e) {
      assertEquals("connect timed out", e.getCause().getMessage());
    }
  }

  @Test
  public void testReadTimeout() throws Exception {
    configuration.setReadTimeout(10);

    try {
      httpClient.post("http://localhost:" + serverSocket.getLocalPort(), new ByteArrayInputStream("foo".getBytes()));
    } catch (NetworkException e) {
      assertEquals("Read timed out", e.getCause().getMessage());
    }
  }

  @Test
  public void testProxyConnection() throws Exception {
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("notSoLocalHost", 8080));
    configuration.setProxy(proxy);

    try{
      httpClient.post("http://localhost:" + serverSocket.getLocalPort(), new ByteArrayInputStream("foo".getBytes()));
    } catch (NetworkException e){
      assertEquals("notSoLocalHost", e.getCause().getMessage());
    }
  }
}

@@ -282,16 +282,53 @@
 tEquals;
+%0Aimport static org.junit.Assert.fail;
 %0A%0Apublic
@@ -1937,32 +1937,101 @@
 %22.getBytes()));%0A
+      fail(%22Should throw Exception because proxy host is unknown.%22);%0A
     %7D catch (Net

make test fail if it doesn't cast exception