package io.javalin;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.servlet.http.HttpServletResponse;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class TestMethodNotAllowed {

    private static final String EXPECTED_HTML_BODY = "\n"
            + "                    <!DOCTYPE html>\n"
            + "                    <html lang=\"en\">\n"
            + "                        <head>\n"
            + "                            <meta charset=\"UTF-8\">\n"
            + "                            <title>Method Not Allowed</title>\n"
            + "                        </head>\n"
            + "                        <body>\n"
            + "                            <h1>405 - Method Not Allowed</h1>\n"
            + "                            <p>\n"
            + "                                Available Methods: <strong>GET, PUT, DELETE</strong>\n"
            + "                            </p>\n"
            + "                        </body>\n"
            + "                    </html>\n"
            + "                ";
    private static final String EXPECTED_JSON_BODY = "{\"availableMethods\":[\"GET\", \"PUT\", \"DELETE\"]}";
    private static Javalin app;
    private static String baseUrl;

    @BeforeClass
    public static void setup() {
        app = Javalin.create()
                     .port(0)
                     .prefer405over404()
                     .start();
        baseUrl = "http://localhost:" + app.port();

        app.get("/test", ctx -> ctx.result("Hello world"));
        app.put("/test", ctx -> ctx.result("Hello world"));
        app.delete("/test", ctx -> ctx.result("Hello world"));
    }

    @Test
    public void test_htmlMethodNotAllowed() throws UnirestException {
        HttpResponse<String> response = Unirest.post(baseUrl + "/test").asString();

        assertThat(response.getStatus(), is(HttpServletResponse.SC_METHOD_NOT_ALLOWED));
        assertThat(response.getBody(), is(EXPECTED_HTML_BODY));
    }

    @Test
    public void test_jsonMethodNotAllowed() throws UnirestException {
        HttpResponse<String> response = Unirest.post(baseUrl + "/test").header("Accept", "application/json").asString();

        assertThat(response.getStatus(), is(HttpServletResponse.SC_METHOD_NOT_ALLOWED));
        assertThat(response.getBody(), is(EXPECTED_JSON_BODY));
    }

    @AfterClass
    public static void tearDown() {
        app.stop();
    }
}

@@ -162,16 +162,63 @@
 eption;%0A
+import javax.servlet.http.HttpServletResponse;%0A
 import o
@@ -234,24 +234,24 @@
 AfterClass;%0A
-
 import org.j
@@ -295,57 +295,8 @@
 st;%0A
-%0Aimport javax.servlet.http.HttpServletResponse;%0A%0A
 impo
@@ -480,36 +480,32 @@
  = %22%5Cn%22%0A        
-    
 + %22             
@@ -530,36 +530,32 @@
 tml%3E%5Cn%22%0A        
-   
- 
 + %22             
@@ -583,36 +583,32 @@
 n%5C%22%3E%5Cn%22%0A        
-    
 + %22             
@@ -628,36 +628,32 @@
 ead%3E%5Cn%22%0A        
-  
-  
 + %22             
@@ -691,28 +691,24 @@
 UTF-8%5C%22%3E%5Cn%22%0A
-    
         + %22 
@@ -771,36 +771,32 @@
 tle%3E%5Cn%22%0A        
- 
-   
 + %22             
@@ -813,28 +813,24 @@
  %3C/head%3E%5Cn%22%0A
-    
         + %22 
@@ -862,36 +862,32 @@
 ody%3E%5Cn%22%0A        
-    
 + %22             
@@ -938,36 +938,32 @@
 /h1%3E%5Cn%22%0A        
-   
- 
 + %22             
@@ -980,28 +980,24 @@
      %3Cp%3E%5Cn%22%0A
-    
         + %22 
@@ -1083,36 +1083,32 @@
 ong%3E%5Cn%22%0A        
-  
-  
 + %22             
@@ -1130,36 +1130,32 @@
 %3C/p%3E%5Cn%22%0A        
-    
 + %22             
@@ -1176,36 +1176,32 @@
 ody%3E%5Cn%22%0A        
- 
-   
 + %22             
@@ -1214,28 +1214,24 @@
  %3C/html%3E%5Cn%22%0A
-    
         + %22 
@@ -1519,25 +1519,16 @@
         
-         
 .port(0)
@@ -1540,25 +1540,16 @@
         
-     
-    
 .prefer4
@@ -1560,25 +1560,16 @@
 er404()%0A
-         
         

Format 405 test code