package com.atexpose;

import com.atexpose.dispatcher.Dispatcher;
import io.schinzel.basicutils.Sandman;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

public class WebServerRedirectTest {
    Dispatcher mWebServer;


    @Before
    public void before() {
        mWebServer = AtExpose.create().getWebServerBuilder()
                .numberOfThreads(5)
                .addFileRedirect("src.html", "dest.html")
                .addFileRedirect("dir1/dir2/src.html", "dirdest/dest.html")
                .startWebServer();
    }


    @After
    public void after() {
        mWebServer.shutdown();
        //Snooze required to get tests to work on Travis
        Sandman.snoozeMillis(10);
    }


    @Test
    public void testRedirect_pageInRoot_shouldRedirect() throws Exception {
        //Basic test
        Connection.Response response = Jsoup
                .connect("http://127.0.0.1:5555/src.html")
                .method(Connection.Method.GET)
                .followRedirects(false)
                .execute();
        assertEquals(302, response.statusCode());
        assertEquals("http://127.0.0.1:5555/dest.html", response.header("Location"));
    }


    @Test
    public void testRedirect_pageInSubDir_shouldRedirect() throws Exception {
        //Basic test
        Connection.Response response = Jsoup
                .connect("http://127.0.0.1:5555/dir1/dir2/src.html")
                .method(Connection.Method.GET)
                .followRedirects(false)
                .execute();
        assertEquals(302, response.statusCode());
        assertEquals("http://127.0.0.1:5555/dirdest/dest.html", response.header("Location"));
    }


    @Test
    public void testRedirect_pageInRoot_withQueryString_shouldRedirectWithQuery() throws Exception {
        //Basic test
        Connection.Response response = Jsoup
                .connect("http://127.0.0.1:5555/src.html?key1=val1")
                .method(Connection.Method.GET)
                .followRedirects(false)
                .execute();
        assertEquals(302, response.statusCode());
        assertEquals("http://127.0.0.1:5555/dest.html?key1=val1", response.header("Location"));
    }


    @Test
    public void testRedirect_pageInSubDir_withQueryString_shouldRedirectWithQuery() throws IOException {
        //Test that query strings are passed on with dirs
        Connection.Response response = Jsoup
                .connect("http://127.0.0.1:5555/dir1/dir2/src.html?key2=val2")
                .method(Connection.Method.GET)
                .followRedirects(false)
                .execute();
        assertEquals(302, response.statusCode());
        assertEquals("http://127.0.0.1:5555/dirdest/dest.html?key2=val2", response.header("Location"));
    }


}

@@ -228,37 +228,8 @@
 t;%0A%0A
-import java.io.IOException;%0A%0A
 impo
@@ -472,16 +472,109 @@
 eads(5)%0A
+                .forceHttps(true)%0A                .addHostRedirect(%22127.0.0.1%22, %22localhost%22)%0A
         
@@ -927,36 +927,41 @@
     public void 
-test
+WebServer
 Redirect_pageInR
@@ -945,33 +945,33 @@
 bServerRedirect_
-p
+P
 ageInRoot_should
@@ -964,17 +964,17 @@
 eInRoot_
-s
+S
 houldRed
@@ -1312,36 +1312,37 @@
 Equals(%22http
+s
 ://
-127.0.0.1
+localhost
 :5555/dest.h
@@ -1403,36 +1403,41 @@
     public void 
-test
+WebServer
 Redirect_pageInS
@@ -1421,33 +1421,33 @@
 bServerRedirect_
-p
+P
 ageInSubDir_shou
@@ -1442,17 +1442,17 @@
 nSubDir_
-s
+S
 houldRed
@@ -1800,36 +1800,37 @@
 Equals(%22http
+s
 ://
-127.0.0.1
+localhost
 :5555/dirdes
@@ -1899,36 +1899,41 @@
     public void 
-test
+WebServer
 Redirect_pageInR
@@ -1921,25 +1921,25 @@
 verRedirect_
-p
+P
 ageInRoot_wi
@@ -1935,18 +1935,17 @@
 geInRoot
-_w
+W
 ithQuery
@@ -1943,33 +1943,33 @@
 WithQueryString_
-s
+S
 houldRedirectWit
@@ -2318,36 +2318,37 @@
 Equals(%22http
+s
 ://
-127.0.0.1
+localhost
 :5555/dest.h
@@ -2427,20 +2427,25 @@
 ic void 
-test
+WebServer
 Redirect
@@ -2445,17 +2445,17 @@
 edirect_
-p
+P
 ageInSub
@@ -2461,10 +2461,9 @@
 bDir
-_w
+W
 ithQ
@@ -2473,17 +2473,17 @@
 yString_
-s
+S
 houldRed
@@ -2510,10 +2510,8 @@
 ows 
-IO
 Exce
@@ -2887,36 +2887,37 @@
 Equals(%22http
+s
 ://
-127.0.0.1
+localhost
 :5555/dirdes
@@ -2978,10 +2978,485 @@
     %7D%0A%0A%0A
+    @Test%0A    public void WebServerRedirect_MethodCall_ShouldNotRedirect() throws Exception %7B%0A        //Test that query strings are passed on with dirs%0A        Connection.Response response = Jsoup%0A                .connect(%22http://127.0.0.1:5555/call/ping%22)%0A                .method(Connection.Method.GET)%0A                .followRedirects(false)%0A                .execute();%0A        assertEquals(200, response.statusCode());%0A        assertEquals(%22pong%22, response.body());%0A    %7D%0A
 %7D%0A

Test added