@Test public void testDeliverPausedBufferWhenResumeOnOtherThread() throws Exception { ExecutorService exec = Executors.newSingleThreadExecutor(); try { testDeliverPausedBufferWhenResume(block -> exec.execute(() -> { try { Thread.sleep(10); } catch (InterruptedException e) { fail(e); Thread.currentThread().interrupt(); } block.run(); })); } finally { exec.shutdown(); } }
@Test public void testInvalidAbsoluteURI() { try { client.requestAbs(HttpMethod.GET, "ijdijwidjqwoijd192d192192ej12d", resp -> { }).end(); fail("Should throw exception"); } catch (VertxException e) { //OK } }
@Test public void testOtherMethodWithRawMethod() throws Exception { try { client.request(HttpMethod.OTHER, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { }).end(); fail(); } catch (IllegalStateException expected) { } }
@Test public void testClientExceptionHandlerCalledWhenServerTerminatesConnection() throws Exception { int numReqs = 10; CountDownLatch latch = new CountDownLatch(numReqs); server.requestHandler(request -> { request.response().close(); }).listen(DEFAULT_HTTP_PORT, onSuccess(s -> { // Exception handler should be called for any requests in the pipeline if connection is closed for (int i = 0; i < numReqs; i++) { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> { latch.countDown(); })) .exceptionHandler(error -> fail("Exception handler should not be called")) .end(); } })); awaitLatch(latch); }
@Test public void testClientExceptionHandlerCalledWhenExceptionOnDataHandler() throws Exception { server.requestHandler(request -> { request.response().end("foo"); }).listen(DEFAULT_HTTP_PORT, onSuccess(s -> { // Exception handler should be called for any exceptions in the data handler client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.handler(data -> { throw new RuntimeException("should be caught"); }); resp.exceptionHandler(t -> testComplete()); })).exceptionHandler(error -> fail()).end(); })); await(); }
@Test public void testClientExceptionHandlerCalledWhenExceptionOnBodyHandler() throws Exception { server.requestHandler(request -> { request.response().end("foo"); }).listen(DEFAULT_HTTP_PORT, onSuccess(s -> { // Exception handler should be called for any exceptions in the data handler client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.bodyHandler(data -> { throw new RuntimeException("should be caught"); }); resp.exceptionHandler(t -> testComplete()); })).exceptionHandler(error -> fail()).end(); })); await(); }
@Test public void testConnectionErrorsGetReportedToHandlers() throws InterruptedException { CountDownLatch latch = new CountDownLatch(3); // This one should cause an error in the Client Exception handler, because it has no exception handler set specifically. HttpClientRequest req1 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl1", onFailure(resp -> { latch.countDown(); })); req1.exceptionHandler(t -> { fail("Should not be called"); }); HttpClientRequest req2 = client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, "someurl2", onFailure(resp -> { latch.countDown(); })); AtomicInteger req2Exceptions = new AtomicInteger(); req2.exceptionHandler(t -> { assertEquals("More than one call to req2 exception handler was not expected", 1, req2Exceptions.incrementAndGet()); latch.countDown(); }); req1.end(); req2.sendHead(); awaitLatch(latch); testComplete(); }
@Test public void testNoExceptionHandlerCalledWhenResponseEnded() throws Exception { server.requestHandler(req -> { HttpServerResponse resp = req.response(); req.exceptionHandler(this::fail); resp.exceptionHandler(err -> { err.printStackTrace(); }); resp.end(); }).listen(DEFAULT_HTTP_PORT, onSuccess(s -> { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.endHandler(v -> { vertx.setTimer(100, tid -> testComplete()); }); resp.exceptionHandler(t -> { fail("Should not be called"); }); })).exceptionHandler(t -> { fail("Should not be called"); }).end(); })); await(); }
@Test public void testConnectInvalidPort() { client.request(HttpMethod.GET, 9998, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> testComplete())) .exceptionHandler(t -> fail("Exception handler should not be called")) .end(); await(); }
@Test public void testConnectInvalidHost() { client.request(HttpMethod.GET, 9998, "255.255.255.255", DEFAULT_TEST_URI, onFailure(resp -> testComplete())) .exceptionHandler(t -> fail("Exception handler should not be called")) .end(); await(); }
@Test public void testClientExceptionHandlerCalledWhenFailingToConnect() throws Exception { client.request(HttpMethod.GET, 9998, "255.255.255.255", DEFAULT_TEST_URI, onFailure(err -> testComplete())) .exceptionHandler(error -> fail("Exception handler should not be called")) .end(); await(); }
@Test public void testClientExceptionHandlerCalledWhenServerTerminatesConnectionAfterPartialResponse() throws Exception { server.requestHandler(request -> { //Write partial response then close connection before completing it request.response().setChunked(true).write("foo").close(); }).listen(DEFAULT_HTTP_PORT, onSuccess(s -> { // Exception handler should be called for any requests in the pipeline if connection is closed client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> resp.exceptionHandler(t -> testComplete()))).exceptionHandler(error -> fail()).end(); })); await(); }
@Test public void test100ContinueRejectedManually() throws Exception { server.close(); server = vertx.createHttpServer(createBaseServerOptions()); server.requestHandler(req -> { req.response().setStatusCode(405).end(); req.bodyHandler(data -> { fail("body should not be received"); }); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(405, resp.statusCode()); testComplete(); })); req.headers().set("Expect", "100-continue"); req.setChunked(true); req.continueHandler(v -> { fail("should not be called"); }); req.sendHead(); })); await(); }
@Test public void testHttpInvalidConnectResponseEnded() { waitFor(2); server.requestHandler(req -> { req.response().end(); try { req.netSocket(); fail(); } catch (IllegalStateException e) { complete(); } }); server.listen(onSuccess(s -> { client.request(HttpMethod.CONNECT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); complete(); })).end(); })); await(); }
@Test public void testFollowRedirectLimit() throws Exception { AtomicInteger redirects = new AtomicInteger(); server.requestHandler(req -> { int val = redirects.incrementAndGet(); if (val > 16) { fail(); } else { String scheme = createBaseServerOptions().isSsl() ? "https" : "http"; req.response().setStatusCode(301).putHeader(HttpHeaders.LOCATION, scheme + "://localhost:8080/otherpath").end(); } }); startServer(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(16, redirects.get()); assertEquals(301, resp.statusCode()); assertEquals("/otherpath", resp.request().path()); testComplete(); })).setFollowRedirects(true).end(); await(); }
@Test public void testHttpInvalidConnectResponseChunked() { waitFor(2); server.requestHandler(req -> { req.response().setChunked(true).write("some-chunk"); try { req.netSocket(); fail(); } catch (IllegalStateException e) { complete(); } }); server.listen(onSuccess(s -> { client.request(HttpMethod.CONNECT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); complete(); })).end(); })); await(); }
req.setExpectMultipart(true); req.uploadHandler(event -> event.handler(buffer -> { fail("Should not get here"); })); req.endHandler(v -> {