@Test public void test() throws IOException { server.getUrlMapping() .append("/hello", HelloController.class, "hello"); Assert.assertEquals("hello!", makeRequest("/hello").get().asString()); Assert.assertEquals("GET, HEAD, POST, PUT, DELETE, OPTIONS", makeRequest("/hello").options().header("Allow")); }
@Test public void test_no_gzip_for_empty_response() throws IOException { String data = ""; handler = (request, response) -> response.setBody(data); server.getFiltersMapping().addAfter("/*", new GzipFilter()); Response response = makeRequest("/").get(); Assert.assertEquals(data, response.asString()); Assert.assertEquals(null, response.header("Content-Encoding")); Assert.assertEquals(0, response.getContentLength()); handler = (request, resp) -> resp.setBody(data).header(Header.KEY_CONTENT_ENCODING, Header.VALUE_GZIP); response = makeRequest("/").get(); Assert.assertEquals("gzip", response.header("Content-Encoding")); Assert.assertEquals(0, response.getContentLength()); } }
@Test public void test_gzip() throws IOException { String data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis feugiat nibh eu dapibus interdum. Fusce tincidunt neque nec urna rutrum, eget tristique nulla cursus. Aliquam eu convallis libero posuere."; handler = (request, response) -> response.setBody(data); server.getFiltersMapping().addAfter("/*", new GzipFilter()); Response response = makeRequest("/").get(); Assert.assertEquals(data, response.asString()); Assert.assertEquals("gzip", response.header("Content-Encoding")); Assert.assertEquals(158, response.getContentLength()); }
@Test public void testHead() throws IOException { handler = (request, response) -> response.setBody("response"); com.wizzardo.tools.http.Response response = makeRequest("/").setMethod(ConnectionMethod.HEAD).execute(); Assert.assertEquals(200, response.getResponseCode()); Assert.assertEquals("8", response.header("Content-Length")); Assert.assertEquals("", response.asString()); Assert.assertEquals("response", makeRequest("/").get().asString()); }
@Test public void test_only_get() throws IOException { server.getUrlMapping() .append("/hello", HelloController.class, "hello", GET); Assert.assertEquals("hello!", makeRequest("/hello").get().asString()); Assert.assertEquals("GET, HEAD, OPTIONS", makeRequest("/hello").options().header("Allow")); Assert.assertEquals(405, makeRequest("/hello").delete().getResponseCode()); }
@Test public void test_allowPostAndPut() throws IOException { handler = new UrlHandler() .append("/rest", new RestHandler() .post( (request, response) -> response.setBody("post") ) .put( (request, response) -> response.setBody("put") )) ; Assert.assertEquals("post", makeRequest("/rest").post().asString()); Assert.assertEquals("put", makeRequest("/rest").method(ConnectionMethod.PUT).execute().asString()); Assert.assertEquals(405, makeRequest("/rest").get().getResponseCode()); Assert.assertEquals("POST, PUT, OPTIONS", makeRequest("/rest").get().header("Allow")); Assert.assertEquals("POST, PUT, OPTIONS", makeRequest("/rest").options().header("Allow")); }
@Test public void test_allowNothing_butOptions() throws IOException { handler = new UrlHandler() .append("/rest", new RestHandler().get(null)) ; Assert.assertEquals(405, makeRequest("/rest").get().getResponseCode()); Assert.assertEquals("OPTIONS", makeRequest("/rest").get().header("Allow")); Assert.assertEquals(405, makeRequest("/rest").method(ConnectionMethod.HEAD).execute().getResponseCode()); }
@Test public void test_allowGetAndDelete() throws IOException { handler = new UrlHandler() .append("/rest", new RestHandler() .get( (request, response) -> response.setBody("get") ) .delete( (request, response) -> response.setBody("delete") )) ; Assert.assertEquals("get", makeRequest("/rest").get().asString()); Assert.assertEquals("delete", makeRequest("/rest").method(ConnectionMethod.DELETE).execute().asString()); Assert.assertEquals(405, makeRequest("/rest").post().getResponseCode()); Assert.assertEquals("GET, HEAD, DELETE, OPTIONS", makeRequest("/rest").post().header("Allow")); }