@Override public Map<String, HttpHandler> adminInterfaceHandlers() { return ImmutableMap.of("status", (request, context) -> Eventual.of( response(OK) .addHeader(CONTENT_TYPE, APPLICATION_JSON) .body(format("{ name: \"%s\" status: \"%s\" }", name, status), UTF_8) .build() .stream())); }
@Override public LiveHttpResponse doHandle(LiveHttpRequest request) { return HttpResponse.response(NOT_FOUND) .body(NOT_FOUND_MESSAGE, UTF_8) .build() .stream(); } }
private LiveHttpResponse okResponse(String content) { return HttpResponse.response(OK) .header(CONTENT_TYPE, PLAIN_TEXT_UTF_8) .body(content, 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)); }
public AppHandler(Origin origin) { this.origin = origin; this.standardResponse = HttpResponse.response(OK) .header(CONTENT_TYPE, HTML_UTF_8.toString()) .body(makeAResponse(origin), UTF_8) .build(); }
@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 headerValuesAreCaseSensitive() { HttpResponse response = response(OK).header("Content-Type", "TEXT/PLAIN").build(); assertThat(response.header("content-type"), not(isValue("text/plain"))); }
@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 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)); }
@Override protected LiveHttpResponse doHandle(LiveHttpRequest request) { return response(OK) .disableCaching() .addHeader(CONTENT_TYPE, PLAIN_TEXT_UTF_8.toString()) .body("pong", UTF_8) .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(); }
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(); }
@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 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")); }
@Override protected LiveHttpResponse doHandle(LiveHttpRequest request) { return response(OK) .addHeader(CONTENT_TYPE, JSON_UTF_8.toString()) .disableCaching() .body(content(isPrettyPrint(request)), UTF_8) .build() .stream(); }
@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"))); }
@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 removesCookiesInSameBuilder() { HttpResponse r1 = response() .addCookies(responseCookie("x", "x1").build()) .removeCookies("x") .build(); assertThat(r1.cookie("x"), isAbsent()); }
@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())); }