@Test public void testTimeout() throws Exception { AtomicInteger count = new AtomicInteger(); server.requestHandler(req -> count.incrementAndGet()); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get.timeout(50).send(onFailure(err -> { assertTrue(err instanceof TimeoutException); testComplete(); })); await(); }
private void testExpectation(boolean shouldFail, Consumer<HttpRequest<?>> modifier, Consumer<HttpServerResponse> bilto, Consumer<AsyncResult<?>> resultTest) throws Exception { server.requestHandler(request -> bilto.accept(request.response())); startServer(); HttpRequest<Buffer> request = client .get("/test"); modifier.accept(request); request.send(ar -> { if (ar.succeeded()) { assertFalse("Expected response success", shouldFail); } else { assertTrue("Expected response failure", shouldFail); } if (resultTest != null) resultTest.accept(ar); testComplete(); }); await(); } }
@Test public void testResponseUnknownContentTypeBodyAsJsonObject() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get.send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.bodyAsJsonObject()); testComplete(); })); await(); }
private void testTLS(WebClientOptions clientOptions, HttpServerOptions serverOptions, Function<WebClient, HttpRequest<Buffer>> requestProvider, Consumer<HttpServerRequest> serverAssertions) throws Exception { WebClient sslClient = WebClient.create(vertx, clientOptions); HttpServer sslServer = vertx.createHttpServer(serverOptions); sslServer.requestHandler(req -> { assertEquals(serverOptions.isSsl(), req.isSSL()); if (serverAssertions != null) { serverAssertions.accept(req); } req.response().end(); }); try { startServer(sslServer); HttpRequest<Buffer> builder = requestProvider.apply(sslClient); builder.send(onSuccess(resp -> testComplete())); await(); } finally { sslClient.close(); sslServer.close(); } }
@Test public void testResponseUnknownContentTypeBodyAsJsonMapped() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get.send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.bodyAsJson(WineAndCheese.class)); testComplete(); })); await(); }
@Test public void testResponseBodyUnmarshallingError() throws Exception { server.requestHandler(req -> req.response().end("not-json-object")); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonObject()) .send(onFailure(err -> { assertTrue(err instanceof DecodeException); testComplete(); })); await(); }
@Test public void testResponseBodyAsAsJsonObject() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonObject()) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.body()); testComplete(); })); await(); }
@Test public void testVirtualHost() throws Exception { server.requestHandler(req -> { assertEquals("another-host:8080", req.host()); req.response().end(); }); startServer(); HttpRequest<Buffer> req = client.get("/test").virtualHost("another-host"); req.send(onSuccess(resp -> testComplete())); await(); }
@Test public void testResponseBodyAsAsJsonMapped() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.json(WineAndCheese.class)) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body()); testComplete(); })); await(); }
@Test public void testResponseUnknownContentTypeBodyAsJsonArray() throws Exception { JsonArray expected = new JsonArray().add("cheese").add("wine"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get.send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.bodyAsJsonArray()); testComplete(); })); await(); }
@Test public void testResponseBodyAsBuffer() throws Exception { Buffer expected = TestUtils.randomBuffer(2000); server.requestHandler(req -> req.response().end(expected)); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get.send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.body()); testComplete(); })); await(); }
private <R> void testResponseMissingBody(BodyCodec<R> codec) throws Exception { server.requestHandler(req -> req.response().setStatusCode(403).end()); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(codec) .send(onSuccess(resp -> { assertEquals(403, resp.statusCode()); assertNull(resp.body()); testComplete(); })); await(); }
@Test public void testHttpResponseError() throws Exception { server.requestHandler(req -> req.response().setChunked(true).write(Buffer.buffer("some-data")).close()); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonObject()) .send(onFailure(err -> { assertTrue(err instanceof VertxException); testComplete(); })); await(); }
@Test public void testResponseBodyDiscarded() throws Exception { server.requestHandler(req -> req.response().end(TestUtils.randomAlphaString(1024))); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.none()) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(null, resp.body()); testComplete(); })); await(); }
get .as(BodyCodec.pipe(stream)) .send(onFailure(err -> testComplete())); assertWaitUntil(() -> received.get() == 2048); fail.complete(null);
testComplete(); })); await();
@Test public void testResponseBodyAsAsJsonArray() throws Exception { JsonArray expected = new JsonArray().add("cheese").add("wine"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonArray()) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.body()); testComplete(); })); await(); }
@Test public void testResponseBodyAsAsJsonArrayMapped() throws Exception { JsonArray expected = new JsonArray().add("cheese").add("wine"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.json(List.class)) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected.getList(), resp.body()); testComplete(); })); await(); }
@Test public void testHttpProxyFtpRequest() throws Exception { startProxy(null, ProxyType.HTTP); proxy.setForceUri("http://" + DEFAULT_HTTP_HOST + ":" + DEFAULT_HTTP_PORT); server.requestHandler(req -> req.response().setStatusCode(200).end()); startServer(); WebClientOptions options = new WebClientOptions(); options.setProxyOptions(new ProxyOptions().setPort(proxy.getPort())); WebClient client = WebClient.create(vertx, options); client .getAbs("ftp://ftp.gnu.org/gnu/") .send(ar -> { if (ar.succeeded()) { // Obtain response HttpResponse<Buffer> response = ar.result(); assertEquals(200, response.statusCode()); assertEquals("ftp://ftp.gnu.org/gnu/", proxy.getLastUri()); testComplete(); } else { fail(ar.cause()); } }); await(); }
.send(onFailure(err -> { assertSame(cause, err); testComplete(); })); await();