public static HttpResponse waitForResponse(Observable<LiveHttpResponse> responseObs) { return responseObs .flatMap(response -> toObservable(response.aggregate(120*1024))) .toBlocking() .single(); }
@Override protected void describeMismatchSafely(T item, Description mismatchDescription) { mismatchDescription.appendText("content was '" + Mono.from(item.aggregate(0x100000)).block().bodyAs(UTF_8) + "'"); }
@Override public boolean matchesSafely(T actual) { return matcher.matches(Mono.from(actual.aggregate(0x100000)).block().bodyAs(UTF_8)); }
public static HttpResponse waitForResponse(Eventual<LiveHttpResponse> responseObs) { return Mono.from(responseObs.flatMap(response -> response.aggregate(120*1024))).block(); }
@Test(dataProvider = "emptyBodyResponses") public void encodesToFullHttpResponseWithEmptyBody(LiveHttpResponse response) throws Exception { HttpResponse full = Mono.from(response.aggregate(0x1000)).block(); assertThat(full.body(), is(new byte[0])); }
/** * Sends a request as {@link HttpRequest} object. * * @param request a {@link HttpRequest} object to be sent to remote origin. * @return a {@link CompletableFuture} of response */ public CompletableFuture<HttpResponse> send(HttpRequest request) { return sendRequestInternal(connectionFactory, request.stream(), this.transactionParameters) .flatMap(response -> Mono.from(response.aggregate(this.transactionParameters.maxResponseSize()))) .toFuture(); }
/** * Sends a request as {@link HttpRequest} object. * * @param request a {@link HttpRequest} object to be sent to remote origin. * @return a {@link CompletableFuture} of response */ @Override public CompletableFuture<HttpResponse> send(HttpRequest request) { return StyxHttpClient.sendRequestInternal(connectionFactory, request.stream(), transactionParameters) .flatMap(response -> Mono.from(response.aggregate(transactionParameters.maxResponseSize()))) .toFuture(); } }
@Test public void exposesNameAndStatusViaAdminInterface() throws ExecutionException, InterruptedException { DerivedStyxService service = new DerivedStyxService("derived-service", new CompletableFuture<>()); HttpResponse response = Mono.from(service.adminInterfaceHandlers().get("status").handle(get, MOCK_CONTEXT) .flatMap(r -> r.aggregate(1024))).block(); assertThat(response.bodyAs(UTF_8), is("{ name: \"derived-service\" status: \"CREATED\" }")); }
@Override public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) { return chain.proceed(request) .flatMap(response -> response.aggregate(this.maxContentLength)) .flatMap(fullResponse -> Eventual.from(asyncEvent(this.delayMillis)) .map(x -> fullResponse)) .map(HttpResponse::stream); }
@Test public void respondsWithStaticBody() { StaticBodyHttpHandler handler = new StaticBodyHttpHandler(PLAIN_TEXT_UTF_8, "foo", UTF_8); LiveHttpResponse response = Mono.from(handler.handle(get("/").build(), HttpInterceptorContext.create())).block(); HttpResponse fullResponse = Mono.from(response.aggregate(1024)).block(); assertThat(fullResponse.status(), is(OK)); assertThat(fullResponse.contentType(), isValue(PLAIN_TEXT_UTF_8.toString())); assertThat(fullResponse.contentLength(), isValue(length("foo"))); assertThat(fullResponse.bodyAs(UTF_8), is("foo")); }
@Test public void transformsBody() throws ExecutionException, InterruptedException { Buffer buffer = new Buffer("I'm going to get removed.", UTF_8); LiveHttpResponse response = response(NO_CONTENT) .body(new ByteStream(Flux.just(buffer))) .build(); HttpResponse fullResponse = Mono.from(response.newBuilder() .body(ByteStream::drop) .build() .aggregate(1000)).block(); assertThat(fullResponse.body().length, is(0)); assertThat(buffer.delegate().refCnt(), is(0)); }
@Test public void encodesToFullHttpResponse() { LiveHttpResponse response = response(CREATED) .version(HTTP_1_0) .header("HeaderName", "HeaderValue") .cookies(responseCookie("CookieName", "CookieValue").build()) .body(new ByteStream(Flux.just("foo", "bar").map(it -> new Buffer(copiedBuffer(it, UTF_8))))) .build(); HttpResponse full = Mono.from(response.aggregate(0x1000)).block(); assertThat(full.status(), is(CREATED)); assertThat(full.version(), is(HTTP_1_0)); assertThat(full.headers(), containsInAnyOrder(header("HeaderName", "HeaderValue"), header("Set-Cookie", "CookieName=CookieValue"))); assertThat(full.cookies(), contains(responseCookie("CookieName", "CookieValue").build())); assertThat(full.body(), is(bytes("foobar"))); }
@Test public void toFullResponseReleasesOriginalRefCountedBuffers() throws ExecutionException, InterruptedException { Buffer content = new Buffer(Unpooled.copiedBuffer("original", UTF_8)); LiveHttpResponse original = LiveHttpResponse.response(OK) .body(new ByteStream(Flux.just(content))) .build(); StepVerifier.create(original.aggregate(100)) .expectNextCount(1) .then(() -> assertThat(content.delegate().refCnt(), is(0))) .verifyComplete(); }
@Test public void convertsToStreamingHttpResponse() throws Exception { HttpResponse response = response(CREATED) .version(HTTP_1_1) .header("HeaderName", "HeaderValue") .cookies(responseCookie("CookieName", "CookieValue").build()) .body("message content", UTF_8) .build(); LiveHttpResponse streaming = response.stream(); assertThat(streaming.version(), is(HTTP_1_1)); assertThat(streaming.status(), is(CREATED)); assertThat(streaming.headers(), containsInAnyOrder( header("Content-Length", "15"), header("HeaderName", "HeaderValue"), header("Set-Cookie", "CookieName=CookieValue") )); assertThat(streaming.cookies(), contains(responseCookie("CookieName", "CookieValue").build())); StepVerifier.create(streaming.aggregate(0x100000).map(it -> it.bodyAs(UTF_8))) .expectNext("message content") .verifyComplete(); }
@Test public void transformerReplacesBody() { Buffer buf1 = new Buffer("chunk 1, ", UTF_8); Buffer buf2 = new Buffer("chunk 2.", UTF_8); LiveHttpResponse response1 = response(NO_CONTENT) .body(new ByteStream(Flux.just(buf1, buf2))) .build() .newBuilder() .body(body -> body.replaceWith(ByteStream.from("replacement", UTF_8))) .build(); HttpResponse response2 = Mono.from(response1.aggregate(100)).block(); assertEquals(response2.bodyAs(UTF_8), "replacement"); assertEquals(buf1.delegate().refCnt(), 0); assertEquals(buf2.delegate().refCnt(), 0); }
@Override public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) { String header = xHcomPluginsHeader(request); final String configPath = environment.pluginConfig(String.class); String pluginsList = environment.configuration().get("plugins.active").get(); LiveHttpRequest newRequest = request.newBuilder() .header(X_HCOM_PLUGINS_HEADER, header) .header(X_HCOM_PLUGINS_CONFIGURATION_PATH, configPath) .header(X_HCOM_PLUGINS_LIST, pluginsList) .header("X-Hcom-Styx-Started", styxStarted) .header("X-Hcom-Styx-Stopped", styxStopped) .build(); Function<ByteBuf, String> byteBufStringFunction = byteBuf -> byteBuf.toString(Charsets.UTF_8); return chain.proceed(newRequest) .flatMap(response -> response.aggregate(1 * 1024 * 1024)) .map(response -> response.newBuilder() .header(X_HCOM_PLUGINS_HEADER, header) .header(X_HCOM_PLUGINS_CONFIGURATION_PATH, configPath) .header(X_HCOM_PLUGINS_LIST, pluginsList) .header("X-Hcom-Styx-Started", styxStarted) .header("X-Hcom-Styx-Stopped", styxStopped) .build()) .map(HttpResponse::stream); }