private RequestOptions getRequestOptions() { return new RequestOptions() .setPauseResponse(true) .addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); }
private Promise<HttpClientResponseAndBody> doRequestAndReadBody(RequestOptions options, RequestFunction requestFunction) { if (options == null) { options = new RequestOptions(); } if (options.getPauseResponse()) { throw new IllegalStateException("Cannot pause response when reading body"); } Deferred<HttpClientResponseAndBody> d = when.defer(); Handler<HttpClientRequestContext> handler = context -> { HttpClientResponse response = context.getResponse(); response.bodyHandler(body -> { if (context.shouldCloseClient()) { context.getClient().close(); context.setCloseClient(false); } context.setBody(body); d.resolve(context); }); }; return innerDoRequest(options, requestFunction, handler, d::reject, d.getPromise()); }
private Promise<HttpClientResponseAndBody> doRequestAndReadBody(RequestOptions options, RequestFunction requestFunction) { if (options == null) { options = new RequestOptions(); } if (options.getPauseResponse()) { throw new IllegalStateException("Cannot pause response when reading body"); } Deferred<HttpClientResponseAndBody> d = when.defer(); Handler<HttpClientRequestContext> handler = context -> { HttpClientResponse response = context.getResponse(); response.bodyHandler(body -> { if (context.shouldCloseClient()) { context.getClient().close(); context.setCloseClient(false); } context.setBody(body); d.resolve(context); }); }; return innerDoRequest(options, requestFunction, handler, d::reject, d.getPromise()); }
private Promise<HttpClientResponse> doRequest(RequestOptions options, RequestFunction requestFunction) { if (options == null) { options = new RequestOptions(); } Deferred<HttpClientResponse> d = when.defer(); RequestOptions finalOptions = options; Handler<HttpClientRequestContext> handler = context -> { HttpClientResponse response = context.getResponse(); if (context.shouldCloseClient()) { HttpClient client = context.getClient(); response.endHandler(aVoid -> { client.close(); context.setCloseClient(false); }); } if (finalOptions.getPauseResponse()) { response.pause(); } d.resolve(HttpClientResponseProxy.newInstance(response, context)); }; return innerDoRequest(options, requestFunction, handler, d::reject, d.getPromise()); }
private Promise<HttpClientResponse> doRequest(RequestOptions options, RequestFunction requestFunction) { if (options == null) { options = new RequestOptions(); } Deferred<HttpClientResponse> d = when.defer(); RequestOptions finalOptions = options; Handler<HttpClientRequestContext> handler = context -> { HttpClientResponse response = context.getResponse(); if (context.shouldCloseClient()) { HttpClient client = context.getClient(); response.endHandler(aVoid -> { client.close(); context.setCloseClient(false); }); } if (finalOptions.getPauseResponse()) { response.pause(); } d.resolve(HttpClientResponseProxy.newInstance(response, context)); }; return innerDoRequest(options, requestFunction, handler, d::reject, d.getPromise()); }
private void runTest(String additionalPath, Consumer<Buffer> assertMethod) throws Exception { getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH + additionalPath, new RequestOptions().setPauseResponse(true)) .then(response -> { assertEquals(200, response.statusCode()); return getWhenHttpClient().body(response); }) .then(body -> { assertMethod.accept(body); testComplete(); return null; }) .otherwise(this::onRejected); await(); }
private void runTest(String additionalPath, Consumer<Buffer> assertMethod) throws Exception { getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH + additionalPath, new RequestOptions().setPauseResponse(true)) .then(response -> { assertEquals(200, response.statusCode()); return getWhenHttpClient().body(response); }) .then(body -> { assertMethod.accept(body); testComplete(); return null; }) .otherwise(this::onRejected); await(); }
private void testPost(String path) throws Exception { RequestOptions options = new RequestOptions() .setPauseResponse(true) .setSetupHandler(request -> { request.putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON); return null; }) .setData(new JsonObject().put("name", "Andy").encode()); getWhenHttpClient().requestAbs(HttpMethod.POST, path, options) .then(response -> { assertEquals(200, response.statusCode()); return getWhenHttpClient().body(response); }) .then(body -> { JsonObject json = new JsonObject(body.toString()); assertEquals("Andy", json.getString("name")); testComplete(); return null; }) .otherwise(this::onRejected); await(); }
private void runTest(String additionalPath, Consumer<Buffer> assertMethod) throws Exception { RequestOptions options = new RequestOptions() .setPauseResponse(true) .addHeader(SwaggerCorsFilter.HEADER_ORIGIN, "http://test.org"); getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH + additionalPath, options) .then(response -> { assertEquals(200, response.statusCode()); assertEquals("*", response.getHeader(SwaggerCorsFilter.HEADER_ACCESS_CONTROL_ALLOW_ORIGIN)); return getWhenHttpClient().body(response); }) .then(body -> { assertMethod.accept(body); testComplete(); return null; }) .otherwise(this::onRejected); await(); } }
private void runTest(String path) { RequestOptions options = new RequestOptions() .setPauseResponse(true); getWhenHttpClient().requestAbs(HttpMethod.GET, path, options) .then(response -> { assertEquals(200, response.statusCode()); assertEquals("chunked", response.headers().get(HttpHeaderNames.TRANSFER_ENCODING)); return getWhenHttpClient().body(response); }) .then(body -> { String text = body.toString(); assertTrue(text.startsWith("aaaaa")); testComplete(); return null; }) .otherwise(this::onRejected); await(); }
@Test public void testGet_OK() throws Exception { getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH, new RequestOptions().setPauseResponse(true)) .then(response -> { assertEquals(200, response.statusCode()); return getWhenHttpClient().body(response); }) .then(body -> { String str = body.toString(); assertEquals(str, TestResource.RESPONSE_BODY); testComplete(); return null; }) .otherwise(this::onRejected); await(); }
@Test public void testGet_OK() throws Exception { getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH, new RequestOptions().setPauseResponse(true)) .then(response -> { assertEquals(200, response.statusCode()); return getWhenHttpClient().body(response); }) .then(body -> { String str = body.toString(); assertEquals(str, "Hello World!!! Hello!"); testComplete(); return null; }) .otherwise(this::onRejected); await(); }
@Test public void testExceptionMapper() throws Exception { RequestOptions options = new RequestOptions().setPauseResponse(true); getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH + "/exception", options) .then(response -> { assertEquals(GuiceExceptionMapper.STATUS_IM_A_TEAPOT.getStatusCode(), response.statusCode()); assertEquals(MediaType.APPLICATION_JSON, response.getHeader(HttpHeaders.CONTENT_TYPE)); return getWhenHttpClient().body(response); }) .then(body -> { JsonObject json = body.toJsonObject(); assertTrue(json.containsKey("msg")); assertTrue(json.containsKey("type")); testComplete(); return null; }) .otherwise(this::onRejected); await(); }
@Test public void testGetStream() throws Exception { RequestOptions options = new RequestOptions().setPauseResponse(true); getWhenHttpClient().requestAbs(HttpMethod.GET, BASE_PATH + "/stream", options) .then(response -> { assertEquals(200, response.statusCode()); assertNull(response.headers().get(HttpHeaderNames.TRANSFER_ENCODING)); assertEquals("36", response.headers().get(HttpHeaderNames.CONTENT_LENGTH)); return getWhenHttpClient().body(response); }) .then(body -> { String text = body.toString(); assertEquals("abcdefghijklmnopqrstuvwxyz0123456789", text); testComplete(); return null; }) .otherwise(this::onRejected); await(); } }