use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-framework by spring-projects.
the class MockMvcClientHttpRequestFactory method createRequest.
@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
return new MockClientHttpRequest(httpMethod, uri) {
@Override
public ClientHttpResponse executeInternal() throws IOException {
try {
MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri);
requestBuilder.content(getBodyAsBytes());
requestBuilder.headers(getHeaders());
MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse servletResponse = mvcResult.getResponse();
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
byte[] body = servletResponse.getContentAsByteArray();
HttpHeaders headers = getResponseHeaders(servletResponse);
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
clientResponse.getHeaders().putAll(headers);
return clientResponse;
} catch (Exception ex) {
byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8);
return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
};
}
use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-framework by spring-projects.
the class ResponseCreatorsTests method created.
@Test
public void created() throws Exception {
URI location = new URI("/foo");
DefaultResponseCreator responseCreator = MockRestResponseCreators.withCreatedEntity(location);
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertEquals(location, response.getHeaders().getLocation());
assertNull(response.getBody());
}
use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-framework by spring-projects.
the class ResponseCreatorsTests method noContent.
@Test
public void noContent() throws Exception {
DefaultResponseCreator responseCreator = MockRestResponseCreators.withNoContent();
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
assertTrue(response.getHeaders().isEmpty());
assertNull(response.getBody());
}
use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-framework by spring-projects.
the class ResponseCreatorsTests method unauthorized.
@Test
public void unauthorized() throws Exception {
DefaultResponseCreator responseCreator = MockRestResponseCreators.withUnauthorizedRequest();
MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode());
assertTrue(response.getHeaders().isEmpty());
assertNull(response.getBody());
}
use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-framework by spring-projects.
the class ResponseCreatorsTests method success.
@Test
public void success() throws Exception {
MockClientHttpResponse response = (MockClientHttpResponse) MockRestResponseCreators.withSuccess().createResponse(null);
assertEquals(HttpStatus.OK, response.getStatusCode());
assertTrue(response.getHeaders().isEmpty());
assertNull(response.getBody());
}
Aggregations