/** * Creates a new {@link Builder} object from an existing {@link LiveHttpResponse} object. * Similar to {@link this.newBuilder} method. * * @param response a response object for which the builder is based on */ public Builder(LiveHttpResponse response) { this.status = response.status(); this.version = response.version(); this.headers = response.headers().newBuilder(); this.body = response.body(); }
public static HttpResponse waitForResponse(Observable<LiveHttpResponse> responseObs) { return responseObs .flatMap(response -> toObservable(response.aggregate(120*1024))) .toBlocking() .single(); }
private static LiveHttpResponse disableCaching(LiveHttpResponse response) { return response.newBuilder() .disableCaching() .build(); }
/** * Creates a new {@link Builder} object from a response code and a content byte array. * * @param response a streaming HTTP response instance * @param body a HTTP message body */ public Builder(LiveHttpResponse response, byte[] body) { this.status = response.status(); this.version = response.version(); this.headers = response.headers().newBuilder(); this.body = body; }
private LiveHttpResponse removeRedundantContentLengthHeader(LiveHttpResponse response) { if (contentValidation && response.contentLength().isPresent() && response.chunked()) { return response.newBuilder() .removeHeader(CONTENT_LENGTH) .build(); } return response; }
@Override protected boolean matchesSafely(LiveHttpResponse response) { return response.status() == status; } }
@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(); }
private static LiveHttpResponse.Builder response() { return LiveHttpResponse.response(); }
@Test public void transformsStatus() { LiveHttpResponse response = response(OK).build() .newBuilder() .status(MOVED_PERMANENTLY) .build(); assertEquals(response.status(), MOVED_PERMANENTLY); }
@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 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 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 transformerAddsHeaders() { LiveHttpResponse response = response().build() .newBuilder() .addHeader("X-Styx-ID", "y") .build(); assertEquals(response.header("X-Styx-ID"), Optional.of("y")); }
private LiveHttpResponse exceptionToResponse(Throwable exception, LiveHttpRequest request) { HttpResponseStatus status = status(exception instanceof PluginException ? exception.getCause() : exception); String message = status.code() >= 500 ? "Site temporarily unavailable." : status.description(); return responseEnhancer.enhance( LiveHttpResponse .response(status) .body(new ByteStream(Flux.just(new Buffer(message, UTF_8)))) .build() .newBuilder(), request) .header(CONTENT_LENGTH, message.getBytes(UTF_8).length) .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 toFullResponseReleasesOriginalRefCountedBuffers() throws ExecutionException, InterruptedException { Buffer content = new Buffer(Unpooled.copiedBuffer("original", UTF_8)); LiveHttpResponse original = LiveHttpResponse.response(OK) .body(new ByteStream(Flux.just(content))) .build(); StepVerifier.create(original.aggregate(100)) .expectNextCount(1) .then(() -> assertThat(content.delegate().refCnt(), is(0))) .verifyComplete(); }
@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())); }
@Test public void firesWhenContentCancelled() { EmitterProcessor<Buffer> contentPublisher = EmitterProcessor.create(); Flux<LiveHttpResponse> listener = ResponseEventListener.from( Flux.just(response(OK) .body(new ByteStream(contentPublisher)) .build())) .whenCancelled(() -> cancelled.set(true)) .apply(); StepVerifier.create(listener) .consumeNextWith(response -> StepVerifier.create(response.body()) .then(() -> assertFalse(cancelled.get())) .thenCancel() .verify()) .verifyComplete(); assertTrue(cancelled.get()); }