@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)); }
@Test public void shouldNotFailToRemoveNonExistentContentLength() { HttpResponse response = HttpResponse.response().build(); HttpResponse chunkedResponse = response.newBuilder().setChunked().build(); assertThat(chunkedResponse.chunked(), is(true)); assertThat(chunkedResponse.header(CONTENT_LENGTH).isPresent(), is(false)); }
@Override public HttpResponse doHandle(HttpRequest request) { return response(OK) .header(CONTENT_TYPE, this.contentType.toString()) .header(CONTENT_LENGTH, this.contentLength) .body(this.body) .build(); } }
private static LiveHttpResponse responseWith(HttpResponseStatus status, String message) { return HttpResponse.response(status) .body(message + "\n", UTF_8) .addHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8.toString()) .disableCaching() .build() .stream(); }
@Override protected LiveHttpResponse doHandle(LiveHttpRequest request) { return response(OK) .addHeader(CONTENT_TYPE, HTML_UTF_8.toString()) .header(CONTENT_LANGUAGE, "en") .body(html, UTF_8) .build() .stream(); }
@Override protected LiveHttpResponse doHandle(LiveHttpRequest request) { return response(OK) .disableCaching() .addHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8.toString()) .body("pong", UTF_8) .build() .stream(); } }
@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "Charset is not provided.") public void contentFromStringThrowsNPEWhenCharsetIsNull() { HttpResponse.response() .body("Response content.", null, false) .build(); }
@Test public void encodesBodyWithGivenCharset() { HttpResponse response = HttpResponse.response() .body("Response content.", UTF_16, true) .build(); assertThat(response.body().length, is(36)); }
@Test public void allowsModificationOfHeadersBasedOnBody() { HttpResponse response = HttpResponse.response() .body("foobar", UTF_8) .build(); assertThat(response.header("some-header"), isAbsent()); HttpResponse newResponse = response.newBuilder() .header("some-header", response.body().length) .build(); assertThat(newResponse.header("some-header"), isValue("6")); assertThat(newResponse.bodyAs(UTF_8), is("foobar")); }
@Test public void addsHeaderValue() { HttpResponse response = HttpResponse.response() .header("name", "value1") .addHeader("name", "value2") .build(); assertThat(response.headers(), hasItem(header("name", "value1"))); assertThat(response.headers(), hasItem(header("name", "value2"))); }
@Test public void shouldRemoveContentLengthFromChunkedMessages() { HttpResponse response = HttpResponse.response().header(CONTENT_LENGTH, 5).build(); HttpResponse chunkedResponse = response.newBuilder().setChunked().build(); assertThat(chunkedResponse.chunked(), is(true)); assertThat(chunkedResponse.header(CONTENT_LENGTH).isPresent(), is(false)); }
@Test public void canRemoveAHeader() { Object headerValue = "b"; HttpResponse response = HttpResponse.response() .header("a", headerValue) .addHeader("c", headerValue) .build(); HttpResponse shouldRemoveHeader = response.newBuilder() .removeHeader("c") .build(); assertThat(shouldRemoveHeader.headers(), contains(header("a", "b"))); }
private LiveHttpResponse okResponse(String content) { return HttpResponse.response(OK) .header(CONTENT_TYPE, PLAIN_TEXT_UTF_8) .body(content, UTF_8) .build() .stream(); }
@Override public LiveHttpResponse doHandle(LiveHttpRequest request) { return HttpResponse.response(OK) .disableCaching() .header(CONTENT_TYPE, PLAIN_TEXT_UTF_8) .body(threadDumpContent(), true) .build() .stream(); }
@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())); }
@Test public void allowsModificationOfBodyBasedOnExistingBody() { HttpResponse response = HttpResponse.response() .body("foobar", UTF_8) .build(); HttpResponse newResponse = response.newBuilder() .body(response.bodyAs(UTF_8) + "x", UTF_8) .build(); assertThat(newResponse.bodyAs(UTF_8), is("foobarx")); }
@Test(expectedExceptions = IllegalArgumentException.class) public void rejectsInvalidContentLength() { HttpResponse.response() .addHeader(CONTENT_LENGTH, "foo") .ensureContentLengthIsValid() .build(); }
LiveHttpResponse toResponse() { return HttpResponse.response(OK) .header(CONTENT_TYPE, contentType) .header(CONTENT_LENGTH, contentLength) .body(content, UTF_8) .build() .stream(); } }