Search in sources :

Example 1 with MockClientHttpResponse

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);
            }
        }
    };
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) IOException(java.io.IOException) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

Example 2 with MockClientHttpResponse

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());
}
Also used : URI(java.net.URI) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.Test)

Example 3 with MockClientHttpResponse

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());
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.Test)

Example 4 with MockClientHttpResponse

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());
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.Test)

Example 5 with MockClientHttpResponse

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());
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.Test)

Aggregations

MockClientHttpResponse (org.springframework.mock.http.client.MockClientHttpResponse)12 Test (org.junit.Test)9 URI (java.net.URI)2 MockClientHttpRequest (org.springframework.mock.http.client.MockClientHttpRequest)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CustomHttpComponentsClientHttpRequestFactory (org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)1 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)1 InterceptingClientHttpRequestFactory (org.springframework.http.client.InterceptingClientHttpRequestFactory)1 MockEnvironment (org.springframework.mock.env.MockEnvironment)1 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)1 RestTemplate (org.springframework.web.client.RestTemplate)1