@Test public void testPutHeaderReplacesPreviousHeaders() throws Exception { server.requestHandler(req -> req.response() .putHeader("Location", "http://example1.org") .putHeader("location", "http://example2.org") .end()); server.listen(onSuccess(server -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(singletonList("http://example2.org"), resp.headers().getAll("LocatioN")); testComplete(); })).end(); })); await(); }
@Test public void testServerResponseWriteBufferFromOtherThread() throws Exception { server.requestHandler(req -> { runAsync(() -> { req.response().write("hello ").end("world"); }); }).listen(onSuccess(v -> { client.get(8080, "localhost", "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); resp.bodyHandler(buff -> { assertEquals(Buffer.buffer("hello world"), buff); testComplete(); }); })).exceptionHandler(this::fail).end(); })); await(); }
@Test public void testPutHeadersOnRequest() { server.requestHandler(req -> { assertEquals("bar", req.headers().get("foo")); assertEquals("bar", req.getHeader("foo")); req.response().end(); }); server.listen(onSuccess(server -> { client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })).putHeader("foo", "bar").end(); })); await(); }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(new HttpClientOptions()); vertx.createHttpServer().requestHandler(req -> { System.out.println("Proxying request: " + req.uri()); HttpClientRequest c_req = client.request(req.method(), 8282, "localhost", req.uri(), c_res -> { System.out.println("Proxying response: " + c_res.statusCode()); req.response().setChunked(true); req.response().setStatusCode(c_res.statusCode()); req.response().headers().setAll(c_res.headers()); c_res.handler(data -> { System.out.println("Proxying response body: " + data.toString("ISO-8859-1")); req.response().write(data); }); c_res.endHandler((v) -> req.response().end()); }); c_req.setChunked(true); c_req.headers().setAll(req.headers()); req.handler(data -> { System.out.println("Proxying request body " + data.toString("ISO-8859-1")); c_req.write(data); }); req.endHandler((v) -> c_req.end()); }).listen(8080); } }
@Test public void testSetChunkedToFalse() throws Exception { server.requestHandler(req -> req.response().setChunked(false).end()); startServer(); client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", resp -> { testComplete(); }).setChunked(false).end(); await(); }
@Test public void testFormUploadAttributes() throws Exception { AtomicInteger attributeCount = new AtomicInteger(); server.requestHandler(req -> { if (req.method() == HttpMethod.POST) { assertEquals(req.path(), "/form"); req.response().setChunked(true); req.setExpectMultipart(true); req.uploadHandler(upload -> upload.handler(buffer -> { assertEquals("jvm", attrs.get("runson")); assertEquals("jvm", req.getFormAttribute("runson")); req.response().end(); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/form", onSuccess(resp -> { assertEquals(200, resp.statusCode()); resp.bodyHandler(body -> { assertEquals(0, body.length()); }); req.headers().set("content-length", String.valueOf(buffer.length())); req.headers().set("content-type", "application/x-www-form-urlencoded"); req.write(buffer).end(); } catch (UnsupportedEncodingException e) { fail(e.getMessage());
@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 testPipeliningOrder() throws Exception { client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setPipelining(true).setMaxPoolSize(1)); int requests = 100; req.response().headers().set("count", String.valueOf(theCount)); req.response().write(buff); server.listen(onSuccess(s -> { vertx.setTimer(500, id -> { for (int count = 0; count < requests; count++) { int theCount = count; HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(theCount, Integer.parseInt(resp.headers().get("count"))); resp.bodyHandler(buff -> { assertEquals("This is content " + theCount, buff.toString()); latch.countDown(); }); })); req.setChunked(true); req.headers().set("count", String.valueOf(count)); req.write("This is content " + count); req.end();
@Test public void test100ContinueHandledAutomatically() throws Exception { Buffer toSend = TestUtils.randomBuffer(1000); server.requestHandler(req -> { req.bodyHandler(data -> { assertEquals(toSend, data); req.response().end(); }); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.PUT, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.endHandler(v -> testComplete()); })); req.headers().set("Expect", "100-continue"); req.setChunked(true); req.continueHandler(v -> { req.write(toSend); req.end(); }); req.sendHead(); })); await(); }
int numGets = 100; int maxPoolSize = 10; client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(keepAlive).setPipelining(pipelining).setMaxPoolSize(maxPoolSize)); server.requestHandler(req -> { String cnt = req.headers().get("count"); req.response().headers().set("count", cnt); req.response().end(); }); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, path, onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(theCount, Integer.parseInt(resp.headers().get("count"))); if (cnt.incrementAndGet() == numGets) { testComplete(); req.exceptionHandler(t -> { fail("Should not throw exception: " + t.getMessage()); }); req.headers().set("count", String.valueOf(i)); req.end();
@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 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(); }
private void testRequestHeaders(boolean individually) { MultiMap headers = getHeaders(10); server.requestHandler(req -> { assertTrue(headers.size() < req.headers().size()); for (Map.Entry<String, String> entry : headers) { assertEquals(entry.getValue(), req.headers().get(entry.getKey())); assertEquals(entry.getValue(), req.getHeader(entry.getKey())); } req.response().end(); }); server.listen(onSuccess(server -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete()); if (individually) { for (Map.Entry<String, String> header : headers) { req.headers().add(header.getKey(), header.getValue()); } } else { req.headers().setAll(headers); } req.end(); })); await(); }
private void testRequestBodyWrite(boolean chunked) { Buffer body = Buffer.buffer(); server.requestHandler(req -> { req.bodyHandler(buffer -> { assertEquals(body, buffer); req.response().end(); }); }); server.listen(onSuccess(server -> { HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> testComplete()); int numWrites = 10; int chunkSize = 100; if (chunked) { req.setChunked(true); } else { req.headers().set("Content-Length", String.valueOf(numWrites * chunkSize)); } for (int i = 0; i < numWrites; i++) { Buffer b = TestUtils.randomBuffer(chunkSize); body.appendBuffer(b); req.write(b); } req.end(); })); await(); }
@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 testFallbackOnHttp1() throws Exception { server.close(); server = vertx.createHttpServer(serverOptions.setUseAlpn(false)); server.requestHandler(req -> { assertEquals(HttpVersion.HTTP_1_1, req.version()); req.response().end(); }); startServer(); client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> { testComplete(); }).exceptionHandler(this::fail).end(); await(); }
@Test public void testRequestTimeoutCanceledWhenRequestEndsNormally() { server.requestHandler(req -> req.response().end()); server.listen(onSuccess(s -> { AtomicReference<Throwable> exception = new AtomicReference<>(); // There is no server running, should fail to connect HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler()); req.exceptionHandler(exception::set); req.setTimeout(500); req.end(); vertx.setTimer(1000, id -> { assertNull("Did not expect any exception", exception.get()); testComplete(); }); })); await(); }
@Test public void testRequestsTimeoutInQueue() { server.requestHandler(req -> { vertx.setTimer(1000, id -> { HttpServerResponse resp = req.response(); if (!resp.closed()) { resp.end(); client.close(); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(false).setMaxPoolSize(1)); server.listen(onSuccess(s -> { HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onFailure(t -> { assertTrue(t instanceof TimeoutException); })); req.setTimeout(500); req.end(); HttpClientRequest req = client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })); req.exceptionHandler(t -> fail("Should not throw exception")); req.setTimeout(3000); req.end(); }));
@Test public void testClientRequestWriteFromOtherThread() throws Exception { CountDownLatch latch1 = new CountDownLatch(1); CountDownLatch latch2 = new CountDownLatch(1); server.requestHandler(req -> { latch2.countDown(); req.endHandler(v -> { req.response().end(); }); }).listen(onSuccess(v -> { latch1.countDown(); })); awaitLatch(latch1); HttpClientRequest req = client.get(8080, "localhost", "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })).setChunked(true).sendHead(); awaitLatch(latch2); // The next write won't be buffered req.write("hello ").end("world"); await(); }