@Override public void start() throws Exception { HttpClientRequest request = vertx.createHttpClient(new HttpClientOptions()).put(8080, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode()); resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1"))); }); request.setChunked(true); for (int i = 0; i < 10; i++) { request.write("client-chunk-" + i); } request.end(); } }
@Override public void start() throws Exception { HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions()).put(8080, "localhost", "/someurl", resp -> { System.out.println("Response " + resp.statusCode()); }); String filename = "upload.txt"; FileSystem fs = vertx.fileSystem(); fs.props(filename, ares -> { FileProps props = ares.result(); System.out.println("props is " + props); long size = props.size(); req.headers().set("content-length", "" + size); fs.open(filename, new OpenOptions(), ares2 -> { AsyncFile file = ares2.result(); Pump pump = Pump.pump(file, req); file.endHandler(v -> { req.end(); }); pump.start(); }); }); } }
}).listen(DEFAULT_HTTP_PORT, ar -> { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somerui", handler -> {
HttpClient client = vertx.createHttpClient(); checker.accept(expectedThread.get(), expectedContext.get()); HttpClientRequest req = client.put(8080, "localhost", "/"); req.handler(resp -> { executeInVanillaThread(() -> {
}); startServer(); HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTPS_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { resp.endHandler(v -> { testComplete();
awaitLatch(latch); HttpClient client = vertx.createHttpClient(); client.put(8080, "localhost", "/", onSuccess(resp -> { resp.netSocket().closeHandler(v -> { vertx.close(v4 -> {
@Override public void start() throws Exception { HttpClientRequest request = vertx.createHttpClient(new HttpClientOptions() .setSsl(true) .setTrustAll(true) .setVerifyHost(false) .setProxyOptions(new ProxyOptions() .setType(ProxyType.HTTP) .setHost("localhost") .setPort(8080))) .put(8282, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode()); resp.bodyHandler(body -> System.out.println("Got data " + body.toString("ISO-8859-1"))); }); request.setChunked(true); for (int i = 0; i < 10; i++) { request.write("client-chunk-" + i); } request.end(); } }
startServer(); AtomicBoolean called = new AtomicBoolean(); HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", ar -> { assertEquals(expectFail, ar.failed()); if (ar.succeeded()) {
private void testFollowRedirectWithBody(Function<Buffer, Buffer> translator) throws Exception { Buffer expected = TestUtils.randomBuffer(2048); AtomicBoolean redirected = new AtomicBoolean(); server.requestHandler(req -> { if (redirected.compareAndSet(false, true)) { assertEquals(HttpMethod.PUT, req.method()); req.bodyHandler(body -> { assertEquals(body, expected); String scheme = createBaseServerOptions().isSsl() ? "https" : "http"; req.response().setStatusCode(303).putHeader(HttpHeaders.LOCATION, scheme + "://localhost:8080/whatever").end(); }); } else { assertEquals(HttpMethod.GET, req.method()); req.response().end(); } }); startServer(); client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })).setFollowRedirects(true).end(translator.apply(expected)); await(); }
@Test public void testChunkedClientRequest() { server.requestHandler(req -> { HttpServerResponse resp = req.response(); assertEquals("chunked", req.getHeader("transfer-encoding")); req.bodyHandler(body -> { assertEquals("the-chunk", body.toString()); req.response().end(); }); }).listen(onSuccess(server -> { HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/", res -> { testComplete(); }); req.setChunked(true); req.write("the-chunk"); vertx.setTimer(1, id -> { req.end(); }); })); await(); }
HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete();
@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(); }
@Test public void testPausedHttpServerRequestDuringLastChunkEndsTheRequest() throws Exception { server.requestHandler(req -> { req.handler(buff -> { assertEquals("small", buff.toString()); req.pause(); }); req.endHandler(v -> { req.response().end(); }); }); startServer(); client.close(); client = vertx.createHttpClient(createBaseClientOptions().setMaxPoolSize(1)); client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/someuri", resp -> { complete(); }).end("small"); await(); }
@Test public void testFollowRedirectSendHeadThenBody() throws Exception { Buffer expected = Buffer.buffer(TestUtils.randomAlphaString(2048)); AtomicBoolean redirected = new AtomicBoolean(); server.requestHandler(req -> { if (redirected.compareAndSet(false, true)) { assertEquals(HttpMethod.PUT, req.method()); req.bodyHandler(body -> { assertEquals(body, expected); req.response().setStatusCode(303).putHeader(HttpHeaders.LOCATION, "/whatever").end(); }); } else { assertEquals(HttpMethod.GET, req.method()); req.response().end(); } }); startServer(); HttpClientRequest req = client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", onSuccess(resp -> { assertEquals(200, resp.statusCode()); testComplete(); })).setFollowRedirects(true); req.putHeader("Content-Length", "" + expected.length()); req.exceptionHandler(this::fail); req.sendHead(v -> { req.end(expected); }); await(); }
@Test public void testPausedHttpServerRequestUnpauseTheConnectionAtRequestEnd() throws Exception { int numRequests = 20; waitFor(numRequests); server.requestHandler(req -> { req.handler(buff -> { assertEquals("small", buff.toString()); req.pause(); }); req.endHandler(v -> { req.response().end(); }); }); startServer(); client.close(); client = vertx.createHttpClient(createBaseClientOptions().setMaxPoolSize(1)); for (int i = 0; i < numRequests; i++) { client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/someuri", resp -> { complete(); }).end("small"); } }
@Test public void testEndServerRequestResumeTheConnection() throws Exception { server.requestHandler(req -> { req.response().end(); req.endHandler(v -> { req.pause(); }); }); startServer(); client.close(); waitFor(2); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setMaxPoolSize(1)); client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); complete(); })).end("1"); client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, onSuccess(resp -> { assertEquals(200, resp.statusCode()); complete(); })).end("2"); await(); }
assertTrue(Context.isOnWorkerThread()); HttpClient client = vertx.createHttpClient(); client.put(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/blah", onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertTrue(Vertx.currentContext().isWorkerContext());
private void testFollowRedirectWithBody(Function<Buffer, Buffer> translator) throws Exception { Buffer expected = TestUtils.randomBuffer(2048); AtomicBoolean redirected = new AtomicBoolean(); server.requestHandler(req -> { if (redirected.compareAndSet(false, true)) { assertEquals(HttpMethod.PUT, req.method()); req.bodyHandler(body -> { assertEquals(body, expected); String scheme = createBaseServerOptions().isSsl() ? "https" : "http"; req.response().setStatusCode(303).putHeader(HttpHeaders.LOCATION, scheme + "://localhost:8080/whatever").end(); }); } else { assertEquals(HttpMethod.GET, req.method()); req.response().end(); } }); startServer(); client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath", resp -> { assertEquals(200, resp.statusCode()); testComplete(); }).setFollowRedirects(true).end(translator.apply(expected)); await(); }
@Test public void testEndServerRequestResumeTheConnection() throws Exception { server.requestHandler(req -> { req.response().end(); req.endHandler(v -> { req.pause(); }); }); startServer(); client.close(); waitFor(2); client = vertx.createHttpClient(new HttpClientOptions().setKeepAlive(true).setMaxPoolSize(1)); client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { assertEquals(200, resp.statusCode()); complete(); }).end("1"); client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, resp -> { assertEquals(200, resp.statusCode()); complete(); }).end("2"); await(); }
@Test public void testPausedHttpServerRequestDuringLastChunkEndsTheRequest() throws Exception { server.requestHandler(req -> { req.handler(buff -> { assertEquals("small", buff.toString()); req.pause(); }); req.endHandler(v -> { req.response().end(); }); }); startServer(); client.close(); client = vertx.createHttpClient(createBaseClientOptions().setMaxPoolSize(1)); client.put(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/someuri", resp -> { complete(); }).end("small"); await(); }