Skip to content

Commit d95bbb6

Browse files
committed
Test for hasError keeping body available in case of unknown status code
Issue: SPR-16604
1 parent b2d87ab commit d95bbb6

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,13 +18,15 @@
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
2122

2223
import org.junit.Test;
2324

2425
import org.springframework.http.HttpHeaders;
2526
import org.springframework.http.HttpStatus;
2627
import org.springframework.http.MediaType;
2728
import org.springframework.http.client.ClientHttpResponse;
29+
import org.springframework.util.StreamUtils;
2830

2931
import static org.junit.Assert.*;
3032
import static org.mockito.BDDMockito.*;
@@ -33,6 +35,8 @@
3335
* Unit tests for {@link DefaultResponseErrorHandler}.
3436
*
3537
* @author Arjen Poutsma
38+
* @author Juergen Hoeller
39+
* @author Denys Ivano
3640
*/
3741
public class DefaultResponseErrorHandlerTests {
3842

@@ -121,4 +125,56 @@ public void hasErrorForUnknownStatusCode() throws Exception {
121125
assertFalse(handler.hasError(response));
122126
}
123127

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+
124180
}

0 commit comments

Comments
 (0)