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

The following examples show how to use org.springframework.mock.http.client.MockClientHttpResponse. 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
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 7 votes vote down vote up
@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 #2
Source File: SpringRestDataProviderEngineTest.java    From n2o-framework with Apache License 2.0 6 votes vote down vote up
@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 #3
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@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 #5
Source File: ResponseCreatorsTests.java    From spring4-understanding with Apache License 2.0 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());
	assertNull(response.getBody());
}
 
Example #6
Source File: ResourceServerTokenServicesConfigurationTests.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
@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 #7
Source File: ResponseCreatorsTests.java    From spring4-understanding with Apache License 2.0 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());
	assertNull(response.getBody());
}
 
Example #8
Source File: ResponseCreatorsTests.java    From spring4-understanding with Apache License 2.0 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(), FileCopyUtils.copyToByteArray(response.getBody()));
}
 
Example #9
Source File: ResponseCreatorsTests.java    From spring4-understanding with Apache License 2.0 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(), FileCopyUtils.copyToByteArray(response.getBody()));
}
 
Example #10
Source File: ResponseCreatorsTests.java    From spring4-understanding with Apache License 2.0 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());
	assertNull(response.getBody());
}
 
Example #11
Source File: ResponseCreatorsTests.java    From spring4-understanding with Apache License 2.0 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());
	assertNull(response.getBody());
}
 
Example #12
Source File: ResponseCreatorsTests.java    From spring4-understanding with Apache License 2.0 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());
	assertNull(response.getBody());
}
 
Example #13
Source File: ResponseCreatorsTests.java    From spring4-understanding with Apache License 2.0 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());
	assertNull(response.getBody());
}
 
Example #14
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void retryWithDefaultConstructorTest() throws Throwable {
	HttpRequest request = mock(HttpRequest.class);
	when(request.getURI()).thenReturn(new URI("http://default"));
	ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[] {},
			HttpStatus.OK);
	LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
	when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class)))
			.thenReturn(true);
	MyBackOffPolicy backOffPolicy = new MyBackOffPolicy();
	ServiceInstance serviceInstance = mock(ServiceInstance.class);
	when(this.client.choose(eq("default"))).thenReturn(serviceInstance);
	when(this.client.execute(eq("default"), eq(serviceInstance),
			any(LoadBalancerRequest.class))).thenThrow(new IOException())
					.thenReturn(clientHttpResponse);
	this.lbProperties.setEnabled(true);
	when(this.lbRequestFactory.createRequest(any(), any(), any()))
			.thenReturn(mock(LoadBalancerRequest.class));
	RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(
			this.client, this.lbProperties, this.lbRequestFactory,
			new MyLoadBalancedRetryFactory(policy, backOffPolicy));
	byte[] body = new byte[] {};
	ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
	ClientHttpResponse rsp = interceptor.intercept(request, body, execution);
	verify(this.client, times(2)).execute(eq("default"), eq(serviceInstance),
			any(LoadBalancerRequest.class));
	then(rsp).isEqualTo(clientHttpResponse);
	verify(this.lbRequestFactory, times(2)).createRequest(request, body, execution);
	then(backOffPolicy.getBackoffAttempts()).isEqualTo(1);
}
 
Example #15
Source File: ResponseCreatorsTests.java    From spring4-understanding with Apache License 2.0 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());
	assertNull(response.getBody());
}
 
Example #16
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void interceptRetryOnStatusCode() throws Throwable {
	HttpRequest request = mock(HttpRequest.class);
	when(request.getURI()).thenReturn(new URI("http://foo"));
	InputStream notFoundStream = mock(InputStream.class);
	when(notFoundStream.read(any(byte[].class))).thenReturn(-1);
	ClientHttpResponse clientHttpResponseNotFound = new MockClientHttpResponse(
			notFoundStream, HttpStatus.NOT_FOUND);
	ClientHttpResponse clientHttpResponseOk = new MockClientHttpResponse(
			new byte[] {}, HttpStatus.OK);
	LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
	when(policy.retryableStatusCode(eq(HttpStatus.NOT_FOUND.value())))
			.thenReturn(true);
	when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class)))
			.thenReturn(true);
	ServiceInstance serviceInstance = mock(ServiceInstance.class);
	when(this.client.choose(eq("foo"))).thenReturn(serviceInstance);
	when(this.client.execute(eq("foo"), eq(serviceInstance),
			nullable(LoadBalancerRequest.class)))
					.thenReturn(clientHttpResponseNotFound)
					.thenReturn(clientHttpResponseOk);
	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);
	verify(this.client, times(2)).execute(eq("foo"), eq(serviceInstance),
			nullable(LoadBalancerRequest.class));
	verify(notFoundStream, times(1)).close();
	then(rsp).isEqualTo(clientHttpResponseOk);
	verify(this.lbRequestFactory, times(2)).createRequest(request, body, execution);
}
 
Example #17
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void interceptRetryFailOnStatusCode() throws Throwable {
	HttpRequest request = mock(HttpRequest.class);
	when(request.getURI()).thenReturn(new URI("http://foo"));

	InputStream notFoundStream = new ByteArrayInputStream("foo".getBytes());
	ClientHttpResponse clientHttpResponseNotFound = new MockClientHttpResponse(
			notFoundStream, HttpStatus.NOT_FOUND);

	LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
	when(policy.retryableStatusCode(eq(HttpStatus.NOT_FOUND.value())))
			.thenReturn(true);
	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),
			ArgumentMatchers.<LoadBalancerRequest<ClientHttpResponse>>any()))
					.thenReturn(clientHttpResponseNotFound);

	this.lbProperties.setEnabled(true);
	byte[] body = new byte[] {};
	ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
	RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(
			this.client, this.lbProperties, this.lbRequestFactory,
			new MyLoadBalancedRetryFactory(policy));
	ClientHttpResponse rsp = interceptor.intercept(request, body, execution);

	verify(this.client, times(1)).execute(eq("foo"), eq(serviceInstance),
			ArgumentMatchers.<LoadBalancerRequest<ClientHttpResponse>>any());
	verify(this.lbRequestFactory, times(1)).createRequest(request, body, execution);
	verify(policy, times(2)).canRetryNextServer(any(LoadBalancedRetryContext.class));

	// call twice in a retry attempt
	byte[] content = new byte[1024];
	int length = rsp.getBody().read(content);
	then(length).isEqualTo("foo".getBytes().length);
	then(new String(content, 0, length)).isEqualTo("foo");
}
 
Example #18
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void interceptRetry() 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);
	when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class)))
			.thenReturn(true);
	MyBackOffPolicy backOffPolicy = new MyBackOffPolicy();
	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, backOffPolicy));
	byte[] body = new byte[] {};
	ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
	ClientHttpResponse rsp = interceptor.intercept(request, body, execution);
	verify(this.client, times(2)).execute(eq("foo"), eq(serviceInstance),
			any(LoadBalancerRequest.class));
	then(rsp).isEqualTo(clientHttpResponse);
	verify(this.lbRequestFactory, times(2)).createRequest(request, body, execution);
	then(backOffPolicy.getBackoffAttempts()).isEqualTo(1);
}
 
Example #19
Source File: RetryLoadBalancerInterceptorTest.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void retryListenerTest() throws Throwable {
	HttpRequest request = mock(HttpRequest.class);
	when(request.getURI()).thenReturn(new URI("http://listener"));
	ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[] {},
			HttpStatus.OK);
	LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class);
	when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class)))
			.thenReturn(true);
	MyBackOffPolicy backOffPolicy = new MyBackOffPolicy();
	ServiceInstance serviceInstance = mock(ServiceInstance.class);
	when(this.client.choose(eq("listener"))).thenReturn(serviceInstance);
	when(this.client.execute(eq("listener"), eq(serviceInstance),
			any(LoadBalancerRequest.class))).thenThrow(new IOException())
					.thenReturn(clientHttpResponse);
	this.lbProperties.setEnabled(true);
	MyRetryListener retryListener = new MyRetryListener();
	when(this.lbRequestFactory.createRequest(any(), any(), any()))
			.thenReturn(mock(LoadBalancerRequest.class));
	RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(
			this.client, this.lbProperties, this.lbRequestFactory,
			new MyLoadBalancedRetryFactory(policy, backOffPolicy,
					new RetryListener[] { retryListener }));
	byte[] body = new byte[] {};
	ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class);
	ClientHttpResponse rsp = interceptor.intercept(request, body, execution);
	verify(this.client, times(2)).execute(eq("listener"), eq(serviceInstance),
			any(LoadBalancerRequest.class));
	then(rsp).isEqualTo(clientHttpResponse);
	verify(this.lbRequestFactory, times(2)).createRequest(request, body, execution);
	then(backOffPolicy.getBackoffAttempts()).isEqualTo(1);
	then(retryListener.getOnError()).isEqualTo(1);
}
 
Example #20
Source File: ResponseCreatorsTests.java    From spring-analysis-note with 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 #21
Source File: ResponseCreatorsTests.java    From java-technology-stack with 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 #22
Source File: ResponseCreatorsTests.java    From spring-analysis-note with 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 #23
Source File: ResponseCreatorsTests.java    From spring-analysis-note with 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 #24
Source File: ResponseCreatorsTests.java    From spring-analysis-note with 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 #25
Source File: ResponseCreatorsTests.java    From spring-analysis-note with 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 #26
Source File: ResponseCreatorsTests.java    From spring-analysis-note with 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 #27
Source File: ResponseCreatorsTests.java    From spring-analysis-note with 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 #28
Source File: ResponseCreatorsTests.java    From spring-analysis-note with 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 #29
Source File: ResponseCreatorsTests.java    From spring-analysis-note with 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 #30
Source File: ResponseCreatorsTests.java    From java-technology-stack with 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()));
}