/*
 * Javalin - https://javalin.io
 * Copyright 2017 David Åse
 * Licensed under Apache 2.0: https://github.com/tipsy/javalin/blob/master/LICENSE
 */

package io.javalin;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import io.javalin.core.util.Util;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

public class TestContextPath {

    @Test
    public void test_normalizeContextPath_works() throws Exception {
        Function<String, String> normalize = Util.INSTANCE::normalizeContextPath;
        assertThat(normalize.apply("path"), is("/path"));
        assertThat(normalize.apply("/path"), is("/path"));
        assertThat(normalize.apply("/path/"), is("/path"));
        assertThat(normalize.apply("//path/"), is("/path"));
        assertThat(normalize.apply("/path//"), is("/path"));
        assertThat(normalize.apply("////path////"), is("/path"));
    }

    @Test
    public void test_prefixPath_works() throws Exception {
        BiFunction<String, String, String> prefix = Util.INSTANCE::prefixContextPath;
        assertThat(prefix.apply("*", "/c-p"), is("*"));
        assertThat(prefix.apply("/*", "/c-p"), is("/c-p/*"));
        assertThat(prefix.apply("path", "/c-p"), is("/c-p/path"));
        assertThat(prefix.apply("/path", "/c-p"), is("/c-p/path"));
        assertThat(prefix.apply("//path", "/c-p"), is("/c-p/path"));
        assertThat(prefix.apply("/path/", "/c-p"), is("/c-p/path/"));
        assertThat(prefix.apply("//path//", "/c-p"), is("/c-p/path/"));
    }

    @Test
    public void test_router_works() throws Exception {
        Javalin app = createAppWithContextPath("/context-path")
            .get("/hello", ctx -> ctx.result("Hello World"))
            .start();
        assertThat(GET_asString("/hello").getBody(), is("Not found. Request is below context-path (context-path: '/context-path')"));
        assertThat(GET_asString("/context-path/hello").getBody(), is("Hello World"));
        app.stop();
    }

    @Test
    public void test_twoLevelContextPath_works() throws Exception {
        Javalin app = createAppWithContextPath("/context-path/path-context")
            .get("/hello", ctx -> ctx.result("Hello World"))
            .start();
        assertThat(GET_asString("/context-path/").getStatus(), is(404));
        assertThat(GET_asString("/context-path/path-context/hello").getBody(), is("Hello World"));
        app.stop();
    }

    @Test
    public void test_staticFiles_work() throws Exception {
        Javalin app = createAppWithContextPath("/context-path").enableStaticFiles("/public").start();
        assertThat(GET_asString("/script.js").getStatus(), is(404));
        assertThat(GET_asString("/context-path/script.js").getBody(), containsString("JavaScript works"));
        app.stop();
    }

    @Test
    public void test_welcomeFile_works() throws Exception {
        Javalin app = createAppWithContextPath("/context-path").enableStaticFiles("/public").start();
        assertThat(GET_asString("/context-path/subdir/").getBody(), is("<h1>Welcome file</h1>"));
        app.stop();
    }

    private Javalin createAppWithContextPath(String contextPath) {
        return Javalin.create().port(7733).contextPath(contextPath);
    }

    private static HttpResponse<String> GET_asString(String pathname) throws Exception {
        return Unirest.get("http://localhost:7733/" + pathname).asString();
    }

}

@@ -561,16 +561,41 @@
 Path %7B%0A%0A
+    static Javalin app;%0A%0A
     @Tes
@@ -588,32 +588,32 @@
 app;%0A%0A    @Test%0A
-
     public void 
@@ -1814,32 +1814,24 @@
 n %7B%0A        
-Javalin 
 app = create
@@ -2278,32 +2278,24 @@
 n %7B%0A        
-Javalin 
 app = create
@@ -2698,32 +2698,24 @@
 n %7B%0A        
-Javalin 
 app = create
@@ -3069,24 +3069,16 @@
         
-Javalin 
 app = cr
@@ -3384,20 +3384,17 @@
 ().port(
-7733
+0
 ).contex
@@ -3558,12 +3558,26 @@
 ost:
-7733
+%22 + app.port() + %22
 /%22 +

Use port(0) in TestContextPath