private DecodedResponse(HttpResponse response, T content) { this.responseBuilder = response.newBuilder().body(NO_BODY); this.content = content; }
private HttpResponse addOriginId(Id originId, HttpResponse response) { return response.newBuilder() .header(originIdHeaderName, originId) .build(); } }
private HttpResponse removeRedundantContentLengthHeader(HttpResponse response) { if (contentValidation && response.contentLength().isPresent() && response.chunked()) { return response.newBuilder() .removeHeader(CONTENT_LENGTH) .build(); } return response; }
private static HttpResponse responseWithoutBody(HttpResponse response) { return response.newBuilder() .header(CONTENT_LENGTH, 0) .removeHeader(TRANSFER_ENCODING) .removeBody() .build(); }
@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()); }
@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)); }
@Test public void shouldRemoveContentLengthFromChunkedMessages() { HttpResponse response = HttpResponse.response().header(CONTENT_LENGTH, 5).build(); HttpResponse chunkedResponse = response.newBuilder().setChunked().build(); assertThat(chunkedResponse.chunked(), is(true)); assertThat(chunkedResponse.header(CONTENT_LENGTH).isPresent(), is(false)); }
private HttpResponse addStickySessionIdentifier(HttpResponse httpResponse, Origin origin) { if (this.loadBalancer instanceof StickySessionLoadBalancingStrategy) { int maxAge = backendService.stickySessionConfig().stickySessionTimeoutSeconds(); return httpResponse.newBuilder() .addCookie(newStickySessionCookie(id, origin.id(), maxAge)) .build(); } else { return httpResponse; } }
}).map(response -> response.newBuilder().request(request).build());
@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 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 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 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 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 public void removesCookies() { HttpResponse r1 = response() .addCookies(responseCookie("x", "x1").build(), responseCookie("y", "y1").build()) .build(); HttpResponse r2 = r1.newBuilder() .removeCookies("x") .removeCookies("foo") // ensure that trying to remove a non-existent cookie does not cause Exception .build(); assertThat(r2.cookies(), contains(responseCookie("y", "y1").build())); }
@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); }