@Test public void testListenInvalidPort() throws Exception { /* Port 7 is free for use by any application in Windows, so this test fails. */ Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows")); server.close(); server = vertx.createHttpServer(new HttpServerOptions().setPort(7)); server.requestHandler(noOpHandler()).listen(onFailure(server -> testComplete())); await(); }
@Test public void testListenInvalidHost() { server.close(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setHost("iqwjdoqiwjdoiqwdiojwd")); server.requestHandler(noOpHandler()); server.listen(onFailure(s -> testComplete())); }
@Test public void testClientConnectInvalidPort() { client.getNow(-1, "localhost", "/someuri", onFailure(err -> { assertEquals(err.getClass(), IllegalArgumentException.class); assertEquals(err.getMessage(), "port p must be in range 0 <= p <= 65535"); testComplete(); })); await(); }
@Test public void testRequestTimeoutCanceledWhenRequestHasAnOtherError() { AtomicReference<Throwable> exception = new AtomicReference<>(); // There is no server running, should fail to connect client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(exception::set)) .setTimeout(800) .end(); vertx.setTimer(1500, id -> { assertNotNull("Expected an exception to be set", exception.get()); assertFalse("Expected to not end with timeout exception, but did: " + exception.get(), exception.get() instanceof TimeoutException); testComplete(); }); 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 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 testSendFileDirectoryWithHandler() throws Exception { File dir = testFolder.newFolder(); server.requestHandler(req -> { req.response().putHeader("Content-Type", "wibble"); req.response().sendFile(dir.getAbsolutePath(), onFailure(t -> { assertTrue(t instanceof FileNotFoundException); testComplete(); })); }); server.listen(onSuccess(s -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> {})).end(); })); await(); }
@Test public void testSendFileNotFoundWithHandler() throws Exception { server.requestHandler(req -> { req.response().putHeader("Content-Type", "wibble"); req.response().sendFile("nosuchfile.html", onFailure(t -> { assertTrue(t instanceof FileNotFoundException); testComplete(); })); }); server.listen(onSuccess(s -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(resp -> {})).end(); })); await(); }
@Test public void testRequestTimesoutWhenIndicatedPeriodExpiresWithoutAResponseFromRemoteServer() { server.requestHandler(noOpHandler()); // No response handler so timeout triggers AtomicBoolean failed = new AtomicBoolean(); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(t -> { // Catch the first, the second is going to be a connection closed exception when the // server is shutdown on testComplete if (failed.compareAndSet(false, true)) { assertTrue("Expected to end with timeout exception but ended with other exception: " + t, t instanceof TimeoutException); testComplete(); } })); req.setTimeout(1000); req.end(); })); await(); }
@Test public void testServerConnectionClose() throws Exception { // Test server connection close + client close handler server.requestHandler(req -> { req.connection().close(); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(err -> { })) .connectionHandler(conn -> { conn.closeHandler(v -> { testComplete(); }); }).sendHead(); await(); }
@Test public void testFollowRedirectPropagatesTimeout() throws Exception { AtomicInteger redirections = new AtomicInteger(); server.requestHandler(req -> { switch (redirections.getAndIncrement()) { case 0: String scheme = createBaseServerOptions().isSsl() ? "https" : "http"; req.response().setStatusCode(307).putHeader(HttpHeaders.LOCATION, scheme + "://localhost:8080/whatever").end(); break; } }); startServer(); AtomicBoolean done = new AtomicBoolean(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(t -> { if (done.compareAndSet(false, true)) { assertEquals(2, redirections.get()); testComplete(); } })).setFollowRedirects(true) .setTimeout(500).end(); await(); }
@Test public void testClientConnectionClose() throws Exception { // Test client connection close + server close handler CountDownLatch latch = new CountDownLatch(1); server.requestHandler(req -> { AtomicInteger len = new AtomicInteger(); req.handler(buff -> { if (len.addAndGet(buff.length()) == 1024) { latch.countDown(); } }); req.connection().closeHandler(v -> { testComplete(); }); }); CountDownLatch listenLatch = new CountDownLatch(1); server.listen(onSuccess(s -> listenLatch.countDown())); awaitLatch(listenLatch); HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(err -> {})); req.setChunked(true); req.write(TestUtils.randomBuffer(1024)); awaitLatch(latch); req.connection().close(); await(); }
@Test public void testHttpClientRequestTimeoutResetsTheConnection() throws Exception { waitFor(3); server.requestHandler(req -> { AtomicBoolean errored = new AtomicBoolean(); req.exceptionHandler(err -> { if (errored.compareAndSet(false, true)) { complete(); } }); }); startServer(); HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> { complete(); })); AtomicBoolean errored = new AtomicBoolean(); req.exceptionHandler(err -> { if (errored.compareAndSet(false, true)) { complete(); } }); CountDownLatch latch = new CountDownLatch(1); req.setChunked(true).sendHead(version -> latch.countDown()); awaitLatch(latch); req.setTimeout(100); await(); }
@Test public void testClientSynchronousConnectFailures() { System.setProperty("vertx.disableDnsResolver", "true"); Vertx vertx = Vertx.vertx(new VertxOptions().setAddressResolverOptions(new AddressResolverOptions().setQueryTimeout(100))); try { int poolSize = 2; HttpClient client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(poolSize)); AtomicInteger failures = new AtomicInteger(); vertx.runOnContext(v -> { for (int i = 0; i < (poolSize + 1); i++) { AtomicBoolean f = new AtomicBoolean(); client.getAbs("http://invalid-host-name.foo.bar", onFailure(resp -> { if (f.compareAndSet(false, true)) { if (failures.incrementAndGet() == poolSize + 1) { testComplete(); } } })).end(); } }); await(); } finally { vertx.close(); System.setProperty("vertx.disableDnsResolver", "false"); } }
@Test public void testSendFileNotFound() throws Exception { server.requestHandler(req -> { req.response().putHeader("Content-Type", "wibble"); req.response().sendFile("nosuchfile.html"); }); server.listen(onSuccess(s -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(err -> {})).end(); vertx.setTimer(100, tid -> testComplete()); })); 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 testClientRequestExceptionHandlerCalledWhenConnectionClosed() throws Exception { server.requestHandler(req -> { req.handler(buff -> { req.connection().close(); }); }); startServer(); HttpClientRequest req = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onFailure(err -> {})).setChunked(true); req.exceptionHandler(err -> { testComplete(); }); req.write("chunk"); await(); }
@Test public void testListenInvalidPort() throws Exception { /* Port 7 is free for use by any application in Windows, so this test fails. */ Assume.assumeFalse(System.getProperty("os.name").startsWith("Windows")); server.close(); server = vertx.createHttpServer(new HttpServerOptions().setPort(7)); server.requestHandler(noOpHandler()).listen(onFailure(server -> testComplete())); await(); }