package com.wizzardo.servlet;

import com.wizzardo.http.HttpServer;
import com.wizzardo.tools.http.HttpClient;
import com.wizzardo.tools.http.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.StdErrLog;
import org.junit.After;
import org.junit.Before;

import java.util.Properties;

/**
 * @author: wizzardo
 * Date: 23.10.14
 */
public class ServerTest {
    protected CustomServlet servlet;
    private Server jetty;
    private HttpServer myServer;
    private int jettyPort = 8080;
    private int myPort = 8081;

    @Before
    public void setUp() throws Exception {
        Properties loggingProperties = new Properties();
        loggingProperties.setProperty("org.eclipse.jetty.LEVEL", "WARN");
        StdErrLog.setProperties(loggingProperties);

        jetty = new Server(jettyPort);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/servlet");
        jetty.setHandler(context);

        servlet = new CustomServlet();
        context.addServlet(new ServletHolder(servlet), "/*");
        jetty.start();


        myServer = new HttpServer(myPort);
        myServer.setIoThreadsCount(1);
        myServer.setHandler(new ServletHandler(servlet));

        myServer.start();
    }

    @After
    public void tearDown() throws Exception {
        jetty.stop();
        servlet.clean();
    }

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

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

    public void printHeaders(Response response) {
        response.headers().forEach((s, strings) -> {
            System.out.println(s + ": " + strings);
        });
    }
}

@@ -379,16 +379,69 @@
 nit.
-Before;%0A
+Assert;%0Aimport org.junit.Before;%0A%0Aimport java.io.IOException;
 %0Aimp
@@ -540,16 +540,71 @@
 rTest %7B%0A
+    protected final String DEFAULT_PATH = %22/servlet/%22;%0A
     prot
@@ -1170,18 +1170,20 @@
 ath(
-%22/servlet%22
+DEFAULT_PATH
 );%0A 
@@ -1620,16 +1620,47 @@
 clean();
+%0A%0A        myServer.stopEpoll();
 %0A    %7D%0A%0A
@@ -1710,32 +1710,252 @@
 st jettyRequest(
+) %7B%0A        return jettyRequest(DEFAULT_PATH);%0A    %7D%0A%0A    protected com.wizzardo.tools.http.Request myRequest() %7B%0A        return myRequest(DEFAULT_PATH);%0A    %7D%0A%0A    protected com.wizzardo.tools.http.Request jettyRequest(
 String path) %7B%0A 
@@ -1964,35 +1964,24 @@
      return 
-HttpClient.
 createReques
@@ -2025,54 +2025,152 @@
 ath)
+;
 %0A    
+%7D%0A%0A
     
-        .header(%22Connection%22, %22Close%22
+protected com.wizzardo.tools.http.Request myRequest(String path) %7B%0A        return createRequest(%22http://localhost:%22 + myPort + path
 );%0A 
@@ -2213,34 +2213,38 @@
 ls.http.Request 
-my
+create
 Request(String p
@@ -2234,36 +2234,35 @@
 eRequest(String 
-path
+url
 ) %7B%0A        retu
@@ -2293,43 +2293,47 @@
 est(
-%22http://localhost:%22 + myPort + path
+url)%0A                .disableRedirects(
 )%0A  
@@ -2557,11 +2557,313 @@
 );%0A    %7D
+%0A%0A    protected void test(TestStrategy strategy) throws IOException %7B%0A        Assert.assertEquals(strategy.exec(jettyRequest()), strategy.exec(myRequest()));%0A    %7D%0A%0A    protected static interface TestStrategy%3CT%3E %7B%0A        public T exec(com.wizzardo.tools.http.Request request) throws IOException;%0A    %7D
 %0A%7D%0A

improve environment