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()));
}
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());
}
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;
}
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()));
}
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());
}
Aggregations