Skip to content

Commit ddac108

Browse files
authored
Merge pull request #350 from sthamman/null-response
Parsing error occurs when POST response is empty gzip content type
2 parents 96c2e8f + d1e8669 commit ddac108

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public byte[] getContentAsBytes() {
8181
}
8282
try {
8383
InputStream is = getContent();
84-
if (isGzipped(getEncoding())) {
85-
is = new GZIPInputStream(getContent());
84+
if (is.available() > 0 && isGzipped(getEncoding())) {
85+
is = new GZIPInputStream(getContent());
8686
}
8787
return getBytes(is);
8888
} catch (IOException e2) {

unirest/src/test/java/BehaviorTests/GZipTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@
3030
import org.junit.Test;
3131

3232
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertFalse;
3334

3435
public class GZipTest extends BddTest {
36+
@Test
37+
public void emptyGzip() {
38+
HttpResponse<String> result = Unirest.post(MockServer.EMPTY_GZIP)
39+
.asString();
40+
assertFalse(result.getParsingError().isPresent());
41+
assertEquals("", result.getBody());
42+
}
43+
3544
@Test
3645
public void testGzip() {
3746
HttpResponse<RequestCapture> resp = Unirest.get(MockServer.GZIP)

unirest/src/test/java/BehaviorTests/MockServer.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class MockServer {
6868
public static final String ERROR_RESPONSE = HOST + "/error";
6969
public static final String DELETE = HOST + "/delete";
7070
public static final String GZIP = HOST + "/gzip";
71+
public static final String EMPTY_GZIP = HOST + "/empty-gzip";
7172
public static final String PATCH = HOST + "/patch";
7273
public static final String INVALID_REQUEST = HOST + "/invalid";
7374
public static final String PASSED_PATH_PARAM = GET + "/{params}/passed";
@@ -98,6 +99,7 @@ public static void reset(){
9899
post("/post", MockServer::jsonResponse);
99100
get("/get", MockServer::jsonResponse);
100101
get("/gzip", MockServer::gzipResponse);
102+
post("/empty-gzip", MockServer::emptyGzipResponse);
101103
get("/redirect", MockServer::redirect);
102104
patch("/patch", MockServer::jsonResponse);
103105
get("/invalid", MockServer::inValid);
@@ -193,6 +195,14 @@ private static Object inValid(Request request, Response response) {
193195
return "You did something bad";
194196
}
195197

198+
private static Object emptyGzipResponse(Request request, Response response) throws Exception {
199+
response.raw().setHeader("Content-Encoding", "gzip");
200+
response.raw().setContentType("application/json");
201+
response.raw().setStatus(200);
202+
response.raw().getOutputStream().close();
203+
return null;
204+
}
205+
196206
private static Object gzipResponse(Request request, Response response) {
197207
response.header("Content-Encoding", "gzip");
198208
return jsonResponse(request, response);
@@ -248,7 +258,7 @@ public static void expectedPages(int expected) {
248258
}
249259

250260
public static void main(String[] args){
251-
261+
252262
}
253263

254264
public static void expectCookie(String name, String value) {

0 commit comments

Comments
 (0)