/** * Decodes a specified Set-Cookie header and returns it as a {@link ResponseCookie} object. * * @param name cookie name * @return an optional {@link ResponseCookie} value if corresponding cookie name is present, * or {@link Optional#empty} if not. */ public Optional<ResponseCookie> cookie(String name) { return cookies().stream() .filter(cookie -> cookie.name().equals(name)) .findFirst(); }
@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 setsMultipleOutboundCookies() { LiveHttpResponse response = 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 addsCookies() { LiveHttpResponse 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 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 removesCookies() { LiveHttpResponse r1 = response() .addCookies(responseCookie("x", "x1").build(), responseCookie("y", "y1").build()) .build(); LiveHttpResponse 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 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(); }
@Test public void addsCookiesToExistingCookies() { LiveHttpResponse 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())); }