org.mockserver.model.HttpResponse.withReasonPhrase()方法的使用及代码示例

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

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

HttpResponse.withReasonPhrase介绍

[英]The reason phrase to return, if no reason code is returned this will be defaulted to the standard reason phrase for the statusCode, i.e. for a statusCode of 200 the standard reason phrase is "OK"
[中]要返回的原因短语,如果没有返回原因代码,则默认为状态代码的标准原因短语,即对于200的状态代码,标准原因短语为“OK”

代码示例

代码示例来源:origin: jamesdbloom/mockserver

private void setStatusCode(HttpResponse httpResponse, FullHttpResponse fullHttpResponse) {
  HttpResponseStatus status = fullHttpResponse.status();
  httpResponse.withStatusCode(status.code());
  httpResponse.withReasonPhrase(status.reasonPhrase());
}

代码示例来源:origin: jamesdbloom/mockserver

/**
 * Static builder to create a not found response.
 */
public static HttpResponse notFoundResponse() {
  return new HttpResponse().withStatusCode(NOT_FOUND_404.code()).withReasonPhrase(NOT_FOUND_404.reasonPhrase());
}

代码示例来源:origin: jamesdbloom/mockserver

@Override
public void writeResponse(HttpRequest request, HttpResponseStatus responseStatus, String body, String contentType) {
  HttpResponse response = response()
    .withStatusCode(responseStatus.code())
    .withReasonPhrase(responseStatus.reasonPhrase())
    .withBody(body);
  if (body != null && !body.isEmpty()) {
    response.replaceHeader(header(CONTENT_TYPE.toString(), contentType + "; charset=utf-8"));
  }
  writeResponse(request, response, true);
}

代码示例来源:origin: jamesdbloom/mockserver

/**
 * Static builder to create a response with a 200 status code and the string response body.
 *
 * @param body a string
 */
public static HttpResponse response(String body) {
  return new HttpResponse().withStatusCode(OK_200.code()).withReasonPhrase(OK_200.reasonPhrase()).withBody(body);
}

代码示例来源:origin: jamesdbloom/mockserver

public HttpResponse buildObject() {
  return new HttpResponse()
    .withStatusCode(statusCode)
    .withReasonPhrase(reasonPhrase)
    .withBody(body != null ? body.buildObject() : null)
    .withHeaders(headers)
    .withCookies(cookies)
    .withDelay((delay != null ? delay.buildObject() : null))
    .withConnectionOptions((connectionOptions != null ? connectionOptions.buildObject() : null));
}

代码示例来源:origin: jamesdbloom/mockserver

public HttpResponse clone() {
    return response()
      .withStatusCode(statusCode)
      .withReasonPhrase(reasonPhrase)
      .withBody(body)
      .withHeaders(headers.clone())
      .withCookies(cookies.clone())
      .withDelay(getDelay())
      .withConnectionOptions(connectionOptions);
  }
}

代码示例来源:origin: jamesdbloom/mockserver

response()
    .withStatusCode(HttpStatusCode.ACCEPTED_202.code())
    .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase())
    .withBody("some_body_response")
);
response()
  .withStatusCode(HttpStatusCode.NOT_FOUND_404.code())
  .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()),
makeRequest(
  request()
response()
  .withStatusCode(HttpStatusCode.NOT_FOUND_404.code())
  .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()),
makeRequest(
  request()

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldNotVerifyNoRequestsReceived() {
  // when
  mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response().withBody("some_body"));
  // then
  assertEquals(
    response()
      .withStatusCode(OK_200.code())
      .withReasonPhrase(OK_200.reasonPhrase())
      .withBody("some_body"),
    makeRequest(
      request()
        .withPath(calculatePath("some_path")),
      headersToIgnore)
  );
  try {
    mockServerClient.verifyZeroInteractions();
    fail();
  } catch (AssertionError ae) {
    assertThat(ae.getMessage(), startsWith("Request not found exactly 0 times, expected:<{ }> but was:<{" + NEW_LINE +
      "  \"method\" : \"GET\"," + NEW_LINE +
      "  \"path\" : \"" + calculatePath("some_path") + "\"," + NEW_LINE));
  }
}

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldReturnResponseWithOnlyBody() {
  // when
  mockServerClient.when(request()).respond(response().withBody("some_body"));
  // then
  // - in http
  assertEquals(
    response()
      .withStatusCode(OK_200.code())
      .withReasonPhrase(OK_200.reasonPhrase())
      .withBody("some_body"),
    makeRequest(
      request()
        .withPath(calculatePath("")),
      headersToIgnore)
  );
  // - in https
  assertEquals(
    response()
      .withStatusCode(OK_200.code())
      .withReasonPhrase(OK_200.reasonPhrase())
      .withBody("some_body"),
    makeRequest(
      request()
        .withSecure(true)
        .withPath(calculatePath("")),
      headersToIgnore)
  );
}

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldReturnResponseForRequestNotInSsl() {
  // when
  mockServerClient.when(request().withSecure(false)).respond(response().withBody("some_body"));
  // then
  // - in http
  assertEquals(
    response()
      .withStatusCode(OK_200.code())
      .withReasonPhrase(OK_200.reasonPhrase())
      .withBody("some_body"),
    makeRequest(
      request()
        .withPath(calculatePath("")),
      headersToIgnore)
  );
  // - in https
  assertEquals(
    response()
      .withStatusCode(HttpStatusCode.NOT_FOUND_404.code())
      .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()),
    makeRequest(
      request()
        .withSecure(true)
        .withPath(calculatePath("")),
      headersToIgnore)
  );
}

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldReturnResponseForRequestInSsl() {
  // when
  mockServerClient.when(request().withSecure(true)).respond(response().withBody("some_body"));
  // then
  // - in http
  assertEquals(
    response()
      .withStatusCode(HttpStatusCode.NOT_FOUND_404.code())
      .withReasonPhrase(HttpStatusCode.NOT_FOUND_404.reasonPhrase()),
    makeRequest(
      request()
        .withPath(calculatePath("")),
      headersToIgnore)
  );
  // - in https
  assertEquals(
    response()
      .withStatusCode(OK_200.code())
      .withReasonPhrase(OK_200.reasonPhrase())
      .withBody("some_body"),
    makeRequest(
      request()
        .withSecure(true)
        .withPath(calculatePath("")),
      headersToIgnore)
  );
}

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldVerifyNotEnoughRequestsReceived() {
  // when
  mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response().withBody("some_body"));
  // then
  assertEquals(
    response()
      .withStatusCode(OK_200.code())
      .withReasonPhrase(OK_200.reasonPhrase())
      .withBody("some_body"),
    makeRequest(
      request()
        .withPath(calculatePath("some_path")),
      headersToIgnore)
  );
  try {
    mockServerClient.verify(request()
      .withPath(calculatePath("some_path")), VerificationTimes.atLeast(2));
    fail();
  } catch (AssertionError ae) {
    assertThat(ae.getMessage(), startsWith("Request not found at least 2 times, expected:<{" + NEW_LINE +
      "  \"path\" : \"" + calculatePath("some_path") + "\"" + NEW_LINE +
      "}> but was:<{" + NEW_LINE +
      "  \"method\" : \"GET\"," + NEW_LINE +
      "  \"path\" : \"" + calculatePath("some_path") + "\"," + NEW_LINE));
  }
}

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldVerifyNoMatchingRequestsReceived() {
  // when
  mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response().withBody("some_body"));
  // then
  assertEquals(
    response()
      .withStatusCode(OK_200.code())
      .withReasonPhrase(OK_200.reasonPhrase())
      .withBody("some_body"),
    makeRequest(
      request()
        .withPath(calculatePath("some_path")),
      headersToIgnore)
  );
  try {
    mockServerClient.verify(request()
      .withPath(calculatePath("some_other_path")), VerificationTimes.exactly(2));
    fail();
  } catch (AssertionError ae) {
    assertThat(ae.getMessage(), startsWith("Request not found exactly 2 times, expected:<{" + NEW_LINE +
      "  \"path\" : \"" + calculatePath("some_other_path") + "\"" + NEW_LINE +
      "}> but was:<{" + NEW_LINE +
      "  \"method\" : \"GET\"," + NEW_LINE +
      "  \"path\" : \"" + calculatePath("some_path") + "\"," + NEW_LINE));
  }
}

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldVerifyTooManyRequestsReceived() {
  // when
  mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response().withBody("some_body"));
  // then
  assertEquals(
    response()
      .withStatusCode(OK_200.code())
      .withReasonPhrase(OK_200.reasonPhrase())
      .withBody("some_body"),
    makeRequest(
      request()
        .withPath(calculatePath("some_path")),
      headersToIgnore)
  );
  try {
    mockServerClient.verify(request()
      .withPath(calculatePath("some_path")), VerificationTimes.exactly(0));
    fail();
  } catch (AssertionError ae) {
    assertThat(ae.getMessage(), startsWith("Request not found exactly 0 times, expected:<{" + NEW_LINE +
      "  \"path\" : \"" + calculatePath("some_path") + "\"" + NEW_LINE +
      "}> but was:<{" + NEW_LINE +
      "  \"method\" : \"GET\"," + NEW_LINE +
      "  \"path\" : \"" + calculatePath("some_path") + "\"," + NEW_LINE));
  }
}

代码示例来源:origin: jamesdbloom/mockserver

response()
    .withStatusCode(HttpStatusCode.ACCEPTED_202.code())
    .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase())
    .withBody("some_body_response")
);
response()
  .withStatusCode(HttpStatusCode.ACCEPTED_202.code())
  .withReasonPhrase(HttpStatusCode.ACCEPTED_202.reasonPhrase())
  .withBody("some_body_response"),
makeRequest(

代码示例来源:origin: jamesdbloom/mockserver

response()
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withBody("some_body2"),
makeRequest(

代码示例来源:origin: jamesdbloom/mockserver

.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_16).toString())
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withBody(body),
makeRequest(
  .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_16).toString())
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withBody(body),
makeRequest(

代码示例来源:origin: jamesdbloom/mockserver

.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withBody(body),
makeRequest(
  .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withBody(body),
makeRequest(

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldVerifyReceivedRequestsWithNoBody() {
  // when
  mockServerClient.when(request().withPath(calculatePath("some_path")), exactly(2)).respond(response());
  // then
  // - in http
  assertEquals(
    response()
      .withStatusCode(OK_200.code())
      .withReasonPhrase(OK_200.reasonPhrase()),
    makeRequest(
      request()
        .withPath(calculatePath("some_path")),
      headersToIgnore)
  );
  mockServerClient.verify(request()
    .withPath(calculatePath("some_path")));
  mockServerClient.verify(request()
    .withPath(calculatePath("some_path")), VerificationTimes.exactly(1));
}

代码示例来源:origin: jamesdbloom/mockserver

.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withBody(body),
makeRequest(
  .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withBody(body),
makeRequest(

相关文章