|
21 | 21 | import feign.Request.HttpMethod;
|
22 | 22 | import feign.Response;
|
23 | 23 | import feign.Util;
|
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.io.InputStream; |
24 | 26 | import java.util.Collection;
|
25 | 27 | import java.util.Collections;
|
| 28 | +import java.util.HashMap; |
26 | 29 | import java.util.LinkedHashMap;
|
27 | 30 | import java.util.Map;
|
28 | 31 | import org.junit.Rule;
|
@@ -135,4 +138,53 @@ public void retryAfterHeaderThrowsRetryableException() throws Throwable {
|
135 | 138 |
|
136 | 139 | throw errorDecoder.decode("Service#foo()", response);
|
137 | 140 | }
|
| 141 | + |
| 142 | + @Test |
| 143 | + public void lengthOfBodyExceptionTest() { |
| 144 | + Response response = bigBodyResponse(); |
| 145 | + Exception defaultException = errorDecoder.decode("Service#foo()", response); |
| 146 | + assertThat(defaultException.getMessage().length()).isLessThan(response.body().length()); |
| 147 | + |
| 148 | + ErrorDecoder customizedErrorDecoder = new ErrorDecoder.Default(4000, 2000); |
| 149 | + Exception customizedException = customizedErrorDecoder.decode("Service#foo()", response); |
| 150 | + assertThat(customizedException.getMessage().length()) |
| 151 | + .isGreaterThanOrEqualTo(response.body().length()); |
| 152 | + } |
| 153 | + |
| 154 | + private Response bigBodyResponse() { |
| 155 | + String content = "I love a storm in early May\n" + |
| 156 | + "When springtime’s boisterous, firstborn thunder\n" + |
| 157 | + "Over the sky will gaily wander\n" + |
| 158 | + "And growl and roar as though in play.\n" + |
| 159 | + "\n" + |
| 160 | + "A peal, another — gleeful, cheering…\n" + |
| 161 | + "Rain, raindust… On the trees, behold!-\n" + |
| 162 | + "The drops hang, each a long pearl earring;\n" + |
| 163 | + "Bright sunshine paints the thin threads gold.\n" + |
| 164 | + "\n" + |
| 165 | + "A stream downhill goes rushing reckless,\n" + |
| 166 | + "And in the woods the birds rejoice.\n" + |
| 167 | + "Din. Clamour. Noise. All nature echoes\n" + |
| 168 | + "The thunder’s youthful, merry voice.\n" + |
| 169 | + "\n" + |
| 170 | + "You’ll say: ‘Tis laughing, carefree Hebe —\n" + |
| 171 | + "She fed her father’s eagle, and\n" + |
| 172 | + "The Storm Cup brimming with a seething\n" + |
| 173 | + "And bubbling wine dropped from her hand"; |
| 174 | + |
| 175 | + InputStream inputStream = new ByteArrayInputStream(content.getBytes(UTF_8)); |
| 176 | + Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>(); |
| 177 | + headers.put("Content-Type", Collections.singleton("text/plain")); |
| 178 | + return Response.builder() |
| 179 | + .status(400) |
| 180 | + .request(Request.create( |
| 181 | + Request.HttpMethod.GET, |
| 182 | + "/home", |
| 183 | + Collections.emptyMap(), |
| 184 | + "data".getBytes(Util.UTF_8), |
| 185 | + Util.UTF_8, |
| 186 | + null)) |
| 187 | + .body(content, Util.UTF_8) |
| 188 | + .build(); |
| 189 | + } |
138 | 190 | }
|
0 commit comments