@Test public void testHeaderNameValidation() { for (char c : "\0\t\n\u000B\f\r ,:;=\u0080".toCharArray()) { try { HttpUtils.validateHeaderName(Character.toString(c)); fail("Char 0x" + Integer.toHexString(c) + " should not be valid"); } catch (IllegalArgumentException ignore) { // Ok } } }
@Test public void testSharedServersRoundRobinWithOtherServerRunningOnDifferentPort() throws Exception { // Have a server running on a different port to make sure it doesn't interact CountDownLatch latch = new CountDownLatch(1); HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(8081)); theServer.requestHandler(req -> { fail("Should not process request"); }).listen(onSuccess(s -> latch.countDown())); awaitLatch(latch); testSharedServersRoundRobin(); }
@Test public void testHeaderValueValidation() { List<String> invalid = Arrays.asList("\f", "\0", "\u000b", "\r\n3", "\r3", "\n3", "\n\r"); for (String test : invalid) { try { HttpUtils.validateHeaderValue(test); fail("String \"" + test + "\" should not be valid"); } catch (IllegalArgumentException e) { // Ok } } List<String> valid = Arrays.asList("\r\n\t", "\r\n ", "\n\t", "\n "); for (String test : valid) { HttpUtils.validateHeaderValue(test); } }
@Test public void testSharedServersRoundRobinButFirstStartAndStopServer() throws Exception { // Start and stop a server on the same port/host before hand to make sure it doesn't interact CountDownLatch latch = new CountDownLatch(1); HttpServer theServer = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)); theServer.requestHandler(req -> { fail("Should not process request"); }).listen(onSuccess(s -> latch.countDown())); awaitLatch(latch); CountDownLatch closeLatch = new CountDownLatch(1); theServer.close(ar -> { assertTrue(ar.succeeded()); closeLatch.countDown(); }); assertTrue(closeLatch.await(10, TimeUnit.SECONDS)); testSharedServersRoundRobin(); }
@Test public void testSendFileFailsWhenClientClosesConnection() throws Exception { // 10 megs final File f = setupFile("file.pdf", TestUtils.randomUnicodeString(10 * 1024 * 1024)); server.requestHandler(req -> { try { req.response().sendFile(f.getAbsolutePath(), ar -> { if (ar.failed()) { // Broken pipe testComplete(); } else { fail(new Exception("It should not reach this point")); } }); } catch (Exception e) { // this was the bug reported with issues/issue-80 fail(e); } }); startServer(); vertx.createNetClient().connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, socket -> { socket.result().write("GET / HTTP/1.1\r\n\r\n").close(); }); await(); }
@Test public void testPoolingNoKeepAliveAndPipelining() { try { vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false).setPipelining(true)); fail(); } catch (IllegalStateException ignore) { } }
@Test public void testServerExceptionHandler() throws Exception { server.exceptionHandler(err -> { assertTrue(err instanceof TooLongFrameException); testComplete(); }); server.requestHandler(req -> { fail(); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { }); req.putHeader("the_header", TestUtils.randomAlphaString(10000)); req.sendHead(); await(); }
@Test public void testIncorrectHttpVersion() throws Exception { server.requestHandler(req -> { NetSocket so = req.netSocket(); so.write(Buffer.buffer("HTTP/1.2 200 OK\r\nContent-Length:5\r\n\r\nHELLO")); so.close(); }); startServer(); AtomicBoolean a = new AtomicBoolean(); HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> { if (a.compareAndSet(false, true)) { assertTrue("message " + err.getMessage() + " should contain HTTP/1.2", err.getMessage().contains("HTTP/1.2")); } })); req.exceptionHandler(err -> { fail("Should not be called"); }).putHeader("connection", "close") .connectionHandler(conn -> conn.closeHandler(v -> testComplete())) .end(); await(); }
req.version(); fail("Should throw exception"); } catch (IllegalStateException e) {
@Test public void testRequestHandlerNotCalledInvalidRequest() { server.requestHandler(req -> { fail(); }); server.listen(onSuccess(s -> { vertx.createNetClient(new NetClientOptions()).connect(8080, "127.0.0.1", onSuccess(socket -> { socket.closeHandler(r -> { testComplete(); }); socket.write("GET HTTP1/1\r\n"); // trigger another write to be sure we detect that the other peer has closed the connection. socket.write("X-Header: test\r\n"); })); })); await(); }
fail("Should not throw exception: " + t.getMessage()); }); req.headers().set("count", String.valueOf(i));
@Test public void testRequestTimeoutExtendedWhenResponseChunksReceived() { long timeout = 2000; int numChunks = 100; AtomicInteger count = new AtomicInteger(0); long interval = timeout * 2 / numChunks; server.requestHandler(req -> { req.response().setChunked(true); vertx.setPeriodic(interval, timerID -> { req.response().write("foo"); if (count.incrementAndGet() == numChunks) { req.response().end(); vertx.cancelTimer(timerID); } }); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); resp.endHandler(v -> testComplete()); })); req.exceptionHandler(t -> fail("Should not be called")); req.setTimeout(timeout); req.end(); })); await(); }
@Test public void testHttpClientRequestShouldExceptionHandlerWhenTheClosedHandlerIsCalled() throws Exception { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { vertx.setTimer(1000, id -> { req.response().close(); }); }); startServer(); HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", resp -> { }).setChunked(true); CountDownLatch latch = new CountDownLatch(1); req.sendHead(version -> latch.countDown()); awaitLatch(latch); CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), req); req.exceptionHandler(err -> { Throwable failure = sender.close(); if (failure != null) { fail(failure); } else { testComplete(); } }); sender.send(); await(); }
client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(keepAlive).setPipelining(pipelined)); HttpClientRequest post = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { fail(); }); post.setChunked(true).write(TestUtils.randomBuffer(1024));
}); })).exceptionHandler(err -> { fail(); }).end();
@Test public void testHttpServerRequestShouldExceptionHandlerWhenTheClosedHandlerIsCalled() { server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> { HttpServerResponse resp = req.response(); resp.setChunked(true); CheckingSender sender = new CheckingSender(vertx.getOrCreateContext(), resp); sender.send(); resp.closeHandler(v -> { Throwable failure = sender.close(); if (failure != null) { fail(failure); } else { testComplete(); } }); }); server.listen(onSuccess(s -> { client.getNow(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/someuri", onSuccess(resp -> { vertx.setTimer(1000, id -> { resp.request().connection().close(); }); })); })); await(); }
@Test public void testHttpSocksProxyRequest() throws Exception { startProxy(null, ProxyType.SOCKS5); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setHost("localhost").setPort(proxy.getPort()))); server.requestHandler(req -> { req.response().end(); }); server.listen(onSuccess(s -> { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertNotNull("request did not go through proxy", proxy.getLastUri()); testComplete(); })).exceptionHandler(th -> fail(th)).end(); })); await(); }
@Test public void testHttpSocksProxyRequestAuth() throws Exception { startProxy("user", ProxyType.SOCKS5); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.SOCKS5).setHost("localhost").setPort(proxy.getPort()) .setUsername("user").setPassword("user"))); server.requestHandler(req -> { req.response().end(); }); server.listen(onSuccess(s -> { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertNotNull("request did not go through proxy", proxy.getLastUri()); testComplete(); })).exceptionHandler(th -> fail(th)).end(); })); await(); }
@Test public void testHttpProxyRequestAuth() throws Exception { startProxy("user", ProxyType.HTTP); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()) .setUsername("user").setPassword("user"))); server.requestHandler(req -> { req.response().end(); }); server.listen(onSuccess(s -> { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertNotNull("request did not go through proxy", proxy.getLastUri()); assertEquals("Host header doesn't contain target host", "localhost:8080", proxy.getLastRequestHeaders().get("Host")); testComplete(); })).exceptionHandler(th -> fail(th)).end(); })); await(); }
testComplete(); })); req.exceptionHandler(t -> fail("Should not throw exception")); req.setTimeout(3000); req.end();