@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)); }
@Test public void transformedBodyIsNewCopy() { HttpResponse request = response() .body("Original body", UTF_8) .build(); HttpResponse newRequest = response() .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 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\" }")); }
@Test public void allowsModificationOfBodyBasedOnExistingBody() { HttpResponse response = HttpResponse.response() .body("foobar", UTF_8) .build(); HttpResponse newResponse = response.newBuilder() .body(response.bodyAs(UTF_8) + "x", UTF_8) .build(); assertThat(newResponse.bodyAs(UTF_8), is("foobarx")); }
@Test public void responseBodyIsImmutable() { HttpResponse response = response(OK) .body("Original body", UTF_8) .build(); response.body()[0] = 'A'; assertThat(response.bodyAs(UTF_8), is("Original body")); }
public static MetricsSnapshot downloadFrom(String host, int port) throws IOException { HttpClient client = new StyxHttpClient.Builder().build(); HttpResponse response = await(client.sendRequest(get(format("http://%s:%d/admin/metrics", host, port)).build())); return new MetricsSnapshot(decodeToMap(response.bodyAs(UTF_8))); }
@Test public void overridesContent() { HttpResponse response = HttpResponse.response() .body("Response content.", UTF_8) .body(" ", UTF_8) .body("Extra content", UTF_8) .build(); assertThat(response.bodyAs(UTF_8), is("Extra content")); }
@Test public void canRemoveResponseBody() { HttpResponse response = response(NO_CONTENT) .body("shouldn't be here", UTF_8) .build(); HttpResponse shouldClearBody = response.newBuilder() .body("", UTF_8) .build(); assertThat(shouldClearBody.bodyAs(UTF_8), is("")); }
@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 allowsModificationOfHeadersBasedOnBody() { HttpResponse response = HttpResponse.response() .body("foobar", UTF_8) .build(); assertThat(response.header("some-header"), isAbsent()); HttpResponse newResponse = response.newBuilder() .header("some-header", response.body().length) .build(); assertThat(newResponse.header("some-header"), isValue("6")); assertThat(newResponse.bodyAs(UTF_8), is("foobar")); }
@Test public void responseBodyCannotBeChangedViaStreamingMessage() { HttpResponse original = response(OK) .body("original", UTF_8) .build(); Flux.from(original.stream() .body() .map(buf -> { buf.delegate().array()[0] = 'A'; return buf; })) .subscribe(); assertThat(original.bodyAs(UTF_8), is("original")); }
@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); }