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);
}
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 "));
}
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);
}
});
}
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);
}
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));
}
Aggregations