Search in sources :

Example 6 with ClientHttpRequestInterceptor

use of org.springframework.http.client.ClientHttpRequestInterceptor in project uPortal by Jasig.

the class BasicAuthInterceptorTest method doInterceptorTest.

private void doInterceptorTest(PropertyResolver resolver, String id, String expectedAuthCode) throws Exception {
    final String url = "http://www.test.com/lrs";
    final String data = "test";
    final String expectedHeader = "Basic " + expectedAuthCode;
    // holder for the headers...
    HttpHeaders headers = new HttpHeaders();
    // Mock guts of RestTemplate so no need to actually hit the web...
    ClientHttpResponse resp = mock(ClientHttpResponse.class);
    when(resp.getStatusCode()).thenReturn(HttpStatus.ACCEPTED);
    when(resp.getHeaders()).thenReturn(new HttpHeaders());
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ClientHttpRequest client = mock(ClientHttpRequest.class);
    when(client.getHeaders()).thenReturn(headers);
    when(client.getBody()).thenReturn(buffer);
    when(client.execute()).thenReturn(resp);
    ClientHttpRequestFactory factory = mock(ClientHttpRequestFactory.class);
    when(factory.createRequest(any(URI.class), any(HttpMethod.class))).thenReturn(client);
    // add the new interceptor...
    BasicAuthInterceptor interceptor = new BasicAuthInterceptor();
    interceptor.setPropertyResolver(resolver);
    interceptor.setId(id);
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
    interceptors.add(interceptor);
    RestTemplate rest = new RestTemplate(factory);
    rest.setInterceptors(interceptors);
    // do it...
    rest.postForLocation(url, data, Collections.emptyMap());
    // make sure auth header is correctly set...
    assertThat(headers, hasKey(Headers.Authorization.name()));
    assertThat(headers.get(Headers.Authorization.name()), contains(expectedHeader));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) RestTemplate(org.springframework.web.client.RestTemplate) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HttpMethod(org.springframework.http.HttpMethod)

Example 7 with ClientHttpRequestInterceptor

use of org.springframework.http.client.ClientHttpRequestInterceptor in project uPortal by Jasig.

the class ZeroLeggedOAuthInterceptorTest method testInterceptor.

@Test
public void testInterceptor() throws Exception {
    final String url = "http://www.test.com/lrs?param1=val1&param2=val2";
    final String data = "test";
    final String id = "test";
    final String realm = "realm";
    final String consumerKey = "consumerKey";
    final String secretKey = "secretKey";
    PropertyResolver resolver = mock(PropertyResolver.class);
    when(resolver.getProperty(Matchers.eq("org.jasig.rest.interceptor.oauth." + id + ".realm"))).thenReturn(realm);
    when(resolver.getProperty(Matchers.eq("org.jasig.rest.interceptor.oauth." + id + ".consumerKey"))).thenReturn(consumerKey);
    when(resolver.getProperty(Matchers.eq("org.jasig.rest.interceptor.oauth." + id + ".secretKey"))).thenReturn(secretKey);
    // holder for the headers...
    HttpHeaders headers = new HttpHeaders();
    // Mock guts of RestTemplate so no need to actually hit the web...
    ClientHttpResponse resp = mock(ClientHttpResponse.class);
    when(resp.getStatusCode()).thenReturn(HttpStatus.ACCEPTED);
    when(resp.getHeaders()).thenReturn(new HttpHeaders());
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ClientHttpRequest client = mock(ClientHttpRequest.class);
    when(client.getHeaders()).thenReturn(headers);
    when(client.getBody()).thenReturn(buffer);
    when(client.execute()).thenReturn(resp);
    ClientHttpRequestFactory factory = mock(ClientHttpRequestFactory.class);
    when(factory.createRequest(Matchers.any(URI.class), Matchers.any(HttpMethod.class))).thenReturn(client);
    // add the new interceptor...
    ZeroLeggedOAuthInterceptor interceptor = new ZeroLeggedOAuthInterceptor();
    interceptor.setPropertyResolver(resolver);
    interceptor.setId(id);
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
    interceptors.add(interceptor);
    RestTemplate rest = new RestTemplate(factory);
    rest.setInterceptors(interceptors);
    rest.postForLocation(url, data, Collections.emptyMap());
    // make sure auth header is correctly set...
    assertThat(headers, hasKey(Headers.Authorization.name()));
    String authHeader = headers.get(Headers.Authorization.name()).get(0);
    assertThat(authHeader, containsString("OAuth realm=\"" + realm + "\""));
    assertThat(authHeader, containsString("oauth_consumer_key=\"" + consumerKey + "\""));
    // for now, only supports HMAC-SHA1.  May have to fix later...
    assertThat(authHeader, containsString("oauth_signature_method=\"HMAC-SHA1\""));
    assertThat(authHeader, containsString("oauth_version=\"1.0\""));
    assertThat(authHeader, containsString("oauth_timestamp="));
    assertThat(authHeader, containsString("oauth_nonce="));
    assertThat(authHeader, containsString("oauth_signature="));
    // oauth lib will create 2 oauth_signature params if you call sign
    // multiple times.  Make sure only get 1.
    assertThat(StringUtils.countMatches(authHeader, "oauth_signature="), is(1));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PropertyResolver(org.springframework.core.env.PropertyResolver) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) RestTemplate(org.springframework.web.client.RestTemplate) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 8 with ClientHttpRequestInterceptor

use of org.springframework.http.client.ClientHttpRequestInterceptor in project sic by belluccifranco.

the class FacturacionIntegrationTest method setup.

@Before
public void setup() {
    String md5Test = "098f6bcd4621d373cade4e832627b4f6";
    usuarioRepository.save(new UsuarioBuilder().withNombre("test").withPassword(md5Test).build());
    // Interceptor de RestTemplate para JWT
    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add((ClientHttpRequestInterceptor) (HttpRequest request, byte[] body, ClientHttpRequestExecution execution) -> {
        request.getHeaders().set("Authorization", "Bearer " + token);
        return execution.execute(request, body);
    });
    restTemplate.getRestTemplate().setInterceptors(interceptors);
    // ErrorHandler para RestTemplate        
    restTemplate.getRestTemplate().setErrorHandler(new ResponseErrorHandler() {

        @Override
        public boolean hasError(ClientHttpResponse response) throws IOException {
            HttpStatus.Series series = response.getStatusCode().series();
            return (HttpStatus.Series.CLIENT_ERROR.equals(series) || HttpStatus.Series.SERVER_ERROR.equals(series));
        }

        @Override
        public void handleError(ClientHttpResponse response) throws IOException {
            String mensaje = IOUtils.toString(response.getBody());
            throw new RestClientResponseException(mensaje, response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), null, Charset.defaultCharset());
        }
    });
}
Also used : HttpRequest(org.springframework.http.HttpRequest) ResponseErrorHandler(org.springframework.web.client.ResponseErrorHandler) ArrayList(java.util.ArrayList) ClientHttpRequestExecution(org.springframework.http.client.ClientHttpRequestExecution) IOException(java.io.IOException) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) UsuarioBuilder(sic.builder.UsuarioBuilder) RestClientResponseException(org.springframework.web.client.RestClientResponseException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Before(org.junit.Before)

Example 9 with ClientHttpRequestInterceptor

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

the class RestTemplateBuilderTests method additionalInterceptorsShouldAddToExisting.

@Test
public void additionalInterceptorsShouldAddToExisting() throws Exception {
    ClientHttpRequestInterceptor interceptor = mock(ClientHttpRequestInterceptor.class);
    RestTemplate template = this.builder.interceptors(interceptor).additionalInterceptors(this.interceptor).build();
    assertThat(template.getInterceptors()).containsOnly(interceptor, this.interceptor);
}
Also used : RestTemplate(org.springframework.web.client.RestTemplate) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor) Test(org.junit.Test)

Example 10 with ClientHttpRequestInterceptor

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

the class TestRestTemplateTests method assertBasicAuthorizationInterceptorCredentials.

private void assertBasicAuthorizationInterceptorCredentials(TestRestTemplate testRestTemplate, String username, String password) {
    @SuppressWarnings("unchecked") List<ClientHttpRequestInterceptor> requestFactoryInterceptors = (List<ClientHttpRequestInterceptor>) ReflectionTestUtils.getField(testRestTemplate.getRestTemplate().getRequestFactory(), "interceptors");
    assertThat(requestFactoryInterceptors).hasSize(1);
    ClientHttpRequestInterceptor interceptor = requestFactoryInterceptors.get(0);
    assertThat(interceptor).isInstanceOf(BasicAuthorizationInterceptor.class);
    assertThat(ReflectionTestUtils.getField(interceptor, "username")).isEqualTo(username);
    assertThat(ReflectionTestUtils.getField(interceptor, "password")).isEqualTo(password);
}
Also used : List(java.util.List) ClientHttpRequestInterceptor(org.springframework.http.client.ClientHttpRequestInterceptor)

Aggregations

ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)10 RestTemplate (org.springframework.web.client.RestTemplate)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)4 HttpRequest (org.springframework.http.HttpRequest)3 ClientHttpRequestExecution (org.springframework.http.client.ClientHttpRequestExecution)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 URI (java.net.URI)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMethod (org.springframework.http.HttpMethod)2 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)2 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)2 Account (android.accounts.Account)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Before (org.junit.Before)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1