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])); } } }
public Request createRequest(String url) { Request request = new Request(url) .addHeaders(headers) .addParameterLists(params) .setPauseBetweenRetries(pauseBetweenRetries) .setMaxRetryCount(maxRetryCount) .setProxy(proxy) .setSSLSocketFactory(sslFactory) .setHostnameVerifier(hostnameVerifier) .method(method) .connectTimeout(connectTimeout) .readTimeout(readTimeout) .setUrlEncoding(charsetForEncoding); request.data = data; request.dataTypes = dataTypes; request.dataArrays = dataArrays; request.redirects = redirects; request.multipart = multipart; return request; }
@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 test_parameters_array_primitives() throws IOException { Class<? extends Controller> controller = TestParametersArrayPrimitivesController.class; server.getUrlMapping() .append("/int", controller, "test_int") .append("/long", controller, "test_long") .append("/short", controller, "test_short") .append("/byte", controller, "test_byte") .append("/boolean", controller, "test_boolean") .append("/float", controller, "test_float") .append("/double", controller, "test_double") .append("/char", controller, "test_char") ; Assert.assertEquals("[1, 2, 3]", makeRequest("/int").param("v", 1).param("v", 2).param("v", 3).get().asString()); Assert.assertEquals("[1, 2, 3]", makeRequest("/long").param("v", 1).param("v", 2).param("v", 3).get().asString()); Assert.assertEquals("[1, 2, 3]", makeRequest("/short").param("v", 1).param("v", 2).param("v", 3).get().asString()); Assert.assertEquals("[1, 2, 3]", makeRequest("/byte").param("v", 1).param("v", 2).param("v", 3).get().asString()); Assert.assertEquals("[1.0, 2.0, 3.0]", makeRequest("/float").param("v", 1).param("v", 2).param("v", 3).get().asString()); Assert.assertEquals("[1.0, 2.0, 3.0]", makeRequest("/double").param("v", 1).param("v", 2).param("v", 3).get().asString()); Assert.assertEquals("[true, false, true]", makeRequest("/boolean").param("v", true).param("v", false).param("v", true).get().asString()); Assert.assertEquals("[a, b, c]", makeRequest("/char").param("v", 'a').param("v", 'b').param("v", 'c').get().asString()); }
url = createURL(url, params); if (data == null) { c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); data = createPostParameters(params, charsetForEncoding).getBytes(charsetForEncoding); c.setRequestProperty("Connection", "Keep-Alive"); c.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryZzaC4MkAfrAMfJCJ"); c.setRequestProperty("Content-Length", String.valueOf(getLength())); path = u.getProtocol() + "://" + u.getHost() + path; return session.createRequest(path).get(); throw Unchecked.rethrow(ex1); return execute(++retryNumber);
@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 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()); }
Assert.assertEquals("1", makeRequest("/int").param("v", 1).get().asString()); Assert.assertEquals("1", makeRequest("/long").param("v", 1).get().asString()); Assert.assertEquals("1", makeRequest("/short").param("v", 1).get().asString()); Assert.assertEquals("1", makeRequest("/byte").param("v", 1).get().asString()); Assert.assertEquals("1.0", makeRequest("/float").param("v", 1).get().asString()); Assert.assertEquals("1.0", makeRequest("/double").param("v", 1).get().asString()); Assert.assertEquals("true", makeRequest("/boolean").param("v", true).get().asString()); Assert.assertEquals("a", makeRequest("/char").param("v", 'a').get().asString()); Assert.assertEquals("1", makeRequest("/int").param("v", 1).post().asString()); Assert.assertEquals("1", makeRequest("/long").param("v", 1).post().asString()); Assert.assertEquals("1", makeRequest("/short").param("v", 1).post().asString()); Assert.assertEquals("1", makeRequest("/byte").param("v", 1).post().asString()); Assert.assertEquals("1.0", makeRequest("/float").param("v", 1).post().asString()); Assert.assertEquals("1.0", makeRequest("/double").param("v", 1).post().asString()); Assert.assertEquals("true", makeRequest("/boolean").param("v", true).post().asString()); Assert.assertEquals("a", makeRequest("/char").param("v", 'a').post().asString()); Assert.assertEquals("1", makeRequest("/int").param("v", 1).addByteArray("data", new byte[0], "data").post().asString()); Assert.assertEquals("1", makeRequest("/long").param("v", 1).addByteArray("data", new byte[0], "data").post().asString()); Assert.assertEquals("1", makeRequest("/short").param("v", 1).addByteArray("data", new byte[0], "data").post().asString()); Assert.assertEquals("1", makeRequest("/byte").param("v", 1).addByteArray("data", new byte[0], "data").post().asString()); Assert.assertEquals("1.0", makeRequest("/float").param("v", 1).addByteArray("data", new byte[0], "data").post().asString()); Assert.assertEquals("1.0", makeRequest("/double").param("v", 1).addByteArray("data", new byte[0], "data").post().asString()); Assert.assertEquals("true", makeRequest("/boolean").param("v", true).addByteArray("data", new byte[0], "data").post().asString()); Assert.assertEquals("a", makeRequest("/char").param("v", 'a').addByteArray("data", new byte[0], "data").post().asString()); checkResponse(400, "{\"messages\":[\"java.lang.NullPointerException: parameter 'v' is not present\"]}", makeRequest("/int").get()); checkResponse(400, "{\"messages\":[\"java.lang.NumberFormatException: For input string: \\\"a\\\"\"]}", makeRequest("/int").param("v", "a").get());
System.arraycopy(data, 0, test, 0, test.length); Assert.assertArrayEquals(test, makeRequest("/") .header("Range", "bytes=0-99") .get().asBytes()); .header("Range", "bytes=100-199") .get().asBytes()); .header("Range", "bytes=100-") .get().asBytes()); .header("Range", "bytes=-200") .get().asBytes()); .header("Range", "bytes=800-2000") .get().asBytes()); .header("Range", "bytes=400-200") .get().getResponseCode()); .header("Range", "bytes=1000-1001") .get().getResponseCode());
@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 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 testParams() throws IOException { handler = (request, response) -> { Assert.assertEquals("key=value&array=1&array=2&empty=&=empty&=", request.getQueryString()); Assert.assertEquals("value", request.param("key")); Assert.assertEquals("1", request.param("array")); List<String> params = request.params("array"); Assert.assertEquals(2, params.size()); Assert.assertEquals("1", params.get(0)); Assert.assertEquals("2", params.get(1)); Assert.assertEquals("", request.param("empty")); Assert.assertEquals("empty", request.param("")); Assert.assertEquals("", request.params("").get(1)); return response.setBody("ok"); }; Assert.assertEquals("ok", makeRequest("/path") .addParameter("key", "value") .addParameter("array", "1") .addParameter("array", "2") .addParameter("empty", "") .addParameter("", "empty") .addParameter("", "") .get().asString()); handler = (request, response) -> { Assert.assertEquals("", request.param("key")); return response.setBody("ok"); }; Assert.assertEquals("ok", makeRequest("/path?key").get().asString()); }
@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 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() 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_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()); } }
public Response execute() throws IOException { return execute(0); }
public String getUrl() { try { return createURL(url, params); } catch (UnsupportedEncodingException e) { return url; } }
private String createURL(String url, Map<String, List<String>> params) throws UnsupportedEncodingException { if (params == null || params.isEmpty()) { return url; } StringBuilder sb = new StringBuilder(url); if (!params.isEmpty()) sb.append("?").append(createPostParameters(params, null).replace("+", "%20")); return sb.toString(); }