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()); System.arraycopy(data, 100, test, 0, test.length); Assert.assertArrayEquals(test, makeRequest("/") .header("Range", "bytes=100-") .get().asBytes()); System.arraycopy(data, 800, test, 0, test.length); Assert.assertArrayEquals(test, makeRequest("/") .header("Range", "bytes=-200") .get().asBytes()); System.arraycopy(data, 800, test, 0, test.length); Assert.assertArrayEquals(test, makeRequest("/") .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_simple() { handler = new UrlHandler() .append("/", (request, response) -> response.setBody("ok")); HttpServer<ObservedConnection> proxy = new HttpServer<ObservedConnection>(null, port + 1, context, 0) { @Override protected ObservedConnection createConnection(int fd, int ip, int port) { return new ObservedConnection(fd, ip, port, this); } }; proxy.getUrlMapping() .append("/", new ProxyHandler("localhost", port)); proxy.setIoThreadsCount(1); proxy.start(); try { Thread.sleep(25); Assert.assertEquals("ok", makeRequest("/").header("Connection", "Close").get().asString()); Assert.assertEquals("ok", makeRequest("/").header("Connection", "Keep-Alive").get().asString()); Assert.assertEquals("ok", makeRequest("/", port + 1).header("Connection", "Close").get().asString()); Assert.assertEquals("ok", makeRequest("/", port + 1).header("Connection", "Keep-Alive").get().asString()); } catch (Exception e) { e.printStackTrace(); assert false; } finally { proxy.close(); } }
@Test public void test_lazy_writing() { byte[] data = new byte[20 * 1024 * 1024]; ThreadLocalRandom.current().nextBytes(data); handler = new UrlHandler() .append("/", (request, response) -> response.setBody(data)); HttpServer<ObservedConnection> proxy = new HttpServer<ObservedConnection>(null, port + 1, context, 0) { @Override protected ObservedConnection createConnection(int fd, int ip, int port) { return new ObservedConnection(fd, ip, port, this); } }; proxy.getUrlMapping() .append("/", new ProxyHandler("localhost", port)); proxy.setIoThreadsCount(1); proxy.start(); try { Thread.sleep(25); Assert.assertArrayEquals(data, makeRequest("/").header("Connection", "Close").get().asBytes()); writeSocket(data, 0); writeSocket(data, 10); } catch (Exception e) { e.printStackTrace(); assert false; } finally { proxy.close(); } }
@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()); }