use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testHandleErrorWhenAccessDeniedMessageAndStatus403ThenThrowsOAuth2AccessDeniedException.
// gh-875
@Test
public void testHandleErrorWhenAccessDeniedMessageAndStatus403ThenThrowsOAuth2AccessDeniedException() throws Exception {
String accessDeniedMessage = "{\"error\":\"access_denied\", \"error_description\":\"some error message\"}";
ByteArrayInputStream messageBody = new ByteArrayInputStream(accessDeniedMessage.getBytes());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
ClientHttpResponse response = new TestClientHttpResponse(headers, 403, messageBody);
expected.expect(OAuth2AccessDeniedException.class);
handler.handleError(response);
}
use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testBodyCanBeUsedByCustomHandler.
@Test
public void testBodyCanBeUsedByCustomHandler() throws Exception {
final String appSpecificBodyContent = "{\"some_status\":\"app error\"}";
OAuth2ErrorHandler handler = new OAuth2ErrorHandler(new ResponseErrorHandler() {
public boolean hasError(ClientHttpResponse response) throws IOException {
return true;
}
public void handleError(ClientHttpResponse response) throws IOException {
InputStream body = response.getBody();
byte[] buf = new byte[appSpecificBodyContent.length()];
int readResponse = body.read(buf);
Assert.assertEquals(buf.length, readResponse);
Assert.assertEquals(appSpecificBodyContent, new String(buf, "UTF-8"));
throw new RuntimeException("planned");
}
}, resource);
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Length", "" + appSpecificBodyContent.length());
headers.set("Content-Type", "application/json");
InputStream appSpecificErrorBody = new ByteArrayInputStream(appSpecificBodyContent.getBytes("UTF-8"));
ClientHttpResponse response = new TestClientHttpResponse(headers, 400, appSpecificErrorBody);
expected.expectMessage("planned");
handler.handleError(response);
}
use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testHandle500Error.
@Test
public void testHandle500Error() throws Exception {
HttpHeaders headers = new HttpHeaders();
ClientHttpResponse response = new TestClientHttpResponse(headers, 500);
expected.expect(HttpServerErrorException.class);
handler.handleError(response);
}
use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testCustomHandler.
@Test
public void testCustomHandler() throws Exception {
OAuth2ErrorHandler handler = new OAuth2ErrorHandler(new ResponseErrorHandler() {
public boolean hasError(ClientHttpResponse response) throws IOException {
return true;
}
public void handleError(ClientHttpResponse response) throws IOException {
throw new RuntimeException("planned");
}
}, resource);
HttpHeaders headers = new HttpHeaders();
ClientHttpResponse response = new TestClientHttpResponse(headers, 401);
expected.expectMessage("planned");
handler.handleError(response);
}
use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class AbstractAuthorizationCodeProviderTests method setupAccessTokenProvider.
@BeforeOAuth2Context
public void setupAccessTokenProvider() {
accessTokenProvider = new AuthorizationCodeAccessTokenProvider() {
private ResponseExtractor<OAuth2AccessToken> extractor = super.getResponseExtractor();
private ResponseExtractor<ResponseEntity<Void>> authExtractor = super.getAuthorizationResponseExtractor();
private ResponseErrorHandler errorHandler = super.getResponseErrorHandler();
@Override
protected ResponseErrorHandler getResponseErrorHandler() {
return new DefaultResponseErrorHandler() {
public void handleError(ClientHttpResponse response) throws IOException {
response.getHeaders();
response.getStatusCode();
tokenEndpointResponse = response;
errorHandler.handleError(response);
}
};
}
@Override
protected ResponseExtractor<OAuth2AccessToken> getResponseExtractor() {
return new ResponseExtractor<OAuth2AccessToken>() {
public OAuth2AccessToken extractData(ClientHttpResponse response) throws IOException {
try {
response.getHeaders();
response.getStatusCode();
tokenEndpointResponse = response;
return extractor.extractData(response);
} catch (ResourceAccessException e) {
return null;
}
}
};
}
@Override
protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
return new ResponseExtractor<ResponseEntity<Void>>() {
public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
response.getHeaders();
response.getStatusCode();
tokenEndpointResponse = response;
return authExtractor.extractData(response);
}
};
}
};
context.setAccessTokenProvider(accessTokenProvider);
}
Aggregations