Skip to content

Commit 6fd394f

Browse files
committed
skip recovering the body from a failed response handling either because the response is a known binary type or because a UnrecoverableException was thrown
1 parent 9cb1004 commit 6fd394f

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

unirest-bdd-tests/src/test/java/BehaviorTests/AsFileTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package BehaviorTests;
2727

2828
import kong.unirest.HttpResponse;
29+
import kong.unirest.ProgressMonitor;
2930
import org.junit.jupiter.api.AfterEach;
3031
import org.junit.jupiter.api.Assertions;
3132
import org.junit.jupiter.api.Test;
@@ -38,8 +39,7 @@
3839
import java.nio.file.Paths;
3940
import java.nio.file.StandardCopyOption;
4041

41-
import static org.junit.jupiter.api.Assertions.assertEquals;
42-
import static org.junit.jupiter.api.Assertions.assertTrue;
42+
import static org.junit.jupiter.api.Assertions.*;
4343

4444
class AsFileTest extends BddTest {
4545

@@ -153,4 +153,15 @@ void canOverrideExistingFiles_Async() throws Exception {
153153

154154
assertEquals(f1, f2);
155155
}
156+
157+
@Test
158+
void dontReadFilesWhenInError() {
159+
HttpResponse<File> f1 = Unirest.get(MockServer.BINARYFILE)
160+
.downloadMonitor((field, fileName, bytesWritten, totalBytes) -> {
161+
throw new RuntimeException("hey hey hey");
162+
})
163+
.asFile(test.toString());
164+
165+
assertEquals("", f1.getParsingError().get().getOriginalBody());
166+
}
156167
}

unirest/src/main/java/kong/unirest/FileResponse.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package kong.unirest;
2727

2828
import java.io.File;
29-
import java.io.IOException;
3029
import java.io.InputStream;
3130
import java.nio.file.CopyOption;
3231
import java.nio.file.Files;
@@ -43,8 +42,8 @@ public FileResponse(RawResponse r, String path, ProgressMonitor downloadMonitor,
4342
InputStream content = getContent(r, downloadMonitor, target);
4443
Files.copy(content, target, copyOptions);
4544
body = target.toFile();
46-
} catch (IOException e) {
47-
throw new UnirestException(e);
45+
} catch (Exception e) {
46+
throw new UnrecoverableException(e);
4847
}
4948
}
5049

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package kong.unirest;
27+
28+
/**
29+
* An exception which prevents Unirest from attempting to recover the body from a failed response / parsing error
30+
*/
31+
public class UnrecoverableException extends UnirestException {
32+
public UnrecoverableException(Exception e) {
33+
super(e);
34+
}
35+
}

unirest/src/main/java/kong/unirest/apache/ApacheResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public InputStream getContent() {
7676

7777
@Override
7878
public byte[] getContentAsBytes() {
79-
if (!hasContent()) {
79+
if (!hasContent() || ContentType.isBinary(getContentType())) {
8080
return new byte[0];
8181
}
8282
try {

unirest/src/main/java/kong/unirest/apache/BaseApacheClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static CredentialsProvider toApacheCreds(Proxy proxy) {
5858
protected <T> HttpResponse<T> transformBody(Function<RawResponse, HttpResponse<T>> transformer, RawResponse rr) {
5959
try {
6060
return transformer.apply(rr);
61+
}catch (UnrecoverableException ue){
62+
return new BasicResponse(rr, "", ue);
6163
}catch (RuntimeException e){
6264
String originalBody = recoverBody(rr);
6365
return new BasicResponse(rr, originalBody, e);

0 commit comments

Comments
 (0)