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); }
public String asString(String charset) throws IOException { byte[] bytes = asBytes(); return new String(bytes, charset); }
public void writeTo(OutputStream out) throws IOException { InputStream in = asStream(); if (in == null) return; byte[] buffer = new byte[10240]; int r; while ((r = in.read(buffer)) != -1) { out.write(buffer, 0, r); } out.flush(); }
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 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("I say: helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo", makeRequest("/say/helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo").get().asString()); byte[] bytes = makeRequest("/say/helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo?gzip=true").get().asBytes(); Assert.assertNotEquals("I say: helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo", new String(bytes));
@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()); } }
}; Assert.assertEquals("ok", makeRequest("/").get().asString()); Assert.assertEquals("ok", makeRequest("/").get().asString()); Assert.assertEquals("ok", makeRequest("/").get().asString()); Assert.assertEquals("ok", makeRequest("/").get().asString()); Assert.assertEquals("ok", makeRequest("/").get().asString()); Assert.assertEquals("ok", makeRequest("/").get().asString()); Assert.assertEquals("ok", makeRequest("/").get().asString()); }; Assert.assertEquals(md5, MD5.create().update(makeRequest("/").get().asStream()).asString()); Assert.assertEquals(md5, MD5.create().update(makeRequest("/").get().asStream()).asString()); Assert.assertEquals(md5, MD5.create().update(makeRequest("/").get().asStream()).asString()); Assert.assertEquals(md5, MD5.create().update(makeRequest("/").get().asStream()).asString()); }; Assert.assertEquals(md5, MD5.create().update(makeRequest("/").get().asStream()).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() 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")); }
session = new HttpSession(); Response r = new Response(this, c); String path = r.getHeader("Location"); if (!path.startsWith("http://") && !path.startsWith("https://")) path = u.getProtocol() + "://" + u.getHost() + path; return new Response(this, c); } catch (SocketTimeoutException e) { if (retryNumber < maxRetryCount) {
Assert.assertArrayEquals(test, makeRequest("/") .header("Range", "bytes=0-99") .get().asBytes()); .get().asBytes()); Assert.assertArrayEquals(test, makeRequest("/") .header("Range", "bytes=100-") .get().asBytes()); Assert.assertArrayEquals(test, makeRequest("/") .header("Range", "bytes=-200") .get().asBytes()); Assert.assertArrayEquals(test, makeRequest("/") .header("Range", "bytes=800-2000") .get().asBytes()); .get().getResponseCode()); .get().getResponseCode());
public String header(String key) { return getHeader(key); }
public List<Cookie> cookies() { return getCookies(); }
protected Response(Request request, HttpURLConnection connection) { this.connection = connection; this.request = request; if (request.session != null) request.session.appendCookies(parseCookies()); }
public int headerInt(String key, int def) { try { return headerInt(key); } catch (NumberFormatException e) { return def; } }
public long headerLong(String key, long def) { try { return headerLong(key); } catch (NumberFormatException e) { return def; } }
public Map<String, List<String>> headers() { return getHeaders(); }
@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()); }