/** * Decodes the "Set-Cookie" headers in this response and returns the specified cookie. * * @param name cookie name * @return an optional cookie */ public Optional<ResponseCookie> cookie(String name) { return cookies().stream() .filter(cookie -> cookie.name().equals(name)) .findFirst(); }
public Builder(HttpResponse response, byte[] encodedBody) { this.status = statusWithCode(response.status().code()); this.version = httpVersion(response.version().toString()); this.headers = response.headers().newBuilder(); this.body = encodedBody; this.cookies = new ArrayList<>(response.cookies()); }
public Builder(HttpResponse response, Observable<ByteBuf> decoded) { this.status = statusWithCode(response.status().code()); this.version = httpVersion(response.version().toString()); this.headers = response.headers().newBuilder(); this.body = decoded; this.cookies = new ArrayList<>(response.cookies()); }
@Test public void createsAResponseWithDefaultValues() { HttpResponse response = HttpResponse.response().build(); assertThat(response.version(), is(HTTP_1_1)); assertThat(response.cookies(), is(emptyIterable())); assertThat(response.headers(), is(emptyIterable())); assertThat(response.body().length, is(0)); }
@Test public void createsResponseWithMinimalInformation() { HttpResponse response = HttpResponse.response() .status(BAD_GATEWAY) .version(HTTP_1_1) .build(); assertThat(response.status(), is(BAD_GATEWAY)); assertThat(response.version(), is(HTTP_1_1)); assertThat(response.cookies(), is(emptyIterable())); assertThat(response.headers(), is(emptyIterable())); assertThat(response.body().length, is(0)); }
private static Info information(HttpResponse response, boolean longFormatEnabled) { Info info = new Info() .add("status", response.status()); if (longFormatEnabled) { info.add("headers", response.headers()) .add("cookies", response.cookies()); } return info; }
@Test public void encodesToFullHttpResponse() { LiveHttpResponse response = response(CREATED) .version(HTTP_1_0) .header("HeaderName", "HeaderValue") .cookies(responseCookie("CookieName", "CookieValue").build()) .body(new ByteStream(Flux.just("foo", "bar").map(it -> new Buffer(copiedBuffer(it, UTF_8))))) .build(); HttpResponse full = Mono.from(response.aggregate(0x1000)).block(); assertThat(full.status(), is(CREATED)); assertThat(full.version(), is(HTTP_1_0)); assertThat(full.headers(), containsInAnyOrder(header("HeaderName", "HeaderValue"), header("Set-Cookie", "CookieName=CookieValue"))); assertThat(full.cookies(), contains(responseCookie("CookieName", "CookieValue").build())); assertThat(full.body(), is(bytes("foobar"))); }
@Test public void addsCookies() { HttpResponse response = response() .addCookies(responseCookie("x", "x1").build(), responseCookie("y", "y1").build()) .build(); assertThat(response.cookies(), containsInAnyOrder(responseCookie("x", "x1").build(), responseCookie("y", "y1").build())); }
@Test public void setsMultipleOutboundCookies() { HttpResponse response = HttpResponse.response() .cookies( responseCookie("a", "b").build(), responseCookie("c", "d").build()) .build(); Set<ResponseCookie> cookies = response.cookies(); assertThat(cookies, containsInAnyOrder( responseCookie("a", "b").build(), responseCookie("c", "d").build() )); }
@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())); }
@Test public void addsCookiesToExistingCookies() { HttpResponse response = response() .addCookies(responseCookie("z", "z1").build()) .addCookies(responseCookie("x", "x1").build(), responseCookie("y", "y1").build()) .build(); assertThat(response.cookies(), containsInAnyOrder(responseCookie("x", "x1").build(), responseCookie("y", "y1").build(), responseCookie("z", "z1").build())); }