com.hotels.styx.api.HttpResponse类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(81)

本文整理了Java中com.hotels.styx.api.HttpResponse类的一些代码示例,展示了HttpResponse类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpResponse类的具体详情如下:
包路径:com.hotels.styx.api.HttpResponse
类名称:HttpResponse

HttpResponse介绍

[英]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.
    [中]包含全文内容的不可变HTTP响应对象。
    HttpResponse对于具有有限正文内容的响应非常有用,例如当RESTAPI对象作为GET请求的响应返回时。
    通过HttpResponse创建HttpResponse。建设者通过调用以下静态方法可以获得新的生成器:
    *答复()
    *响应(HttpResponseStatus)
    也可以使用其中一个生成器构造函数创建生成器。HttpResponse是不可变的。一旦创建,它就不能被修改。但是,可以使用this#newbuilder方法将一个响应转换为另一个响应。它将创建一个新的生成器,其中克隆了所有消息属性和正文内容。

代码示例

代码示例来源: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;
}

相关文章