package com.groupon.seleniumgridextras.utilities;

import org.junit.Test;

import java.net.ConnectException;
import java.net.URL;
import java.net.UnknownHostException;

import static org.junit.Assert.assertEquals;

/**
 * Created with IntelliJ IDEA. User: dima Date: 7/8/14 Time: 4:09 PM To change this template use
 * File | Settings | File Templates.
 */
public class HttpUtilityTest {

  @Test(expected=ConnectException.class)
  public void testConnectionRefusedError() throws Exception {
    HttpUtility.getRequest(new URL("http://localhost:9999")).getResponseCode();
  }

  @Test
  public void test404Page() throws Exception {
    assertEquals(404, HttpUtility.getRequest(new URL("http://xkcd.com/404")).getResponseCode());
  }

  @Test
  public void test200Page() throws Exception {
    assertEquals(200, HttpUtility.getRequest(new URL("http://google.com")).getResponseCode());
  }

  @Test(expected = UnknownHostException.class)
  public void testUnknownHost() throws Exception {
    HttpUtility.getRequest(new URL("http://googasdfasfdkjashfdkjahsfdle.com/")).getResponseCode();
  }

  @Test
  public void testGetAsString() throws Exception{
    assertEquals("", HttpUtility.getRequestAsString(new URL("http://xkcd.com/404")));
  }
}

@@ -102,16 +102,46 @@
 eption;%0A
+import java.net.ServerSocket;%0A
 import j
@@ -242,150 +242,8 @@
 s;%0A%0A
-/**%0A * Created with IntelliJ IDEA. User: dima Date: 7/8/14 Time: 4:09 PM To change this template use%0A * File %7C Settings %7C File Templates.%0A */%0A
 publ
@@ -286,17 +286,19 @@
 expected
-=
+ = 
 ConnectE
@@ -367,32 +367,275 @@
 ows Exception %7B%0A
+    ServerSocket serverSocket = new ServerSocket(0);%0A    int port = serverSocket.getLocalPort();%0A    serverSocket%0A        .close(); //Find a garanteed open port by taking one and closing. Why doesn't Java allow me to get a list of open ports?%0A
     HttpUtility.
@@ -675,13 +675,16 @@
 ost:
-9999%22
+%22 + port
 )).g
@@ -1276,16 +1276,17 @@
 xception
+ 
 %7B%0A    as

Fix an issue where the port 9999 might be occupied thus the test fails. Now the test finds a garanteed emtpy port.