Skip to content

Commit 5704582

Browse files
committed
ResponseErrorHandler.handleError(URI, HttpMethod,ClientHttpResponse)
Closes gh-17056
1 parent 3976e7d commit 5704582

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/http/OAuth2ErrorResponseErrorHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
package org.springframework.security.oauth2.client.http;
1818

1919
import java.io.IOException;
20+
import java.net.URI;
2021

2122
import com.nimbusds.oauth2.sdk.token.BearerTokenError;
2223

2324
import org.springframework.http.HttpHeaders;
25+
import org.springframework.http.HttpMethod;
2426
import org.springframework.http.HttpStatus;
2527
import org.springframework.http.client.ClientHttpResponse;
2628
import org.springframework.http.converter.HttpMessageConverter;
@@ -53,9 +55,9 @@ public boolean hasError(ClientHttpResponse response) throws IOException {
5355
}
5456

5557
@Override
56-
public void handleError(ClientHttpResponse response) throws IOException {
58+
public void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
5759
if (HttpStatus.BAD_REQUEST.value() != response.getStatusCode().value()) {
58-
this.defaultErrorHandler.handleError(response);
60+
this.defaultErrorHandler.handleError(url, method, response);
5961
}
6062
// A Bearer Token Error may be in the WWW-Authenticate response header
6163
// See https://tools.ietf.org/html/rfc6750#section-3

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/http/OAuth2ErrorResponseErrorHandlerTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
package org.springframework.security.oauth2.client.http;
1818

1919
import java.io.IOException;
20+
import java.net.URI;
2021

2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.http.HttpHeaders;
25+
import org.springframework.http.HttpMethod;
2426
import org.springframework.http.HttpStatus;
2527
import org.springframework.http.client.ClientHttpResponse;
2628
import org.springframework.http.converter.HttpMessageConverter;
@@ -45,6 +47,10 @@ public class OAuth2ErrorResponseErrorHandlerTests {
4547

4648
private OAuth2ErrorResponseErrorHandler errorHandler = new OAuth2ErrorResponseErrorHandler();
4749

50+
private URI anyURi = URI.create("/any");
51+
52+
private HttpMethod anyMethod = HttpMethod.GET;
53+
4854
@Test
4955
public void handleErrorWhenErrorResponseBodyThenHandled() {
5056
// @formatter:off
@@ -55,7 +61,7 @@ public void handleErrorWhenErrorResponseBodyThenHandled() {
5561
// @formatter:on
5662
MockClientHttpResponse response = new MockClientHttpResponse(errorResponse.getBytes(), HttpStatus.BAD_REQUEST);
5763
assertThatExceptionOfType(OAuth2AuthorizationException.class)
58-
.isThrownBy(() -> this.errorHandler.handleError(response))
64+
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
5965
.withMessage("[unauthorized_client] The client is not authorized");
6066
}
6167

@@ -74,7 +80,7 @@ public void handleErrorWhenOAuth2ErrorConverterSetThenCalled() throws IOExceptio
7480
.willReturn(new OAuth2Error("unauthorized_client", "The client is not authorized", null));
7581

7682
assertThatExceptionOfType(OAuth2AuthorizationException.class)
77-
.isThrownBy(() -> this.errorHandler.handleError(response))
83+
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
7884
.withMessage("[unauthorized_client] The client is not authorized");
7985
verify(oauth2ErrorConverter).read(eq(OAuth2Error.class), eq(response));
8086
}
@@ -85,7 +91,7 @@ public void handleErrorWhenErrorResponseWwwAuthenticateHeaderThenHandled() {
8591
MockClientHttpResponse response = new MockClientHttpResponse(new byte[0], HttpStatus.BAD_REQUEST);
8692
response.getHeaders().add(HttpHeaders.WWW_AUTHENTICATE, wwwAuthenticateHeader);
8793
assertThatExceptionOfType(OAuth2AuthorizationException.class)
88-
.isThrownBy(() -> this.errorHandler.handleError(response))
94+
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
8995
.withMessage("[insufficient_scope] The access token expired");
9096
}
9197

@@ -95,15 +101,15 @@ public void handleErrorWhenErrorResponseWithInvalidWwwAuthenticateHeaderThenHand
95101
MockClientHttpResponse response = new MockClientHttpResponse(new byte[0], HttpStatus.BAD_REQUEST);
96102
response.getHeaders().add(HttpHeaders.WWW_AUTHENTICATE, invalidWwwAuthenticateHeader);
97103
assertThatExceptionOfType(OAuth2AuthorizationException.class)
98-
.isThrownBy(() -> this.errorHandler.handleError(response))
104+
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
99105
.withMessage("[server_error] ");
100106
}
101107

102108
@Test
103109
public void handleErrorWhenErrorResponseWithInvalidStatusCodeThenHandled() {
104110
CustomMockClientHttpResponse response = new CustomMockClientHttpResponse(new byte[0], 596);
105111
assertThatExceptionOfType(IllegalArgumentException.class)
106-
.isThrownBy(() -> this.errorHandler.handleError(response))
112+
.isThrownBy(() -> this.errorHandler.handleError(this.anyURi, this.anyMethod, response))
107113
.withMessage("No matching constant for [596]");
108114
}
109115

0 commit comments

Comments
 (0)