|
15 | 15 | */
|
16 | 16 | package org.springframework.security.oauth2.client.http;
|
17 | 17 |
|
| 18 | +import java.io.IOException; |
| 19 | + |
18 | 20 | import org.junit.Test;
|
| 21 | + |
19 | 22 | import org.springframework.http.HttpHeaders;
|
20 | 23 | import org.springframework.http.HttpStatus;
|
| 24 | +import org.springframework.http.client.ClientHttpResponse; |
| 25 | +import org.springframework.mock.http.MockHttpInputMessage; |
21 | 26 | import org.springframework.mock.http.client.MockClientHttpResponse;
|
22 | 27 | import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
|
| 28 | +import org.springframework.web.client.UnknownHttpStatusCodeException; |
23 | 29 |
|
| 30 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
24 | 31 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
25 | 32 |
|
26 | 33 | /**
|
@@ -58,4 +65,49 @@ public void handleErrorWhenErrorResponseWwwAuthenticateHeaderThenHandled() {
|
58 | 65 | .isInstanceOf(OAuth2AuthorizationException.class)
|
59 | 66 | .hasMessage("[insufficient_scope] The access token expired");
|
60 | 67 | }
|
| 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 | + |
61 | 113 | }
|
0 commit comments