MockServer 意外响应为空主体
MockServer unexpectedly responds with empty body
让我们看一个测试,它使用 MockServer (org.mock-server:mockserver-netty:5.10.0
) 来模拟响应。
预计响应正文将等于字符串"something"
。
然而,这个测试失败了,因为响应主体是一个空字符串。
@Test
void test1() throws Exception {
var server = ClientAndServer.startClientAndServer(9001);
server
.when(
request().withMethod("POST").withPath("/checks/"),
exactly(1)
)
.respond(
response()
.withBody("\"something\"")
.withStatusCode(205)
.withHeader("Content-Type", "application/json")
);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:9001/checks/"))
.POST(BodyPublishers.noBody())
.build();
HttpResponse<String> response =
HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
assertEquals(205, response.statusCode());
assertEquals("something", response.body()); // fails
}
如何使响应正文等于response().withBody(...)
中提供的字符串?
让我们看一个测试,它使用 MockServer (org.mock-server:mockserver-netty:5.10.0
) 来模拟响应。
预计响应正文将等于字符串"something"
。
然而,这个测试失败了,因为响应主体是一个空字符串。
@Test
void test1() throws Exception {
var server = ClientAndServer.startClientAndServer(9001);
server
.when(
request().withMethod("POST").withPath("/checks/"),
exactly(1)
)
.respond(
response()
.withBody("\"something\"")
.withStatusCode(205)
.withHeader("Content-Type", "application/json")
);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:9001/checks/"))
.POST(BodyPublishers.noBody())
.build();
HttpResponse<String> response =
HttpClient.newHttpClient().send(request, BodyHandlers.ofString());
assertEquals(205, response.statusCode());
assertEquals("something", response.body()); // fails
}
如何使响应正文等于response().withBody(...)
中提供的字符串?