public String asString() throws IOException { String encoding = connection.getHeaderField("Content-Type"); if (encoding != null) { int i = encoding.indexOf("charset="); if (i > 0) { encoding = encoding.substring(i + "charset=".length()); } else { encoding = "utf-8"; } } else { encoding = "utf-8"; } return asString(encoding); }
protected void init() throws IOException { String data = HttpClient.createRequest(APACHE_MIME_TYPES_URL).get().asString(); for (String s : data.split("[\r\n]")) { if (s.startsWith("#")) continue; String[] type = s.split("\\s+"); for (int i = 1; i < type.length; i++) { types.append("*." + type[i], new Holder(type[0])); } } }
@Test public void testMethod() throws IOException { handler = (request, response) -> response.setBody(request.method().name()); System.out.println("get"); Assert.assertEquals("GET", makeRequest("/").get().asString()); System.out.println("post"); Assert.assertEquals("POST", makeRequest("/").post().asString()); System.out.println("put"); Assert.assertEquals("PUT", makeRequest("/").method(ConnectionMethod.PUT).execute().asString()); System.out.println("delete"); Assert.assertEquals("DELETE", makeRequest("/").method(ConnectionMethod.DELETE).execute().asString()); }
@Test public void testSession() throws IOException { Assert.assertEquals("1", makeRequest("/increment").get().asString()); Assert.assertEquals("2", makeRequest("/increment").get().asString()); Assert.assertEquals("1", makeRequest("/increment", new HttpSession()).get().asString()); Assert.assertEquals("4", makeRequest("/multiply").get().asString()); }
protected void checkResponse(int status, String message, com.wizzardo.tools.http.Response response) { Unchecked.run(() -> { Assert.assertEquals(status, response.getResponseCode()); Assert.assertEquals(message, response.asString()); }); }
@Test public void test_parameters_1() throws IOException { server.getUrlMapping() .append("/test_1", TestParametersController.class, "test_1"); Assert.assertEquals("1", makeRequest("/test_1").param("i", 1).get().asString()); Assert.assertEquals("2", makeRequest("/test_1").param("i", 2).get().asString()); server.getUrlMapping() .append("/test_2", TestParametersController.class, "test_2"); Assert.assertEquals("1", makeRequest("/test_2").param("i", 1).get().asString()); Assert.assertEquals("2", makeRequest("/test_2").param("i", 2).get().asString()); Assert.assertEquals("-1", makeRequest("/test_2").get().asString()); }
@Test public void testOk() throws IOException { handler = (request, response) -> response.setBody("ok"); Assert.assertEquals("ok", makeRequest("").get().asString()); }
@Test public void testContext() throws IOException { handler = new UrlHandler(context) .append("/action1", (request, response) -> response.setBody("action1")) .append("/", (request, response) -> response.setBody("action2")) ; Assert.assertEquals("action1", makeRequest("/context/action1").get().asString()); Assert.assertEquals("action2", makeRequest("/context/").get().asString()); Assert.assertEquals("/ not found", makeRequest("/").get().asString()); } }
@Test public void test_request_in_view() throws IOException { server.getUrlMapping().append("/variables", DefaultVariablesController.class, "test_1"); Assert.assertEquals("" + "/variables\n" + "bar\n" + "defaultVariables\n" + "test_1\n" + "defaultVariables.test_1\n" , makeRequest("/variables").get().asString()); }
@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 testMapping_regex() throws IOException { handler = new UrlHandler() .append("/action(\\d+)*", (request, response) -> response.setBody("ok")) ; Assert.assertEquals("ok", makeRequest("/action").get().asString()); Assert.assertEquals("ok", makeRequest("/action1").get().asString()); Assert.assertEquals(404, makeRequest("/404").get().getResponseCode()); }
@Test public void testInterface() throws IOException { Assert.assertEquals("J", makeRequest("/interface").get().asString()); J j = DependencyFactory.get(J.class); Assert.assertNotNull(j); Assert.assertEquals("J", j.doIt()); SimplesController3 controller = DependencyFactory.get(SimplesController3.class); Assert.assertNotNull(controller.j); Assert.assertEquals("J", controller.j.doIt()); Assert.assertEquals("J", makeRequest("/service").get().asString()); }
@Test public void testMappingVariables() throws IOException { Handler testHandler = (request, response) -> { Parameters params = request.params(); return response.setBody(params.get("a", " ") + "-" + params.get("b", " ") + "-" + params.get("c", " ") + "-" + params.get("d", " ") + "-" + params.get("e", " ")); }; this.handler = new UrlHandler() .append("/$a/$b/$c", testHandler) .append("/$a/$c/$d/$e", testHandler) ; Assert.assertEquals("a-b-c- - ", makeRequest("/a/b/c").get().asString()); Assert.assertEquals("a- -c-d-e", makeRequest("/a/c/d/e").get().asString()); }
@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() throws IOException { handler = new UrlHandler() .append("/set", (request, response) -> { request.session().put("key", "value"); return response.setBody("ok"); }).append("/get", (request, response) -> response.setBody(request.session().get("key").toString())); List<Cookie> cookies = makeRequest("/set").get().getCookies(); // System.out.println(cookies); Assert.assertEquals("value", makeRequest("/get").cookies(cookies).get().asString()); } }
@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 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 testBasicAuth() throws IOException { handler = (request, response) -> response.setBody("ok"); String user = "user"; String password = "password"; server.getFiltersMapping().addBefore("/*", new BasicAuthFilter().allow(user, password)); Assert.assertEquals(401, makeRequest("").get().getResponseCode()); Assert.assertEquals("ok", makeRequest("").setBasicAuthentication(user, password).get().asString()); }
@Test public void testDigestAuth() throws IOException { handler = (request, response) -> response.setBody("ok"); String user = "Mufasa"; String password = "Circle Of Life"; server.getFiltersMapping().addBefore("/*", new DigestAuthFilter("testrealm@host.com") { @Override protected String nonce() { return "dcd98b7102dd2f0e8b11d0f600bfb0c093"; } }.allow(user, password)); Assert.assertEquals(401, makeRequest("").get().getResponseCode()); Assert.assertEquals("ok", makeRequest("/dir/index.html").header("Authorization", "Digest username=\"Mufasa\", realm=\"testrealm@host.com\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", uri=\"/dir/index.html\", qop=auth, nc=00000001, cnonce=\"0a4f113b\", response=\"6629fae49393a05397450978507c4ef1\"").get().asString()); }