|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2017 the original author or authors. |
| 2 | + * Copyright 2002-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.io.ByteArrayInputStream;
|
20 | 20 | import java.io.IOException;
|
| 21 | +import java.nio.charset.StandardCharsets; |
21 | 22 |
|
22 | 23 | import org.junit.Test;
|
23 | 24 |
|
24 | 25 | import org.springframework.http.HttpHeaders;
|
25 | 26 | import org.springframework.http.HttpStatus;
|
26 | 27 | import org.springframework.http.MediaType;
|
27 | 28 | import org.springframework.http.client.ClientHttpResponse;
|
| 29 | +import org.springframework.util.StreamUtils; |
28 | 30 |
|
29 | 31 | import static org.junit.Assert.*;
|
30 | 32 | import static org.mockito.BDDMockito.*;
|
|
33 | 35 | * Unit tests for {@link DefaultResponseErrorHandler}.
|
34 | 36 | *
|
35 | 37 | * @author Arjen Poutsma
|
| 38 | + * @author Juergen Hoeller |
| 39 | + * @author Denys Ivano |
36 | 40 | */
|
37 | 41 | public class DefaultResponseErrorHandlerTests {
|
38 | 42 |
|
@@ -121,4 +125,56 @@ public void hasErrorForUnknownStatusCode() throws Exception {
|
121 | 125 | assertFalse(handler.hasError(response));
|
122 | 126 | }
|
123 | 127 |
|
| 128 | + @Test // SPR-16604 |
| 129 | + public void bodyAvailableAfterHasErrorForUnknownStatusCode() throws Exception { |
| 130 | + HttpHeaders headers = new HttpHeaders(); |
| 131 | + headers.setContentType(MediaType.TEXT_PLAIN); |
| 132 | + TestByteArrayInputStream body = new TestByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)); |
| 133 | + |
| 134 | + given(response.getRawStatusCode()).willReturn(999); |
| 135 | + given(response.getStatusText()).willReturn("Custom status code"); |
| 136 | + given(response.getHeaders()).willReturn(headers); |
| 137 | + given(response.getBody()).willReturn(body); |
| 138 | + |
| 139 | + assertFalse(handler.hasError(response)); |
| 140 | + assertFalse(body.isClosed()); |
| 141 | + assertEquals("Hello World", StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8)); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + private static class TestByteArrayInputStream extends ByteArrayInputStream { |
| 146 | + |
| 147 | + private boolean closed; |
| 148 | + |
| 149 | + public TestByteArrayInputStream(byte[] buf) { |
| 150 | + super(buf); |
| 151 | + this.closed = false; |
| 152 | + } |
| 153 | + |
| 154 | + public boolean isClosed() { |
| 155 | + return closed; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public boolean markSupported() { |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public synchronized void mark(int readlimit) { |
| 165 | + throw new UnsupportedOperationException("mark/reset not supported"); |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public synchronized void reset() { |
| 170 | + throw new UnsupportedOperationException("mark/reset not supported"); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void close() throws IOException { |
| 175 | + super.close(); |
| 176 | + this.closed = true; |
| 177 | + } |
| 178 | + } |
| 179 | + |
124 | 180 | }
|
0 commit comments