Search in sources :

Example 76 with HttpHeaders

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

Example 77 with HttpHeaders

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

Example 78 with HttpHeaders

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

Example 79 with HttpHeaders

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

Example 80 with HttpHeaders

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

the class AbstractAuthorizationCodeProviderTests method getAuthenticatedHeaders.

private HttpHeaders getAuthenticatedHeaders() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    headers.set("Authorization", "Basic " + new String(Base64.encode("user:password".getBytes())));
    if (context.getRestTemplate() != null) {
        context.getAccessTokenRequest().setHeaders(headers);
    }
    return headers;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders)

Aggregations

HttpHeaders (org.springframework.http.HttpHeaders)1676 Test (org.junit.Test)426 ResponseEntity (org.springframework.http.ResponseEntity)383 HttpEntity (org.springframework.http.HttpEntity)345 Test (org.junit.jupiter.api.Test)273 HashMap (java.util.HashMap)184 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)154 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)127 MediaType (org.springframework.http.MediaType)121 URI (java.net.URI)111 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)102 Map (java.util.Map)97 IOException (java.io.IOException)83 RestTemplate (org.springframework.web.client.RestTemplate)78 ArrayList (java.util.ArrayList)75 MessageHeaders (org.springframework.messaging.MessageHeaders)74 MultiValueMap (org.springframework.util.MultiValueMap)74 HttpStatus (org.springframework.http.HttpStatus)71 List (java.util.List)65 Timed (com.codahale.metrics.annotation.Timed)54