package com.ian;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;

public class ServSocketTest {

    // Integration tests that require manual work:
    // Run the following command in the terminal, then un-ignore tests here.
    // $ java -jar ./target/http-server-1.0-SNAPSHOT.jar
    @Test
    @Ignore
    public void testClientSocketGetsHTTP200ResponseWhenRootIsRequested() {
        String expected;
        try { expected = ClientSocketHelper.request("GET / HTTP/1.1");
        } catch ( IOException e ) { expected = e.getMessage(); }
        assertEquals("HTTP/1.1 200 OK", expected);
    }

    @Test
    @Ignore
    public void testClientSocketGetsHTTP200ResponseWhenAnythingOtherThanRootIsRequested() {
        String expected;
        try { expected = ClientSocketHelper.request("GET /a-day-not-night-to-see-till-i-see-thee HTTP/1.1");
        } catch ( IOException e ) { expected = e.getMessage(); }
        assertEquals("HTTP/1.1 404 Not Found", expected);
    }
}
package com.ian;

import static org.junit.Assert.assertEquals;

import org.junit.*;

import java.io.IOException;

public class ServSocketTest {

    @BeforeClass
    public static void startUp() {
        class SeparateThread implements Runnable {
            public void run() {
                ServSocket.serve(5000);
            }
        }
        new Thread(new SeparateThread()).start();
    }

    @Test
    public void testClientSocketGetsHTTP200ResponseWhenRootIsRequested() {
        try { Thread.sleep(1); } catch (InterruptedException e){ e.getMessage(); }
        String expected;
        try { expected = ClientSocketHelper.request("GET / HTTP/1.1");
        } catch ( IOException e ) { expected = e.getMessage(); }
        assertEquals("HTTP/1.1 200 OK", expected);
    }

    @Test
    public void testClientSocketGetsHTTP404ResponseWhenAnythingOtherThanRootIsRequested() {
        try { Thread.sleep(1); } catch (InterruptedException e){ e.getMessage(); }
        String expected;
        try { expected = ClientSocketHelper.request("GET /a-day-not-night-to-see-till-i-see-thee HTTP/1.1");
        } catch ( IOException e ) { expected = e.getMessage(); }
        assertEquals("HTTP/1.1 404 Not Found", expected);
    }
}
automate tests w/ multiple threads & Thread.sleep