use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.
the class AbstractMockWebServerTestCase method getRequest.
private MockResponse getRequest(RecordedRequest request, byte[] body, String contentType) {
if (request.getMethod().equals("OPTIONS")) {
return new MockResponse().setResponseCode(200).setHeader("Allow", "GET, OPTIONS, HEAD, TRACE");
}
Buffer buf = new Buffer();
buf.write(body);
MockResponse response = new MockResponse().setHeader("Content-Length", body.length).setBody(buf).setResponseCode(200);
if (contentType != null) {
response = response.setHeader("Content-Type", contentType);
}
return response;
}
use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.
the class AbstractMockWebServerTestCase method putRequest.
private MockResponse putRequest(RecordedRequest request, String expectedRequestContent) {
assertTrue("Invalid request content-length", Integer.parseInt(request.getHeader("Content-Length")) > 0);
String requestContentType = request.getHeader("Content-Type");
assertNotNull("No content-type", requestContentType);
Charset charset = StandardCharsets.ISO_8859_1;
if (requestContentType.contains("charset=")) {
String charsetName = requestContentType.split("charset=")[1];
charset = Charset.forName(charsetName);
}
assertEquals("Invalid request body", expectedRequestContent, request.getBody().readString(charset));
return new MockResponse().setResponseCode(202);
}
use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.
the class AbstractMockWebServerTestCase method postRequest.
private MockResponse postRequest(RecordedRequest request, String expectedRequestContent, String location, String contentType, byte[] responseBody) {
assertEquals(1, request.getHeaders().values("Content-Length").size());
assertTrue("Invalid request content-length", Integer.parseInt(request.getHeader("Content-Length")) > 0);
String requestContentType = request.getHeader("Content-Type");
assertNotNull("No content-type", requestContentType);
Charset charset = StandardCharsets.ISO_8859_1;
if (requestContentType.contains("charset=")) {
String charsetName = requestContentType.split("charset=")[1];
charset = Charset.forName(charsetName);
}
assertEquals("Invalid request body", expectedRequestContent, request.getBody().readString(charset));
Buffer buf = new Buffer();
buf.write(responseBody);
return new MockResponse().setHeader("Location", baseUrl + location).setHeader("Content-Type", contentType).setHeader("Content-Length", responseBody.length).setBody(buf).setResponseCode(201);
}
use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.
the class AbstractMockWebServerTestCase method jsonPostRequest.
private MockResponse jsonPostRequest(RecordedRequest request, String location, String contentType) {
if (request.getBodySize() > 0) {
assertTrue("Invalid request content-length", Integer.parseInt(request.getHeader("Content-Length")) > 0);
assertNotNull("No content-type", request.getHeader("Content-Type"));
}
return new MockResponse().setHeader("Location", baseUrl + location).setHeader("Content-Type", contentType).setHeader("Content-Length", request.getBody().size()).setBody(request.getBody()).setResponseCode(201);
}
use of okhttp3.mockwebserver.MockResponse in project spring-framework by spring-projects.
the class WebClientIntegrationTests method plainText.
@Test
public void plainText() throws Exception {
this.server.enqueue(new MockResponse().setBody("Hello Spring!"));
Mono<String> result = this.webClient.get().uri("/greeting?name=Spring").header("X-Test-Header", "testvalue").exchange().then(response -> response.bodyToMono(String.class));
StepVerifier.create(result).expectNext("Hello Spring!").expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("testvalue", recordedRequest.getHeader("X-Test-Header"));
Assert.assertEquals("*/*", recordedRequest.getHeader(HttpHeaders.ACCEPT));
Assert.assertEquals("/greeting?name=Spring", recordedRequest.getPath());
}
Aggregations