package com.wizzardo.http;

import com.wizzardo.tools.http.HttpClient;
import com.wizzardo.tools.io.IOTools;
import org.junit.After;
import org.junit.Before;

import java.io.IOException;

/**
 * @author: moxa
 * Date: 5/7/13
 */
public class ServerTest {

    protected HttpServer server;
    protected int workers = 4;
    protected int port = 9999;
    protected volatile Handler handler;

    @Before
    public void setUp() throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
        server = new HttpServer(null, port, workers);
        server.setHandler((request, response) -> handler.handle(request, response));
        server.setIoThreadsCount(1);
        server.start();
    }

    @After
    public void tearDown() throws InterruptedException {
        server.stopEpoll();
        handler = null;
    }

    protected com.wizzardo.tools.http.Request makeRequest(String path) {
        return HttpClient.createRequest("http://localhost:" + port + path)
                .header("Connection", "Close");
    }

    public int getPort() {
        return port;
    }

    protected String curl(String path, String... params) {
        Runtime runtime = Runtime.getRuntime();

        String[] args = new String[params.length + 2];
        args[0] = "curl";
        System.arraycopy(params, 0, args, 1, params.length);
        args[args.length - 1] = "http://localhost:" + port + path;

        try {
            Process p = runtime.exec(args);
            return new String(IOTools.bytes(p.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@@ -21,16 +21,57 @@
 .http;%0A%0A
+import com.wizzardo.http.request.Header;%0A
 import c
@@ -191,16 +191,72 @@
 .Before;
+%0Aimport org.junit.Rule;%0Aimport org.junit.rules.TestName;
 %0A%0Aimport
@@ -482,16 +482,70 @@
 ndler;%0A%0A
+    @Rule%0A    public TestName name = new TestName();%0A%0A
     @Bef
@@ -641,32 +641,93 @@
 ieldException %7B%0A
+        System.out.println(%22setUp %22 + name.getMethodName());%0A
         server =
@@ -812,16 +812,123 @@
 onse) -%3E
+ %7B%0A            response.setHeader(Header.KEY_CONNECTION, Header.VALUE_CONNECTION_CLOSE);%0A            return
  handler
@@ -953,16 +953,27 @@
 esponse)
+;%0A        %7D
 );%0A     
@@ -1095,32 +1095,96 @@
 ptedException %7B%0A
+        System.out.println(%22tearDown %22 + name.getMethodName());%0A
         server.s
@@ -1374,16 +1374,76 @@
 + path)%0A
+                .header(%22testMethod%22, name.getMethodName())%0A
         

improve tests information