@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("")); }
private Eventual<LiveHttpResponse> getCurrentOrPutNewState(LiveHttpRequest request, HttpInterceptor.Context context) { if (GET.equals(request.method())) { return getCurrentState(request, context); } else if (PUT.equals(request.method())) { return putNewState(request, context); } else { return Eventual.of(response(METHOD_NOT_ALLOWED).build()); } }
@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 transformerRemovesHeaders() { LiveHttpResponse response = response().addHeader("X-Styx-ID", "y").build() .newBuilder() .removeHeader("X-Styx-ID") .build(); assertEquals(response.header("X-Styx-ID"), Optional.empty()); }
@Test public void transformsCookies() { LiveHttpResponse response = response().build() .newBuilder() .cookies(responseCookie("x", "y").build()) .build(); assertEquals(response.cookie("x"), Optional.of(responseCookie("x", "y").build())); }
@Test public void transformsStatus() { LiveHttpResponse response = response(OK).build() .newBuilder() .status(MOVED_PERMANENTLY) .build(); assertEquals(response.status(), MOVED_PERMANENTLY); }
@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(expectedExceptions = IllegalArgumentException.class) public void rejectsMultipleContentLengthInSingleHeader() { response() .addHeader(CONTENT_LENGTH, "15, 16") .ensureContentLengthIsValid() .build(); }
private static LiveHttpResponse error(HttpResponseStatus status) { return new LiveHttpResponse.Builder(status) .body(new ByteStream(Flux.just(new Buffer(status.description(), UTF_8)))) .build(); }
@Test public void transformerRemovesCookies() { LiveHttpResponse response = response() .addCookies(ImmutableList.of(responseCookie("x", "y").build())) .build() .newBuilder() .removeCookies("x") .build(); assertEquals(response.cookie("x"), Optional.empty()); }
@Test public void transformerAddsCookies() { LiveHttpResponse response = response().build() .newBuilder() .addCookies(responseCookie("x", "y").build()) .build(); assertEquals(response.cookie("x"), Optional.of(responseCookie("x", "y").build())); }
@Test(expectedExceptions = IllegalArgumentException.class) public void rejectsInvalidContentLength() { response() .addHeader(CONTENT_LENGTH, "foo") .ensureContentLengthIsValid() .build(); }
@Test public void addsHeaderValue() { LiveHttpResponse response = response() .header("name", "value1") .addHeader("name", "value2") .build(); assertThat(response.headers(), hasItem(header("name", "value1"))); assertThat(response.headers(), hasItem(header("name", "value2"))); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Invalid Content-Length found. -3") public void ensuresContentLengthIsPositive() { response(OK) .header("Content-Length", -3) .build(); }
@Test public void transformsWithCookieList() { LiveHttpResponse response = response().build() .newBuilder() .cookies(ImmutableList.of(responseCookie("x", "y").build())) .build(); assertEquals(response.cookie("x"), Optional.of(responseCookie("x", "y").build())); }
@Test public void getASingleCookieValue() { LiveHttpResponse response = response() .cookies( responseCookie("a", "b").build(), responseCookie("c", "d").build()) .build(); assertThat(response.cookie("c"), isValue(responseCookie("c", "d").build())); }
@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 setsASingleOutboundCookie() { LiveHttpResponse response = response() .cookies(responseCookie("user", "QSplbl9HX1VL").domain(".hotels.com").path("/").maxAge(3600).build()) .build(); assertThat(response.cookie("user"), isValue(responseCookie("user", "QSplbl9HX1VL").domain(".hotels.com").path("/").maxAge(3600).build())); }