Search in sources :

Example 21 with ClientHttpRequest

use of org.springframework.http.client.ClientHttpRequest 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 22 with ClientHttpRequest

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

the class OAuth2RestTemplateTests method testNonBearerToken.

@Test
public void testNonBearerToken() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("12345");
    token.setTokenType("MINE");
    restTemplate.getOAuth2ClientContext().setAccessToken(token);
    ClientHttpRequest http = restTemplate.createRequest(URI.create("https://nowhere.com/api/crap"), HttpMethod.GET);
    String auth = http.getHeaders().getFirst("Authorization");
    assertTrue(auth.startsWith("MINE "));
}
Also used : ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Test(org.junit.Test)

Example 23 with ClientHttpRequest

use of org.springframework.http.client.ClientHttpRequest 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 24 with ClientHttpRequest

use of org.springframework.http.client.ClientHttpRequest 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 25 with ClientHttpRequest

use of org.springframework.http.client.ClientHttpRequest 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)

Aggregations

ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)36 URI (java.net.URI)19 Test (org.junit.Test)13 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)13 IOException (java.io.IOException)11 HttpMethod (org.springframework.http.HttpMethod)9 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)9 HttpHeaders (org.springframework.http.HttpHeaders)6 MockClientHttpRequest (org.springframework.mock.http.client.MockClientHttpRequest)6 RequestMatcher (org.springframework.test.web.client.RequestMatcher)6 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)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)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 MediaType (org.springframework.http.MediaType)2 ClientHttpRequestInterceptor (org.springframework.http.client.ClientHttpRequestInterceptor)2 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)2