Search in sources :

Example 41 with ClientHttpResponse

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 42 with ClientHttpResponse

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 43 with ClientHttpResponse

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 44 with ClientHttpResponse

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);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MediaType(org.springframework.http.MediaType) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 45 with ClientHttpResponse

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Aggregations

ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)71 Test (org.junit.Test)35 IOException (java.io.IOException)25 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)21 HttpHeaders (org.springframework.http.HttpHeaders)18 URI (java.net.URI)17 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)15 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)14 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)11 RestTemplate (org.springframework.web.client.RestTemplate)11 ResponseExtractor (org.springframework.web.client.ResponseExtractor)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)7 ServiceError (com.kixeye.chassis.transport.dto.ServiceError)6 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)6 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)6 JsonJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe)6 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)6 XmlMessageSerDe (com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe)6 YamlJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe)6