@Override public String getBodyAsString() { return styxRequest.bodyAs(UTF_8); }
private static Eventual<PluginEnabledState> requestedNewState(LiveHttpRequest request) { return request.aggregate(MAX_CONTENT_SIZE) .map(fullRequest -> fullRequest.bodyAs(UTF_8)) .map(PluginToggleHandler::parseToBoolean) .map(PluginEnabledState::fromBoolean); }
@Override protected LiveHttpResponse doHandle(LiveHttpRequest request) { HttpRequest fullRequest = Mono.from(request.aggregate(0x100000)).block(); String responseBody = format("Response From %s - %s, received content digest: %s", origin.hostAndPortString(), randomUUID(), fullRequest.bodyAs(UTF_8).hashCode()); return HttpResponse.response(OK) .header(CONTENT_TYPE, HTML_UTF_8.toString()) .header(CONTENT_LENGTH, responseBody.getBytes(UTF_8).length) .body(responseBody, UTF_8) .build() .stream(); } }
@Test public void transformedBodyIsNewCopy() { HttpRequest request = get("/foo") .body("Original body", UTF_8) .build(); HttpRequest newRequest = request.newBuilder() .body("New body", UTF_8) .build(); assertThat(request.bodyAs(UTF_8), is("Original body")); assertThat(newRequest.bodyAs(UTF_8), is("New body")); }
@Test public void requestBodyIsImmutable() { HttpRequest request = get("/foo") .body("Original body", UTF_8) .build(); request.body()[0] = 'A'; assertThat(request.bodyAs(UTF_8), is("Original body")); }
@Test public void toFullRequestReleasesOriginalReferenceCountedBuffers() throws ExecutionException, InterruptedException { Buffer content = new Buffer("original", UTF_8); LiveHttpRequest original = LiveHttpRequest.get("/foo") .body(new ByteStream(Flux.just(content))) .build(); HttpRequest fullRequest = Mono.from(original.aggregate(100)).block(); assertThat(content.delegate().refCnt(), is(0)); assertThat(fullRequest.bodyAs(UTF_8), is("original")); }
@Test public void requestBodyCannotBeChangedViaStreamingRequest() { HttpRequest original = HttpRequest.get("/foo") .body("original", UTF_8) .build(); Flux.from(original.stream() .body() .map(buffer -> { buffer.delegate().array()[0] = 'A'; return buffer; })) .subscribe(); assertThat(original.bodyAs(UTF_8), is("original")); }
@Test public void convertsToStreamingHttpRequest() throws Exception { HttpRequest fullRequest = new HttpRequest.Builder(POST, "/foo/bar").body("foobar", UTF_8) .version(HTTP_1_1) .header("HeaderName", "HeaderValue") .cookies(requestCookie("CookieName", "CookieValue")) .build(); LiveHttpRequest streaming = fullRequest.stream(); assertThat(streaming.method(), is(HttpMethod.POST)); assertThat(streaming.url(), is(url("/foo/bar").build())); assertThat(streaming.version(), is(HTTP_1_1)); assertThat(streaming.headers(), containsInAnyOrder( header("Content-Length", "6"), header("HeaderName", "HeaderValue"), header("Cookie", "CookieName=CookieValue"))); assertThat(streaming.cookies(), contains(requestCookie("CookieName", "CookieValue"))); StepVerifier.create(streaming.aggregate(0x10000).map(it -> it.bodyAs(UTF_8))) .expectNext("foobar") .verifyComplete(); }