@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 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 headerValuesAreCaseSensitive() { HttpResponse response = response(OK).header("Content-Type", "TEXT/PLAIN").build(); assertThat(response.header("content-type"), not(isValue("text/plain"))); }
@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 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"))); }
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(); }
private static HttpResponse responseWithoutBody(HttpResponse response) { return response.newBuilder() .header(CONTENT_LENGTH, 0) .removeHeader(TRANSFER_ENCODING) .removeBody() .build(); }
/** * Adds the necessary header to make the current request not cache-able by the client. * * @return {@code this} */ public Builder disableCaching() { header("Pragma", "no-cache"); header("Expires", "Mon, 1 Jan 2007 08:00:00 GMT"); header("Cache-Control", "no-cache,must-revalidate,no-store"); return this; }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Invalid Content-Length found. -3") public void ensuresContentLengthIsPositive() { response() .header("Content-Length", -3) .build(); } }
private HttpResponse addOriginId(Id originId, HttpResponse response) { return response.newBuilder() .header(originIdHeaderName, originId) .build(); } }
/** * Sets the 'Content-Type' header to the specified {@code contentType}. * * @param contentType the content type to set * @return {@code this} */ public Builder contentType(MediaType contentType) { header(CONTENT_TYPE, contentType.toString()); return this; }
@Test public void supportsCaseInsensitiveHeaderNames() { HttpResponse response = response(OK).header("Content-Type", "text/plain").build(); assertThat(response.header("content-type"), isValue("text/plain")); }
private LiveHttpResponse errorResponse(HttpResponseStatus code, String content) { return HttpResponse.response(code) .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(); }