private LiveHttpResponse addOriginId(Id originId, LiveHttpResponse response) { return response.newBuilder() .header(originIdHeader, originId) .build(); }
private static LiveHttpResponse disableCaching(LiveHttpResponse response) { return response.newBuilder() .disableCaching() .build(); }
public static LiveHttpResponse doFinally(LiveHttpResponse response, Consumer<Optional<Throwable>> action) { return response.newBuilder() .body(it -> it.doOnEnd(action::accept)) .build(); }
private LiveHttpResponse removeRedundantContentLengthHeader(LiveHttpResponse response) { if (contentValidation && response.contentLength().isPresent() && response.chunked()) { return response.newBuilder() .removeHeader(CONTENT_LENGTH) .build(); } return response; }
private static LiveHttpResponse responseWithoutBody(LiveHttpResponse response) { return response.newBuilder() .header(CONTENT_LENGTH, 0) .removeHeader(TRANSFER_ENCODING) .removeBody() .build(); }
public static LiveHttpResponse doOnComplete(LiveHttpResponse response, Runnable action) { return response.newBuilder() .body(it -> it.doOnEnd(ifSuccessful(action))) .build(); }
public static LiveHttpResponse doOnError(LiveHttpResponse response, Consumer<Throwable> action) { return response.newBuilder() .body(it -> it.doOnEnd(ifError(action))) .build(); }
private LiveHttpResponse addStickySessionIdentifier(LiveHttpResponse httpResponse, Origin origin) { if (this.loadBalancer instanceof StickySessionLoadBalancingStrategy) { int maxAge = stickySessionConfig.stickySessionTimeoutSeconds(); return httpResponse.newBuilder() .addCookies(newStickySessionCookie(id, origin.id(), maxAge)) .build(); } else { return httpResponse; } }
@Test public void shouldNotFailToRemoveNonExistentContentLength() { LiveHttpResponse response = response().build(); LiveHttpResponse chunkedResponse = response.newBuilder().setChunked().build(); assertThat(chunkedResponse.chunked(), is(true)); assertThat(chunkedResponse.header(CONTENT_LENGTH).isPresent(), is(false)); }
@Test public void shouldRemoveContentLengthFromChunkedMessages() { LiveHttpResponse response = response().header(CONTENT_LENGTH, 5).build(); LiveHttpResponse chunkedResponse = response.newBuilder().setChunked().build(); assertThat(chunkedResponse.chunked(), is(true)); assertThat(chunkedResponse.header(CONTENT_LENGTH).isPresent(), is(false)); }
@Test public void transformerAddsHeaders() { LiveHttpResponse response = response().build() .newBuilder() .addHeader("X-Styx-ID", "y") .build(); assertEquals(response.header("X-Styx-ID"), Optional.of("y")); }
@Test public void transformerRemovesCookiesWithList() { LiveHttpResponse response = response() .addCookies(ImmutableList.of(responseCookie("x", "y").build())) .build() .newBuilder() .removeCookies(ImmutableList.of("x")) .build(); assertEquals(response.cookie("x"), Optional.empty()); }
@Test public void transformerRemovesCookies() { LiveHttpResponse response = response() .addCookies(ImmutableList.of(responseCookie("x", "y").build())) .build() .newBuilder() .removeCookies("x") .build(); assertEquals(response.cookie("x"), Optional.empty()); }
@Test public void transformsWithCookieList() { LiveHttpResponse response = response().build() .newBuilder() .cookies(ImmutableList.of(responseCookie("x", "y").build())) .build(); assertEquals(response.cookie("x"), Optional.of(responseCookie("x", "y").build())); }
@Test public void transformerAddsCookiesList() { LiveHttpResponse response = response().build() .newBuilder() .addCookies(ImmutableList.of(responseCookie("x", "y").build())) .build(); assertEquals(response.cookie("x"), Optional.of(responseCookie("x", "y").build())); }
@Test public void canRemoveAHeader() { Object headerValue = "b"; LiveHttpResponse response = response() .header("a", headerValue) .addHeader("c", headerValue) .build(); LiveHttpResponse shouldRemoveHeader = response.newBuilder() .removeHeader("c") .build(); assertThat(shouldRemoveHeader.headers(), contains(header("a", "b"))); }
@Test public void transformerSetsHeaders() { LiveHttpResponse response = response().build() .newBuilder() .headers(new HttpHeaders.Builder().add("X-Styx-ID", "z").build()) .build(); assertEquals(response.header("X-Styx-ID"), Optional.of("z")); }
@Override public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) { LiveHttpRequest newRequest = request.newBuilder() .header(VIA, viaHeader(request)) .build(); return chain.proceed(newRequest) .map(response -> response.newBuilder() .header(VIA, viaHeader(response)) .build()); }
@Test public void transformerAddsCookies() { LiveHttpResponse response = response().build() .newBuilder() .addCookies(responseCookie("x", "y").build()) .build(); assertEquals(response.cookie("x"), Optional.of(responseCookie("x", "y").build())); }
@Test public void newCookiesWithDuplicateNamesOverridePreviousOnes() { LiveHttpResponse r1 = response() .cookies(responseCookie("y", "y1").build()) .build(); LiveHttpResponse r2 = r1.newBuilder().addCookies( responseCookie("y", "y2").build()) .build(); assertThat(r2.cookies(), containsInAnyOrder(responseCookie("y", "y2").build())); }