private static CharSequence viaHeader(LiveHttpResponse httpMessage) { CharSequence styxViaEntry = styxViaEntry(httpMessage.version()); return httpMessage.headers().get(VIA) .map(viaHeader -> !isNullOrEmpty(viaHeader) ? viaHeader + ", " + styxViaEntry : styxViaEntry) .orElse(styxViaEntry); }
/** * 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; }
/** * 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 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("")); }
public HttpResponse toNettyResponse(LiveHttpResponse httpResponse) { io.netty.handler.codec.http.HttpVersion version = toNettyVersion(httpResponse.version()); HttpResponseStatus httpResponseStatus = HttpResponseStatus.valueOf(httpResponse.status().code()); DefaultHttpResponse nettyResponse = new DefaultHttpResponse(version, httpResponseStatus, false); httpResponse.headers().forEach(httpHeader -> nettyResponse.headers().add(httpHeader.name(), httpHeader.value())); return nettyResponse; }
@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(); }