@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")); }
@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])); }
@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")); }
/** * Creates a new {@link Builder} object from an existing {@link LiveHttpResponse} object. * Similar to {@link this.newBuilder} method. * * @param response a full HTTP response instance */ public Builder(HttpResponse response) { this.status = response.status(); this.version = response.version(); this.headers = response.headers().newBuilder(); this.body = response.body(); }
@Test public void contentFromByteArraySetsContentLengthIfRequired() { HttpResponse response1 = HttpResponse.response() .body("Response content.".getBytes(UTF_16), true) .build(); assertThat(response1.body(), is("Response content.".getBytes(UTF_16))); assertThat(response1.header("Content-Length"), is(Optional.of("36"))); HttpResponse response2 = HttpResponse.response() .body("Response content.".getBytes(UTF_8), false) .build(); assertThat(response2.body(), is("Response content.".getBytes(UTF_8))); assertThat(response2.header("Content-Length"), is(Optional.empty())); }
@Test public void encodesBodyWithGivenCharset() { HttpResponse response = HttpResponse.response() .body("Response content.", UTF_16, true) .build(); assertThat(response.body().length, is(36)); }
@Override public void check(Origin origin, OriginHealthCheckFunction.Callback responseCallback) { HttpRequest request = newHealthCheckRequestFor(origin); client.sendRequest(request).subscribe(response -> { if (response.status().equals(OK)) { responseCallback.originStateResponse(HEALTHY); } else { meterCache.get(origin).mark(); responseCallback.originStateResponse(UNHEALTHY); } response.body().content().subscribe(RELEASE_BUFFER); }, error -> { meterCache.get(origin).mark(); responseCallback.originStateResponse(UNHEALTHY); }); }
@Test public void createsAResponseWithDefaultValues() { HttpResponse response = HttpResponse.response().build(); assertThat(response.version(), is(HTTP_1_1)); assertThat(response.cookies(), is(emptyIterable())); assertThat(response.headers(), is(emptyIterable())); assertThat(response.body().length, is(0)); }
@Test public void createsResponseWithMinimalInformation() { HttpResponse response = HttpResponse.response() .status(BAD_GATEWAY) .version(HTTP_1_1) .build(); assertThat(response.status(), is(BAD_GATEWAY)); assertThat(response.version(), is(HTTP_1_1)); assertThat(response.cookies(), is(emptyIterable())); assertThat(response.headers(), is(emptyIterable())); assertThat(response.body().length, is(0)); }
@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"))); }