|
16 | 16 |
|
17 | 17 | package org.springframework.security.oauth2.client.http;
|
18 | 18 |
|
| 19 | +import java.io.IOException; |
| 20 | + |
19 | 21 | import org.junit.Test;
|
20 | 22 |
|
21 | 23 | import org.springframework.http.HttpHeaders;
|
22 | 24 | import org.springframework.http.HttpStatus;
|
| 25 | +import org.springframework.http.client.ClientHttpResponse; |
| 26 | +import org.springframework.mock.http.MockHttpInputMessage; |
23 | 27 | import org.springframework.mock.http.client.MockClientHttpResponse;
|
24 | 28 | import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
|
| 29 | +import org.springframework.web.client.UnknownHttpStatusCodeException; |
25 | 30 |
|
26 | 31 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
27 | 32 |
|
@@ -67,4 +72,48 @@ public void handleErrorWhenErrorResponseWithInvalidWwwAuthenticateHeaderThenHand
|
67 | 72 | .isThrownBy(() -> this.errorHandler.handleError(response)).withMessage("[server_error] ");
|
68 | 73 | }
|
69 | 74 |
|
| 75 | + @Test |
| 76 | + public void handleErrorWhenErrorResponseWithInvalidStatusCodeThenHandled() { |
| 77 | + CustomMockClientHttpResponse response = new CustomMockClientHttpResponse(new byte[0], 596); |
| 78 | + assertThatExceptionOfType(UnknownHttpStatusCodeException.class) |
| 79 | + .isThrownBy(() -> this.errorHandler.handleError(response)).withMessage("596 : [no body]"); |
| 80 | + } |
| 81 | + |
| 82 | + private static final class CustomMockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse { |
| 83 | + |
| 84 | + private final int statusCode; |
| 85 | + |
| 86 | + private CustomMockClientHttpResponse(byte[] content, int statusCode) { |
| 87 | + super(content); |
| 88 | + this.statusCode = statusCode; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public HttpStatus getStatusCode() throws IOException { |
| 93 | + return HttpStatus.valueOf(getRawStatusCode()); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public int getRawStatusCode() { |
| 98 | + return this.statusCode; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String getStatusText() throws IOException { |
| 103 | + HttpStatus httpStatus = HttpStatus.resolve(this.statusCode); |
| 104 | + return (httpStatus != null) ? httpStatus.getReasonPhrase() : ""; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void close() { |
| 109 | + try { |
| 110 | + getBody().close(); |
| 111 | + } |
| 112 | + catch (IOException ex) { |
| 113 | + // ignore |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
70 | 119 | }
|
0 commit comments