Search in sources :

Example 11 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project spring-security-oauth by spring-projects.

the class OAuth2RestTemplateTests method testRetryAccessDeniedException.

@Test
public void testRetryAccessDeniedException() throws Exception {
    final AtomicBoolean failed = new AtomicBoolean(false);
    restTemplate.getOAuth2ClientContext().setAccessToken(new DefaultOAuth2AccessToken("TEST"));
    restTemplate.setAccessTokenProvider(new StubAccessTokenProvider());
    restTemplate.setRequestFactory(new ClientHttpRequestFactory() {

        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            if (!failed.get()) {
                failed.set(true);
                throw new AccessTokenRequiredException(resource);
            }
            return request;
        }
    });
    Boolean result = restTemplate.doExecute(new URI("http://foo"), HttpMethod.GET, new NullRequestCallback(), new SimpleResponseExtractor());
    assertTrue(result);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) AccessTokenRequiredException(org.springframework.security.oauth2.client.http.AccessTokenRequiredException) IOException(java.io.IOException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) URI(java.net.URI) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 12 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project spring-security-oauth by spring-projects.

the class OAuth2AccessTokenSupportTests method init.

@Before
public void init() throws Exception {
    resource.setClientId("client");
    resource.setClientSecret("secret");
    resource.setAccessTokenUri("http://nowhere/token");
    response = new StubHttpClientResponse();
    support.setRequestFactory(new ClientHttpRequestFactory() {

        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            return new StubClientHttpRequest(response);
        }
    });
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) IOException(java.io.IOException) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) AbstractClientHttpRequest(org.springframework.http.client.AbstractClientHttpRequest) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Before(org.junit.Before)

Example 13 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project spring-security-oauth by spring-projects.

the class AuthorizationCodeAccessTokenProviderWithConversionTests method testGetErrorFromJson.

@Test
public void testGetErrorFromJson() throws Exception {
    final InvalidClientException exception = new InvalidClientException("FOO");
    requestFactory = new ClientHttpRequestFactory() {

        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            return new StubClientHttpRequest(HttpStatus.BAD_REQUEST, new ObjectMapper().writeValueAsString(exception));
        }
    };
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setAuthorizationCode("foo");
    request.setPreservedState(new Object());
    resource.setAccessTokenUri("http://localhost/oauth/token");
    expected.expect(OAuth2AccessDeniedException.class);
    expected.expect(hasCause(instanceOf(InvalidClientException.class)));
    setUpRestTemplate();
    provider.obtainAccessToken(resource, request);
}
Also used : ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) InvalidClientException(org.springframework.security.oauth2.common.exceptions.InvalidClientException) IOException(java.io.IOException) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 14 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project spring-security-oauth by spring-projects.

the class AuthorizationCodeAccessTokenProviderWithConversionTests method testGetAccessTokenFromForm.

@Test
public void testGetAccessTokenFromForm() throws Exception {
    final OAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    final HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    requestFactory = new ClientHttpRequestFactory() {

        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            return new StubClientHttpRequest(responseHeaders, "access_token=FOO");
        }
    };
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setAuthorizationCode("foo");
    request.setPreservedState(new Object());
    resource.setAccessTokenUri("http://localhost/oauth/token");
    setUpRestTemplate();
    assertEquals(token, provider.obtainAccessToken(resource, request));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) IOException(java.io.IOException) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) HttpMethod(org.springframework.http.HttpMethod) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 15 with ClientHttpRequestFactory

use of org.springframework.http.client.ClientHttpRequestFactory in project spring-security-oauth by spring-projects.

the class AuthorizationCodeAccessTokenProviderWithConversionTests method testGetErrorFromForm.

@Test
public void testGetErrorFromForm() throws Exception {
    final HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    requestFactory = new ClientHttpRequestFactory() {

        public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
            return new StubClientHttpRequest(HttpStatus.BAD_REQUEST, responseHeaders, "error=invalid_client&error_description=FOO");
        }
    };
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setAuthorizationCode("foo");
    request.setPreservedState(new Object());
    resource.setAccessTokenUri("http://localhost/oauth/token");
    expected.expect(OAuth2AccessDeniedException.class);
    expected.expect(hasCause(instanceOf(InvalidClientException.class)));
    setUpRestTemplate();
    provider.obtainAccessToken(resource, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) IOException(java.io.IOException) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Aggregations

ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)23 Test (org.junit.Test)17 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)12 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)12 URI (java.net.URI)11 BufferingClientHttpRequestFactory (org.springframework.http.client.BufferingClientHttpRequestFactory)10 Netty4ClientHttpRequestFactory (org.springframework.http.client.Netty4ClientHttpRequestFactory)10 OkHttp3ClientHttpRequestFactory (org.springframework.http.client.OkHttp3ClientHttpRequestFactory)10 HttpMethod (org.springframework.http.HttpMethod)9 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)9 IOException (java.io.IOException)7 RestTemplate (org.springframework.web.client.RestTemplate)6 HttpHeaders (org.springframework.http.HttpHeaders)4 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)4 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 RequestConfig (org.apache.http.client.config.RequestConfig)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2