Subscription subscriber = toObservable(response.body()).map(Buffers::toByteBuf).subscribe(new Subscriber<ByteBuf>() { @Override public void onStart() { } catch (Throwable cause) { LOGGER.warn("Failed to convert response headers. response={}, Cause={}", new Object[]{response, cause}); toObservable(response.body()).forEach(it -> Buffers.toByteBuf(it).release()); future.completeExceptionally(cause); return future;
/** * 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(); }
@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 createsAResponseWithDefaultValues() throws Exception { LiveHttpResponse response = response().build(); assertThat(response.version(), is(HTTP_1_1)); assertThat(response.cookies(), is(emptyIterable())); assertThat(response.headers(), is(emptyIterable())); assertThat(bytesToString(response.body()), is("")); }
@Test public void createsResponseWithMinimalInformation() throws Exception { LiveHttpResponse response = response() .status(BAD_GATEWAY) .version(HTTP_1_0) .build(); assertThat(response.status(), is(BAD_GATEWAY)); assertThat(response.version(), is(HTTP_1_0)); assertThat(response.cookies(), is(emptyIterable())); assertThat(response.headers(), is(emptyIterable())); assertThat(bytesToString(response.body()), is("")); }
@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 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()); }