Search in sources :

Example 6 with MockClientHttpResponse

use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-framework by spring-projects.

the class ResponseCreatorsTests method successWithContentWithoutContentType.

@Test
public void successWithContentWithoutContentType() throws Exception {
    DefaultResponseCreator responseCreator = MockRestResponseCreators.withSuccess("foo", null);
    MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertNull(response.getHeaders().getContentType());
    assertArrayEquals("foo".getBytes(), FileCopyUtils.copyToByteArray(response.getBody()));
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.Test)

Example 7 with MockClientHttpResponse

use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-boot by spring-projects.

the class TestRestTemplateTests method verifyRelativeUriHandling.

private void verifyRelativeUriHandling(TestRestTemplateCallback callback) throws IOException {
    ClientHttpRequestFactory requestFactory = mock(ClientHttpRequestFactory.class);
    MockClientHttpRequest request = new MockClientHttpRequest();
    request.setResponse(new MockClientHttpResponse(new byte[0], HttpStatus.OK));
    URI absoluteUri = URI.create("http://localhost:8080/a/b/c.txt?param=%7Bsomething%7D");
    given(requestFactory.createRequest(eq(absoluteUri), (HttpMethod) any())).willReturn(request);
    RestTemplate delegate = new RestTemplate();
    TestRestTemplate template = new TestRestTemplate(delegate);
    delegate.setRequestFactory(requestFactory);
    LocalHostUriTemplateHandler uriTemplateHandler = new LocalHostUriTemplateHandler(new MockEnvironment());
    template.setUriTemplateHandler(uriTemplateHandler);
    callback.doWithTestRestTemplate(template, URI.create("/a/b/c.txt?param=%7Bsomething%7D"));
    verify(requestFactory).createRequest(eq(absoluteUri), (HttpMethod) any());
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) InterceptingClientHttpRequestFactory(org.springframework.http.client.InterceptingClientHttpRequestFactory) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) CustomHttpComponentsClientHttpRequestFactory(org.springframework.boot.test.web.client.TestRestTemplate.CustomHttpComponentsClientHttpRequestFactory) MockEnvironment(org.springframework.mock.env.MockEnvironment) RestTemplate(org.springframework.web.client.RestTemplate) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) URI(java.net.URI) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

Example 8 with MockClientHttpResponse

use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-framework by spring-projects.

the class DefaultResponseCreator method createResponse.

@Override
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
    MockClientHttpResponse response;
    if (this.contentResource != null) {
        InputStream stream = this.contentResource.getInputStream();
        response = new MockClientHttpResponse(stream, this.statusCode);
    } else {
        response = new MockClientHttpResponse(this.content, this.statusCode);
    }
    response.getHeaders().putAll(this.headers);
    return response;
}
Also used : InputStream(java.io.InputStream) MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse)

Example 9 with MockClientHttpResponse

use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-framework by spring-projects.

the class ResponseCreatorsTests method successWithContent.

@Test
public void successWithContent() throws Exception {
    DefaultResponseCreator responseCreator = MockRestResponseCreators.withSuccess("foo", MediaType.TEXT_PLAIN);
    MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(MediaType.TEXT_PLAIN, response.getHeaders().getContentType());
    assertArrayEquals("foo".getBytes(), FileCopyUtils.copyToByteArray(response.getBody()));
}
Also used : MockClientHttpResponse(org.springframework.mock.http.client.MockClientHttpResponse) Test(org.junit.Test)

Example 10 with MockClientHttpResponse

use of org.springframework.mock.http.client.MockClientHttpResponse in project spring-framework by spring-projects.

the class ResponseCreatorsTests method serverError.

@Test
public void serverError() throws Exception {
    DefaultResponseCreator responseCreator = MockRestResponseCreators.withServerError();
    MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);
    assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, 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