Tabnine Logo
HttpResponse
Code IndexAdd Tabnine to your IDE (free)

How to use
HttpResponse
in
com.hotels.styx.api

Best Java code snippets using com.hotels.styx.api.HttpResponse (Showing top 20 results out of 315)

origin: HotelsDotCom/styx

  @Override
  public LiveHttpResponse doHandle(LiveHttpRequest request) {
    return HttpResponse.response(NOT_FOUND)
        .body(NOT_FOUND_MESSAGE, UTF_8)
        .build()
        .stream();
  }
}
origin: HotelsDotCom/styx

@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));
}
origin: HotelsDotCom/styx

/**
 * Creates a new {@link Builder} object from an existing {@link LiveHttpResponse} object.
 * Similar to {@link this.newBuilder} method.
 *
 * @param response a full HTTP response instance
 */
public Builder(HttpResponse response) {
  this.status = response.status();
  this.version = response.version();
  this.headers = response.headers().newBuilder();
  this.body = response.body();
}
origin: com.hotels.styx/styx-client

private HttpResponse removeRedundantContentLengthHeader(HttpResponse response) {
  if (contentValidation && response.contentLength().isPresent() && response.chunked()) {
    return response.newBuilder()
        .removeHeader(CONTENT_LENGTH)
        .build();
  }
  return response;
}
origin: HotelsDotCom/styx

private HttpResponse.Builder restrictedMetricsResponse(MetricRequest request) {
  Map<String, Metric> fullMetrics = metricRegistry.getMetrics();
  Map<String, Metric> restricted = filter(fullMetrics, (name, metric) -> request.matchesRoot(name));
  return restricted.isEmpty()
      ? response(NOT_FOUND)
      : search(request, restricted);
}
origin: HotelsDotCom/styx

@Test
public void responseBodyCannotBeChangedViaStreamingMessage() {
  HttpResponse original = response(OK)
      .body("original", UTF_8)
      .build();
  Flux.from(original.stream()
      .body()
      .map(buf -> {
        buf.delegate().array()[0] = 'A';
        return buf;
      }))
      .subscribe();
  assertThat(original.bodyAs(UTF_8), is("original"));
}
origin: HotelsDotCom/styx

@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"));
}
origin: HotelsDotCom/styx

@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"));
}
origin: HotelsDotCom/styx

@Test
public void newCookiesWithDuplicateNamesOverridePreviousOnes() {
  HttpResponse r1 = response()
      .cookies(responseCookie("y", "y1").build())
      .build();
  HttpResponse r2 = r1.newBuilder().addCookies(
      responseCookie("y", "y2").build())
      .build();
  assertThat(r2.cookies(), containsInAnyOrder(responseCookie("y", "y2").build()));
}
origin: HotelsDotCom/styx

@Test
public void addsCookies() {
  HttpResponse response = response()
      .addCookies(responseCookie("x", "x1").build(), responseCookie("y", "y1").build())
      .build();
  assertThat(response.cookies(), containsInAnyOrder(responseCookie("x", "x1").build(), responseCookie("y", "y1").build()));
}
origin: HotelsDotCom/styx

@Test(dataProvider = "emptyBodyResponses")
public void convertsToStreamingHttpResponseWithEmptyBody(HttpResponse response) throws ExecutionException, InterruptedException {
  LiveHttpResponse streaming = response.stream();
  byte[] result = streaming.body().aggregate(1000)
      .get()
      .content();
  assertThat(result.length, is(0));
}
origin: HotelsDotCom/styx

@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")));
}
origin: HotelsDotCom/styx

@Test
public void responseBodyIsImmutable() {
  HttpResponse response = response(OK)
      .body("Original body", UTF_8)
      .build();
  response.body()[0] = 'A';
  assertThat(response.bodyAs(UTF_8), is("Original body"));
}
origin: HotelsDotCom/styx

@Test
public void contentFromStringSetsContentLengthIfRequired() {
  HttpResponse response1 = HttpResponse.response()
      .body("Response content.", UTF_8, true)
      .build();
  assertThat(response1.header("Content-Length"), is(Optional.of("17")));
  HttpResponse response2 = HttpResponse.response()
      .body("Response content.", UTF_8, false)
      .build();
  assertThat(response2.header("Content-Length"), is(Optional.empty()));
}
origin: HotelsDotCom/styx

@Test
public void transformedBodyIsNewCopy() {
  HttpResponse request = response()
      .body("Original body", UTF_8)
      .build();
  HttpResponse newRequest = response()
      .body("New body", UTF_8)
      .build();
  assertThat(request.bodyAs(UTF_8), is("Original body"));
  assertThat(newRequest.bodyAs(UTF_8), is("New body"));
}
origin: HotelsDotCom/styx

@Test
public void removesCookiesInSameBuilder() {
  HttpResponse r1 = response()
      .addCookies(responseCookie("x", "x1").build())
      .removeCookies("x")
      .build();
  assertThat(r1.cookie("x"), isAbsent());
}
origin: HotelsDotCom/styx

@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")));
}
origin: HotelsDotCom/styx

@Test
public void contentFromByteArraySetsContentLengthIfRequired() {
  HttpResponse response1 = HttpResponse.response()
      .body("Response content.".getBytes(UTF_16), true)
      .build();
  assertThat(response1.body(), is("Response content.".getBytes(UTF_16)));
  assertThat(response1.header("Content-Length"), is(Optional.of("36")));
  HttpResponse response2 = HttpResponse.response()
      .body("Response content.".getBytes(UTF_8), false)
      .build();
  assertThat(response2.body(), is("Response content.".getBytes(UTF_8)));
  assertThat(response2.header("Content-Length"), is(Optional.empty()));
}
origin: HotelsDotCom/styx

@Override
public Eventual<LiveHttpResponse> handle(LiveHttpRequest request, HttpInterceptor.Context context) {
  HttpResponse.Builder responseBuilder = standardResponse.newBuilder()
      .headers(request.headers())
      .header(STUB_ORIGIN_INFO, origin.applicationInfo());
  return Eventual.of(Optional.ofNullable(responseBuilder)
      .map(it -> request.queryParam("status")
          .map(status -> it.status(httpResponseStatus(status))
              .body("Returning requested status (" + status + ")", UTF_8))
          .orElse(it))
      .map(it -> request.queryParam("length")
          .map(length -> it.body(generateContent(parseInt(length)), UTF_8))
          .orElse(it))
      .orElse(responseBuilder)
      .build()
      .stream());
}
origin: com.hotels.styx/styx-common

private static Info information(HttpResponse response, boolean longFormatEnabled) {
  Info info = new Info()
      .add("status", response.status());
  if (longFormatEnabled) {
    info.add("headers", response.headers())
        .add("cookies", response.cookies());
  }
  return info;
}
com.hotels.styx.apiHttpResponse

Javadoc

An immutable HTTP response object including full body content.

A HttpResponse is useful for responses with a finite body content, such as when a REST API object is returned as a response to a GET request.

A HttpResponse is created via HttpResponse.Builder. A new builder can be obtained by a call to following static methods:

  • response()
  • response(HttpResponseStatus)
A builder can also be created with one of the Builder constructors. HttpResponse is immutable. Once created it cannot be modified. However a response can be transformed to another using the this#newBuildermethod. It creates a new Builder with all message properties and body content cloned in.

Most used methods

  • response
    Creates an HTTP response builder with a given status and empty body.
  • stream
    Converts this response to a streaming form (LiveHttpResponse). Converts this response to an LiveHttp
  • newBuilder
    Return a new HttpResponse.Builder that will inherit properties from this response. This allows a new
  • status
    Returns the HTTP response status.
  • bodyAs
    Returns the message body as a String decoded with provided character set. Decodes the message body
  • body
    Returns the body of this message in its unencoded form.
  • cookies
    Decodes the "Set-Cookie" headers in this response and returns the cookies.
  • headers
  • chunked
    Return true if the response is chunked.
  • contentLength
    Returns the value of the 'Content-Length' header.
  • header
    Returns the value of the header with the specified name. If there is more than one header value for
  • version
    Returns the protocol version of this HttpResponse.
  • header,
  • version,
  • <init>,
  • contentType,
  • cookie,
  • isRedirect

Popular in Java

  • Making http requests using okhttp
  • setScale (BigDecimal)
  • getSystemService (Context)
  • getSharedPreferences (Context)
  • HttpServer (com.sun.net.httpserver)
    This class implements a simple HTTP server. A HttpServer is bound to an IP address and port number a
  • Path (java.nio.file)
  • List (java.util)
    An ordered collection (also known as a sequence). The user of this interface has precise control ove
  • JFrame (javax.swing)
  • Get (org.apache.hadoop.hbase.client)
    Used to perform Get operations on a single row. To get everything for a row, instantiate a Get objec
  • Project (org.apache.tools.ant)
    Central representation of an Ant project. This class defines an Ant project with all of its targets,
  • Github Copilot alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now