Skip to content

Commit 2227232

Browse files
author
Steve Riesenberg
committed
Handle custom status codes in error handler
Fixes an issue where custom status codes in the error response cause an IllegalArgumentException to be thrown when resolving an HttpStatus. Closes gh-9741
1 parent a4216d0 commit 2227232

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public boolean hasError(ClientHttpResponse response) throws IOException {
4848

4949
@Override
5050
public void handleError(ClientHttpResponse response) throws IOException {
51-
if (!HttpStatus.BAD_REQUEST.equals(response.getStatusCode())) {
51+
if (HttpStatus.BAD_REQUEST.value() != response.getRawStatusCode()) {
5252
this.defaultErrorHandler.handleError(response);
5353
}
5454

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@
1515
*/
1616
package org.springframework.security.oauth2.client.http;
1717

18+
import java.io.IOException;
19+
1820
import org.junit.Test;
21+
1922
import org.springframework.http.HttpHeaders;
2023
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.client.ClientHttpResponse;
25+
import org.springframework.mock.http.MockHttpInputMessage;
2126
import org.springframework.mock.http.client.MockClientHttpResponse;
2227
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
28+
import org.springframework.web.client.UnknownHttpStatusCodeException;
2329

30+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2431
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2532

2633
/**
@@ -58,4 +65,49 @@ public void handleErrorWhenErrorResponseWwwAuthenticateHeaderThenHandled() {
5865
.isInstanceOf(OAuth2AuthorizationException.class)
5966
.hasMessage("[insufficient_scope] The access token expired");
6067
}
68+
69+
@Test
70+
public void handleErrorWhenErrorResponseWithInvalidStatusCodeThenHandled() {
71+
CustomMockClientHttpResponse response = new CustomMockClientHttpResponse(new byte[0], 596);
72+
assertThatExceptionOfType(UnknownHttpStatusCodeException.class)
73+
.isThrownBy(() -> this.errorHandler.handleError(response)).withMessage("596 : [no body]");
74+
}
75+
76+
private static final class CustomMockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
77+
78+
private final int statusCode;
79+
80+
private CustomMockClientHttpResponse(byte[] content, int statusCode) {
81+
super(content);
82+
this.statusCode = statusCode;
83+
}
84+
85+
@Override
86+
public HttpStatus getStatusCode() throws IOException {
87+
return HttpStatus.valueOf(getRawStatusCode());
88+
}
89+
90+
@Override
91+
public int getRawStatusCode() {
92+
return this.statusCode;
93+
}
94+
95+
@Override
96+
public String getStatusText() throws IOException {
97+
HttpStatus httpStatus = HttpStatus.resolve(this.statusCode);
98+
return (httpStatus != null) ? httpStatus.getReasonPhrase() : "";
99+
}
100+
101+
@Override
102+
public void close() {
103+
try {
104+
getBody().close();
105+
}
106+
catch (IOException ex) {
107+
// ignore
108+
}
109+
}
110+
111+
}
112+
61113
}

0 commit comments

Comments
 (0)