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_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()); }
.get().getResponseCode()); .get().getResponseCode());
@Test public void before() throws IOException { handler = new UrlHandler() .append("/allowed", (request, response) -> response.setBody("ok")) .append("/notAllowed", (request, response) -> response.setBody("ok")); server.getFiltersMapping().addBefore("/notAllowed", (request, response) -> { boolean allowed = "true".equals(request.param("butAllowed")); if (!allowed) { response.setStatus(Status._403); } return allowed; }); Assert.assertEquals("ok", makeRequest("/allowed").get().asString()); Assert.assertEquals(403, makeRequest("/notAllowed").get().getResponseCode()); Assert.assertEquals("", makeRequest("/notAllowed").get().asString()); Assert.assertEquals(200, makeRequest("/notAllowed?butAllowed=true").get().getResponseCode()); Assert.assertEquals("ok", makeRequest("/notAllowed?butAllowed=true").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 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 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 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()); }
@Test public void test_multipart_1() throws IOException { handler = new MultipartHandler((request, response) -> { String foo = request.entry("foo").asString(); String bar = request.entry("bar").asString(); String fileMD5 = MD5.create().update(request.entry("file").asBytes()).asString(); return response.setBody(foo + "-" + bar + "-" + fileMD5); }); byte[] data = new byte[2 * 1024 * 1024]; new Random().nextBytes(data); for (int i = 0; i < 10; i++) { com.wizzardo.tools.http.Response response = makeRequest("/") .param("foo", "foo") .param("bar", "bar") .addByteArray("file", data, "file") .execute(); Assert.assertEquals(200, response.getResponseCode()); Assert.assertEquals("foo-bar-" + MD5.create().update(data).asString(), response.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 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")); }
@Test public void testBasicAuthToken() throws IOException { TokenFilter tokenFilter = new TokenFilter(new BasicAuthFilter()); handler = (request, response) -> { if (request.param("token") != null) return response.setBody("ok"); else return response.setBody(tokenFilter.generateToken(request)); }; String user = "user"; String password = "password"; server.getFiltersMapping().addBefore("/*", tokenFilter.allow(user, password)); Assert.assertEquals(401, makeRequest("").get().getResponseCode()); String token = makeRequest("").setBasicAuthentication(user, password).get().asString(); Assert.assertNotNull(token); Assert.assertEquals("ok", makeRequest("").param("token", token).get().asString()); }
@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")); }