Java Code Examples for org.springframework.mock.http.client.MockClientHttpResponse

The following examples show how to use org.springframework.mock.http.client.MockClientHttpResponse. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void testFails() {
    TestRestTemplate restTemplate = new TestRestTemplate(new MockClientHttpResponse("Error".getBytes(StandardCharsets.UTF_8), HttpStatus.INTERNAL_SERVER_ERROR));
    SpringRestDataProviderEngine actionEngine = new SpringRestDataProviderEngine(restTemplate, new ObjectMapper());
    N2oRestDataProvider invocation = new N2oRestDataProvider();
    invocation.setQuery("http://www.example.org/");
    invocation.setMethod(N2oRestDataProvider.Method.GET);
    Map<String, Object> request = new HashMap<>();

    try {
        actionEngine.invoke(invocation, request);
        Assert.fail();
    } catch (HttpStatusCodeException e) {
        assertThat(e.getStatusCode(), is(HttpStatus.INTERNAL_SERVER_ERROR));
        assertThat(e.getResponseBodyAsString(), is("Error"));
    }
}
 
Example 2
@Test
public void interceptNeverRetry() throws Throwable {
	HttpRequest request = mock(HttpRequest.class);
	when(request.getURI()).thenReturn(new URI("http://foo"));
	ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[] {},
			HttpStatus.OK);
	ServiceInstance serviceInstance = mock(ServiceInstance.class);
	when(this.client.choose(eq("foo"))).thenReturn(serviceInstance);
	when(this.client.execute(eq("foo"), eq(serviceInstance),
			any(LoadBalancerRequest.class))).thenReturn(clientHttpResponse);
	when(this.lbRequestFactory.createRequest(any(), any(), any()))
			.thenReturn(mock(LoadBalancerRequest.class));
	this.lbProperties.setEnabled(true);
	RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(
			this.client, this.lbProperties, this.lbRequestFactory,
			this.loadBalancedRetryFactory);
	byte[] body = new byte[] {};
	ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
	interceptor.intercept(request, body, execution);
	verify(this.lbRequestFactory).createRequest(request, body, execution);
}
 
Example 3
@Test
public void interceptSuccess() throws Throwable {
	HttpRequest request = mock(HttpRequest.class);
	when(request.getURI()).thenReturn(new URI("http://foo"));
	ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[] {},
			HttpStatus.OK);
	LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
	ServiceInstance serviceInstance = mock(ServiceInstance.class);
	when(this.client.choose(eq("foo"))).thenReturn(serviceInstance);
	when(this.client.execute(eq("foo"), eq(serviceInstance),
			any(LoadBalancerRequest.class))).thenReturn(clientHttpResponse);
	when(this.lbRequestFactory.createRequest(any(), any(), any()))
			.thenReturn(mock(LoadBalancerRequest.class));
	this.lbProperties.setEnabled(true);
	RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(
			this.client, this.lbProperties, this.lbRequestFactory,
			new MyLoadBalancedRetryFactory(policy));
	byte[] body = new byte[] {};
	ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
	ClientHttpResponse rsp = interceptor.intercept(request, body, execution);
	then(rsp).isEqualTo(clientHttpResponse);
	verify(this.lbRequestFactory).createRequest(request, body, execution);
}
 
Example 4
@Test(expected = IOException.class)
public void interceptFailedRetry() throws Exception {
	HttpRequest request = mock(HttpRequest.class);
	when(request.getURI()).thenReturn(new URI("http://foo"));
	ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[] {},
			HttpStatus.OK);
	LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
	when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class)))
			.thenReturn(false);
	ServiceInstance serviceInstance = mock(ServiceInstance.class);
	when(this.client.choose(eq("foo"))).thenReturn(serviceInstance);
	when(this.client.execute(eq("foo"), eq(serviceInstance),
			any(LoadBalancerRequest.class))).thenThrow(new IOException())
					.thenReturn(clientHttpResponse);
	when(this.lbRequestFactory.createRequest(any(), any(), any()))
			.thenReturn(mock(LoadBalancerRequest.class));
	this.lbProperties.setEnabled(true);
	RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(
			this.client, this.lbProperties, this.lbRequestFactory,
			new MyLoadBalancedRetryFactory(policy));
	byte[] body = new byte[] {};
	ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
	interceptor.intercept(request, body, execution);
	verify(this.lbRequestFactory).createRequest(request, body, execution);
}
 
Example 5
Source Project: spring-analysis-note   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void success() throws Exception {
	MockClientHttpResponse response = (MockClientHttpResponse) MockRestResponseCreators.withSuccess().createResponse(null);

	assertEquals(HttpStatus.OK, response.getStatusCode());
	assertTrue(response.getHeaders().isEmpty());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 6
Source Project: spring-analysis-note   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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(), StreamUtils.copyToByteArray(response.getBody()));
}
 
Example 7
Source Project: spring-analysis-note   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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(), StreamUtils.copyToByteArray(response.getBody()));
}
 
Example 8
Source Project: spring-analysis-note   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 9
Source Project: spring-analysis-note   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 10
Source Project: spring-analysis-note   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void badRequest() throws Exception {
	DefaultResponseCreator responseCreator = MockRestResponseCreators.withBadRequest();
	MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

	assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
	assertTrue(response.getHeaders().isEmpty());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 11
Source Project: spring-analysis-note   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 12
Source Project: spring-analysis-note   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 13
Source Project: spring-analysis-note   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void withStatus() throws Exception {
	DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(HttpStatus.FORBIDDEN);
	MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

	assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
	assertTrue(response.getHeaders().isEmpty());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 14
Source Project: java-technology-stack   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void success() throws Exception {
	MockClientHttpResponse response = (MockClientHttpResponse) MockRestResponseCreators.withSuccess().createResponse(null);

	assertEquals(HttpStatus.OK, response.getStatusCode());
	assertTrue(response.getHeaders().isEmpty());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 15
Source Project: java-technology-stack   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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(), StreamUtils.copyToByteArray(response.getBody()));
}
 
Example 16
Source Project: java-technology-stack   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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(), StreamUtils.copyToByteArray(response.getBody()));
}
 
Example 17
Source Project: java-technology-stack   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 18
Source Project: java-technology-stack   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 19
Source Project: java-technology-stack   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void badRequest() throws Exception {
	DefaultResponseCreator responseCreator = MockRestResponseCreators.withBadRequest();
	MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

	assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
	assertTrue(response.getHeaders().isEmpty());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 20
Source Project: java-technology-stack   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 21
Source Project: java-technology-stack   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@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());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 22
Source Project: java-technology-stack   Source File: ResponseCreatorsTests.java    License: MIT License 5 votes vote down vote up
@Test
public void withStatus() throws Exception {
	DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(HttpStatus.FORBIDDEN);
	MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

	assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
	assertTrue(response.getHeaders().isEmpty());
	assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length);
}
 
Example 23
@Override
public void customize(RestTemplate template) {
	template.getInterceptors().add((request, body, execution) -> {
		String payload = "{\"value\":\"FOO\"}";
		MockClientHttpResponse response = new MockClientHttpResponse(payload.getBytes(), HttpStatus.OK);
		response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
		return response;
	});
}
 
Example 24
@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());
}
 
Example 25
@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()));
}
 
Example 26
@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()));
}
 
Example 27
@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());
}
 
Example 28
@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());
}
 
Example 29
@Test
public void badRequest() throws Exception {
	DefaultResponseCreator responseCreator = MockRestResponseCreators.withBadRequest();
	MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null);

	assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
	assertTrue(response.getHeaders().isEmpty());
	assertNull(response.getBody());
}
 
Example 30
@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());
}