use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testHandleErrorClientHttpResponse.
/**
* test response with www-authenticate header
*/
@Test
public void testHandleErrorClientHttpResponse() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.set("www-authenticate", "Bearer error=foo");
ClientHttpResponse response = new TestClientHttpResponse(headers, 401);
// We lose the www-authenticate content in a nested exception (but it's still available) through the
// HttpClientErrorException
expected.expectMessage("401 Unauthorized");
handler.handleError(response);
}
use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testHandleGeneric403ErrorWithBody.
@Test
public // See https://github.com/spring-projects/spring-security-oauth/issues/387
void testHandleGeneric403ErrorWithBody() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
ClientHttpResponse response = new TestClientHttpResponse(headers, 403, new ByteArrayInputStream("{}".getBytes()));
handler = new OAuth2ErrorHandler(new DefaultResponseErrorHandler(), resource);
expected.expect(HttpClientErrorException.class);
handler.handleError(response);
}
use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testHandleGeneric400Error.
@Test
public void testHandleGeneric400Error() throws Exception {
HttpHeaders headers = new HttpHeaders();
ClientHttpResponse response = new TestClientHttpResponse(headers, 400);
expected.expect(HttpClientErrorException.class);
handler.handleError(response);
}
use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testHandleMessageConversionExceptions.
@Test
public void testHandleMessageConversionExceptions() throws Exception {
HttpMessageConverter<?> extractor = new HttpMessageConverter() {
@Override
public boolean canRead(Class clazz, MediaType mediaType) {
return true;
}
@Override
public boolean canWrite(Class clazz, MediaType mediaType) {
return false;
}
@Override
public List<MediaType> getSupportedMediaTypes() {
return null;
}
@Override
public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
throw new HttpMessageConversionException("error");
}
@Override
public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
}
};
ArrayList<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(extractor);
handler.setMessageConverters(messageConverters);
HttpHeaders headers = new HttpHeaders();
final String appSpecificBodyContent = "This user is not authorized";
InputStream appSpecificErrorBody = new ByteArrayInputStream(appSpecificBodyContent.getBytes("UTF-8"));
ClientHttpResponse response = new TestClientHttpResponse(headers, 401, appSpecificErrorBody);
expected.expect(HttpClientErrorException.class);
handler.handleError(response);
}
use of org.springframework.http.client.ClientHttpResponse in project spring-security-oauth by spring-projects.
the class OAuth2ErrorHandlerTests method testHandleErrorWhenAccessDeniedMessageAndStatus400ThenThrowsUserDeniedAuthorizationException.
// gh-875
@Test
public void testHandleErrorWhenAccessDeniedMessageAndStatus400ThenThrowsUserDeniedAuthorizationException() 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, 400, messageBody);
expected.expect(UserDeniedAuthorizationException.class);
handler.handleError(response);
}
Aggregations