com.hotels.styx.api.HttpResponse.bodyAs()方法的使用及代码示例

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

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

HttpResponse.bodyAs介绍

[英]Returns the message body as a String decoded with provided character set.

Decodes the message body into a Java String object with a provided charset. The caller must ensure the provided charset is compatible with message content type and encoding.
[中]以使用提供的字符集解码的字符串形式返回消息正文。
使用提供的字符集将消息体解码为Java字符串对象。调用者必须确保提供的字符集与消息内容类型和编码兼容。

代码示例

代码示例来源:origin: HotelsDotCom/styx

@Override
protected void describeMismatchSafely(T item, Description mismatchDescription) {
  mismatchDescription.appendText("content was '" + Mono.from(item.aggregate(0x100000)).block().bodyAs(UTF_8) + "'");
}

代码示例来源:origin: HotelsDotCom/styx

@Override
public boolean matchesSafely(T actual) {
  return matcher.matches(Mono.from(actual.aggregate(0x100000)).block().bodyAs(UTF_8));
}

代码示例来源: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 exposesNameAndStatusViaAdminInterface() throws ExecutionException, InterruptedException {
  DerivedStyxService service = new DerivedStyxService("derived-service", new CompletableFuture<>());
  HttpResponse response = Mono.from(service.adminInterfaceHandlers().get("status").handle(get, MOCK_CONTEXT)
          .flatMap(r -> r.aggregate(1024))).block();
  assertThat(response.bodyAs(UTF_8), is("{ name: \"derived-service\" status: \"CREATED\" }"));
}

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

public static MetricsSnapshot downloadFrom(String host, int port) throws IOException {
  HttpClient client = new StyxHttpClient.Builder().build();
  HttpResponse response = await(client.sendRequest(get(format("http://%s:%d/admin/metrics", host, port)).build()));
  return new MetricsSnapshot(decodeToMap(response.bodyAs(UTF_8)));
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void overridesContent() {
  HttpResponse response = HttpResponse.response()
      .body("Response content.", UTF_8)
      .body(" ", UTF_8)
      .body("Extra content", UTF_8)
      .build();
  assertThat(response.bodyAs(UTF_8), is("Extra content"));
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void canRemoveResponseBody() {
  HttpResponse response = response(NO_CONTENT)
      .body("shouldn't be here", UTF_8)
      .build();
  HttpResponse shouldClearBody = response.newBuilder()
      .body("", UTF_8)
      .build();
  assertThat(shouldClearBody.bodyAs(UTF_8), is(""));
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void respondsWithStaticBody() {
  StaticBodyHttpHandler handler = new StaticBodyHttpHandler(PLAIN_TEXT_UTF_8, "foo", UTF_8);
  LiveHttpResponse response = Mono.from(handler.handle(get("/").build(), HttpInterceptorContext.create())).block();
  HttpResponse fullResponse = Mono.from(response.aggregate(1024)).block();
  assertThat(fullResponse.status(), is(OK));
  assertThat(fullResponse.contentType(), isValue(PLAIN_TEXT_UTF_8.toString()));
  assertThat(fullResponse.contentLength(), isValue(length("foo")));
  assertThat(fullResponse.bodyAs(UTF_8), is("foo"));
}

代码示例来源: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 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 convertsToStreamingHttpResponse() throws Exception {
  HttpResponse response = response(CREATED)
      .version(HTTP_1_1)
      .header("HeaderName", "HeaderValue")
      .cookies(responseCookie("CookieName", "CookieValue").build())
      .body("message content", UTF_8)
      .build();
  LiveHttpResponse streaming = response.stream();
  assertThat(streaming.version(), is(HTTP_1_1));
  assertThat(streaming.status(), is(CREATED));
  assertThat(streaming.headers(), containsInAnyOrder(
      header("Content-Length", "15"),
      header("HeaderName", "HeaderValue"),
      header("Set-Cookie", "CookieName=CookieValue")
  ));
  assertThat(streaming.cookies(), contains(responseCookie("CookieName", "CookieValue").build()));
  StepVerifier.create(streaming.aggregate(0x100000).map(it -> it.bodyAs(UTF_8)))
      .expectNext("message content")
      .verifyComplete();
}

代码示例来源:origin: HotelsDotCom/styx

@Test
public void transformerReplacesBody() {
  Buffer buf1 = new Buffer("chunk 1, ", UTF_8);
  Buffer buf2 = new Buffer("chunk 2.", UTF_8);
  LiveHttpResponse response1 = response(NO_CONTENT)
      .body(new ByteStream(Flux.just(buf1, buf2)))
      .build()
      .newBuilder()
      .body(body -> body.replaceWith(ByteStream.from("replacement", UTF_8)))
      .build();
  HttpResponse response2 = Mono.from(response1.aggregate(100)).block();
  assertEquals(response2.bodyAs(UTF_8), "replacement");
  assertEquals(buf1.delegate().refCnt(), 0);
  assertEquals(buf2.delegate().refCnt(), 0);
}

相关文章