protected void tearDown() throws Exception { if (client != null) { try { client.close(); } catch (IllegalStateException ignore) { // Client was already closed by the test } } if (server != null) { CountDownLatch latch = new CountDownLatch(1); server.close((asyncResult) -> { assertTrue(asyncResult.succeeded()); latch.countDown(); }); awaitLatch(latch); } super.tearDown(); }
@Test public void testClientMakeRequestHttp2WithSSLWithoutAlpn() throws Exception { client.close(); client = vertx.createHttpClient(createBaseClientOptions().setUseAlpn(false)); try { client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI); fail(); } catch (IllegalArgumentException ignore) { // Expected } }
@Test public void testClientWebsocketIdleTimeout() { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setIdleTimeout(1)); server.websocketHandler(ws -> {}).listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", ws -> { ws.closeHandler(v -> testComplete()); }); }); await(); }
@Test public void testClientOptionsCopiedBeforeUse() { client.close(); server.requestHandler(req -> { req.response().end(); }); server.listen(ar -> { assertTrue(ar.succeeded()); HttpClientOptions options = new HttpClientOptions(); client = vertx.createHttpClient(options); // Now change something - but server should ignore this options.setSsl(true); client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/uri", onSuccess(res -> { assertEquals(200, res.statusCode()); testComplete(); })).end(); }); await(); }
@Test public void testClientContextWithKeepAlive() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(false).setMaxPoolSize(1)); testClientContext(); }
@Test public void testClientContextWithPipelining() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(true).setMaxPoolSize(1)); testClientContext(); }
@Test public void testHttpClientName() throws Exception { HttpClient client1 = vertx.createHttpClient(); try { FakeHttpClientMetrics metrics1 = FakeMetricsBase.getMetrics(client1); assertEquals("", metrics1.getName()); String name = TestUtils.randomAlphaString(10); HttpClient client2 = vertx.createHttpClient(new HttpClientOptions().setMetricsName(name)); try { FakeHttpClientMetrics metrics2 = FakeMetricsBase.getMetrics(client2); assertEquals(name, metrics2.getName()); } finally { client2.close(); } } finally { client1.close(); } }
@Test public void testUnknownContentLengthIsSetToZeroWithHTTP_1_0() throws Exception { server.requestHandler(req -> { req.response().write("Some-String").end(); }); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setProtocolVersion(HttpVersion.HTTP_1_0)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertNull(resp.getHeader("Content-Length")); testComplete(); })); await(); }
@Test public void testClientLogging() throws Exception { client.close(); client = vertx.createHttpClient(createBaseClientOptions().setLogActivity(true)); TestLoggerFactory factory = testLogging(); if (this instanceof Http1xTest) { assertTrue(factory.hasName("io.netty.handler.logging.LoggingHandler")); } else { assertTrue(factory.hasName("io.netty.handler.codec.http2.Http2FrameLogger")); } }
@Test public void testPerPeerPooling() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setMaxPoolSize(1) .setKeepAlive(true) .setPipelining(false)); testPerXXXPooling((i, handler) -> client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", handler).setHost("host" + i + ":8080"), HttpServerRequest::host); }
@Test public void testHttpClientResumeConnectionOnResponseOnLastMessage() throws Exception { server.requestHandler(req -> req.response().end("ok")); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setMaxPoolSize(1)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp1 -> { resp1.pause(); // The connection resume is asynchronous and the end message will be received before connection resume happens resp1.resume(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp2 -> { testComplete(); })); })); await(); }
private void testHttpClientResponsePause(Handler<HttpClientResponse> h) throws Exception { server.requestHandler(req -> req.response().end("ok")); startServer(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setMaxPoolSize(1).setKeepAlive(true)); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp1 -> { h.handle(resp1); vertx.setTimer(10, timerId -> { // The connection should be resumed as it's ended client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp2 -> { assertSame(resp1.request().connection(), resp2.request().connection()); resp2.endHandler(v -> testComplete()); })); }); })); await(); }
@Test public void testClientLocalAddress() throws Exception { String expectedAddress = TestUtils.loopbackAddress(); client.close(); client = vertx.createHttpClient(createBaseClientOptions().setLocalAddress(expectedAddress)); server.requestHandler(req -> { assertEquals(expectedAddress, req.remoteAddress().host()); req.response().end(); }); startServer(); client.getNow(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })); await(); }
@Test public void testPerHostPooling() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setMaxPoolSize(1) .setKeepAlive(true) .setPipelining(false)); testPerXXXPooling((i, handler) -> client.get(DEFAULT_HTTP_PORT, "host" + i, "/somepath", handler).setHost("host:8080") .putHeader("key", "host" + i), req -> req.getHeader("key")); }
@Test public void testResetPendingPushPromise() throws Exception { server.requestHandler(req -> { req.response().push(HttpMethod.GET, "/wibble", ar -> { assertFalse(ar.succeeded()); testComplete(); }); }); startServer(); client.close(); client = vertx.createHttpClient(clientOptions.setInitialSettings(new io.vertx.core.http.Http2Settings().setMaxConcurrentStreams(0L))); HttpClientRequest req = client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onFailure(resp -> {})); req.pushHandler(pushedReq -> { pushedReq.reset(Http2Error.CANCEL.code()); }); req.end(); await(); }
@Test public void testClientDoesNotSupportAlpn() throws Exception { waitFor(2); server.requestHandler(req -> { assertEquals(HttpVersion.HTTP_1_1, req.version()); req.response().end(); complete(); }); startServer(); client.close(); client = vertx.createHttpClient(createBaseClientOptions().setProtocolVersion(HttpVersion.HTTP_1_1).setUseAlpn(false)); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(HttpVersion.HTTP_1_1, resp.version()); complete(); })).exceptionHandler(this::fail).end(); await(); }
@Test public void testHttpProxyRequest() throws Exception { startProxy(null, ProxyType.HTTP); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()))); testHttpProxyRequest2(handler -> client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", handler)); }
@Test public void testPerPeerPoolingWithProxy() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setMaxPoolSize(1) .setKeepAlive(true) .setPipelining(false).setProxyOptions(new ProxyOptions() .setType(ProxyType.HTTP) .setHost(DEFAULT_HTTP_HOST) .setPort(DEFAULT_HTTP_PORT))); testPerXXXPooling((i, handler) -> client.get(80, "host" + i, "/somepath", handler), HttpServerRequest::host); }
@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 testHttpProxyRequestOverrideClientSsl() throws Exception { startProxy(null, ProxyType.HTTP); client.close(); client = vertx.createHttpClient(new HttpClientOptions() .setSsl(true).setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP).setHost("localhost").setPort(proxy.getPort()))); testHttpProxyRequest2(handler -> client.get(new RequestOptions().setSsl(false).setHost("localhost").setPort(8080), handler)); }