@Test public void shouldNotFailToRemoveNonExistentContentLength() { HttpResponse response = HttpResponse.response().build(); HttpResponse chunkedResponse = response.newBuilder().setChunked().build(); assertThat(chunkedResponse.chunked(), is(true)); assertThat(chunkedResponse.header(CONTENT_LENGTH).isPresent(), is(false)); }
/** * 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(); }
private HttpResponse removeRedundantContentLengthHeader(HttpResponse response) { if (contentValidation && response.contentLength().isPresent() && response.chunked()) { return response.newBuilder() .removeHeader(CONTENT_LENGTH) .build(); } return response; }
private HttpResponse.Builder restrictedMetricsResponse(MetricRequest request) { Map<String, Metric> fullMetrics = metricRegistry.getMetrics(); Map<String, Metric> restricted = filter(fullMetrics, (name, metric) -> request.matchesRoot(name)); return restricted.isEmpty() ? response(NOT_FOUND) : search(request, restricted); }
@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 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 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 newCookiesWithDuplicateNamesOverridePreviousOnes() { HttpResponse r1 = response() .cookies(responseCookie("y", "y1").build()) .build(); HttpResponse r2 = r1.newBuilder().addCookies( responseCookie("y", "y2").build()) .build(); assertThat(r2.cookies(), containsInAnyOrder(responseCookie("y", "y2").build())); }
@Test(dataProvider = "emptyBodyResponses") public void convertsToStreamingHttpResponseWithEmptyBody(HttpResponse response) throws ExecutionException, InterruptedException { LiveHttpResponse streaming = response.stream(); byte[] result = streaming.body().aggregate(1000) .get() .content(); assertThat(result.length, is(0)); }
@Test public void canRemoveAHeader() { Object headerValue = "b"; HttpResponse response = HttpResponse.response() .header("a", headerValue) .addHeader("c", headerValue) .build(); HttpResponse shouldRemoveHeader = response.newBuilder() .removeHeader("c") .build(); assertThat(shouldRemoveHeader.headers(), contains(header("a", "b"))); }
@Test public void contentFromStringSetsContentLengthIfRequired() { HttpResponse response1 = HttpResponse.response() .body("Response content.", UTF_8, true) .build(); assertThat(response1.header("Content-Length"), is(Optional.of("17"))); HttpResponse response2 = HttpResponse.response() .body("Response content.", UTF_8, false) .build(); assertThat(response2.header("Content-Length"), is(Optional.empty())); }
@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 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())); }
@Override public Eventual<LiveHttpResponse> handle(LiveHttpRequest request, HttpInterceptor.Context context) { HttpResponse.Builder responseBuilder = standardResponse.newBuilder() .headers(request.headers()) .header(STUB_ORIGIN_INFO, origin.applicationInfo()); return Eventual.of(Optional.ofNullable(responseBuilder) .map(it -> request.queryParam("status") .map(status -> it.status(httpResponseStatus(status)) .body("Returning requested status (" + status + ")", UTF_8)) .orElse(it)) .map(it -> request.queryParam("length") .map(length -> it.body(generateContent(parseInt(length)), UTF_8)) .orElse(it)) .orElse(responseBuilder) .build() .stream()); }