Java Code Examples for org.springframework.http.client.ClientHttpRequestInterceptor#intercept()

The following examples show how to use org.springframework.http.client.ClientHttpRequestInterceptor#intercept() . 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: SpringVaultClientConfigurationTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
private HttpRequest invokeInterceptors(RestOperations restOperations)
		throws IOException {
	assertThat(restOperations).isInstanceOf(RestTemplate.class);
	RestTemplate restTemplate = (RestTemplate) restOperations;

	MockClientHttpRequest request = new MockClientHttpRequest();
	ClientHttpRequestExecution execution = Mockito
			.mock(ClientHttpRequestExecution.class);
	byte[] body = new byte[] {};
	for (ClientHttpRequestInterceptor interceptor : restTemplate.getInterceptors()) {
		interceptor.intercept(request, body, execution);
	}
	return request;
}
 
Example 2
Source File: HttpBasicAuthenticationSecurityConfigurationUnitTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
@Test
public void securityAwareClientHttpRequestInterceptorSetsHttpRequestHeaders() throws IOException {

	ClientHttpRequestExecution mockExecution = mock(ClientHttpRequestExecution.class);

	Environment mockEnvironment = mock(Environment.class);

	when(mockEnvironment.getProperty(eq("spring.data.gemfire.security.username"))).thenReturn("master");
	when(mockEnvironment.getProperty(eq("spring.data.gemfire.security.password"))).thenReturn("s3cr3t");

	HttpHeaders httpHeaders = new HttpHeaders();

	HttpRequest mockHttpRequest = mock(HttpRequest.class);

	when(mockHttpRequest.getHeaders()).thenReturn(httpHeaders);

	ClientHttpRequestInterceptor interceptor =
		new HttpBasicAuthenticationSecurityConfiguration.SecurityAwareClientHttpRequestInterceptor(mockEnvironment);

	byte[] body = new byte[0];

	interceptor.intercept(mockHttpRequest, body, mockExecution);

	assertThat(httpHeaders.getFirst(GeodeConstants.USERNAME)).isEqualTo("master");
	assertThat(httpHeaders.getFirst(GeodeConstants.PASSWORD)).isEqualTo("s3cr3t");

	verify(mockHttpRequest, times(1)).getHeaders();
	verify(mockExecution, times(1)).execute(eq(mockHttpRequest), eq(body));
}
 
Example 3
Source File: HttpBasicAuthenticationSecurityConfigurationUnitTests.java    From spring-boot-data-geode with Apache License 2.0 3 votes vote down vote up
@Test
public void securityAwareClientHttpRequestInterceptorWillNotSetHttpRequestHeadersWhenCredentialsAreIncomplete()
		throws IOException {

	ClientHttpRequestExecution mockExecution = mock(ClientHttpRequestExecution.class);

	Environment mockEnvironment = mock(Environment.class);

	when(mockEnvironment.getProperty(eq("spring.data.gemfire.security.username"))).thenReturn("master");

	HttpHeaders httpHeaders = new HttpHeaders();

	HttpRequest mockHttpRequest = mock(HttpRequest.class);

	when(mockHttpRequest.getHeaders()).thenReturn(httpHeaders);

	ClientHttpRequestInterceptor interceptor =
		new HttpBasicAuthenticationSecurityConfiguration.SecurityAwareClientHttpRequestInterceptor(mockEnvironment);

	byte[] body = new byte[0];

	interceptor.intercept(mockHttpRequest, body, mockExecution);

	assertThat(httpHeaders.containsKey(GeodeConstants.USERNAME)).isFalse();
	assertThat(httpHeaders.containsKey(GeodeConstants.PASSWORD)).isFalse();

	verify(mockHttpRequest, never()).getHeaders();
	verify(mockExecution, times(1)).execute(eq(mockHttpRequest), eq(body));
}