Skip to content

Commit

Permalink
Merge pull request #350 from sthamman/null-response
Browse files Browse the repository at this point in the history
Parsing error occurs when POST response is empty gzip content type
  • Loading branch information
ryber authored Apr 11, 2020
2 parents 96c2e8f + d1e8669 commit ddac108
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions unirest/src/main/java/kong/unirest/apache/ApacheResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public byte[] getContentAsBytes() {
}
try {
InputStream is = getContent();
if (isGzipped(getEncoding())) {
is = new GZIPInputStream(getContent());
if (is.available() > 0 && isGzipped(getEncoding())) {
is = new GZIPInputStream(getContent());
}
return getBytes(is);
} catch (IOException e2) {
Expand Down
9 changes: 9 additions & 0 deletions unirest/src/test/java/BehaviorTests/GZipTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class GZipTest extends BddTest {
@Test
public void emptyGzip() {
HttpResponse<String> result = Unirest.post(MockServer.EMPTY_GZIP)
.asString();
assertFalse(result.getParsingError().isPresent());
assertEquals("", result.getBody());
}

@Test
public void testGzip() {
HttpResponse<RequestCapture> resp = Unirest.get(MockServer.GZIP)
Expand Down
12 changes: 11 additions & 1 deletion unirest/src/test/java/BehaviorTests/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class MockServer {
public static final String ERROR_RESPONSE = HOST + "/error";
public static final String DELETE = HOST + "/delete";
public static final String GZIP = HOST + "/gzip";
public static final String EMPTY_GZIP = HOST + "/empty-gzip";
public static final String PATCH = HOST + "/patch";
public static final String INVALID_REQUEST = HOST + "/invalid";
public static final String PASSED_PATH_PARAM = GET + "/{params}/passed";
Expand Down Expand Up @@ -98,6 +99,7 @@ public static void reset(){
post("/post", MockServer::jsonResponse);
get("/get", MockServer::jsonResponse);
get("/gzip", MockServer::gzipResponse);
post("/empty-gzip", MockServer::emptyGzipResponse);
get("/redirect", MockServer::redirect);
patch("/patch", MockServer::jsonResponse);
get("/invalid", MockServer::inValid);
Expand Down Expand Up @@ -193,6 +195,14 @@ private static Object inValid(Request request, Response response) {
return "You did something bad";
}

private static Object emptyGzipResponse(Request request, Response response) throws Exception {
response.raw().setHeader("Content-Encoding", "gzip");
response.raw().setContentType("application/json");
response.raw().setStatus(200);
response.raw().getOutputStream().close();
return null;
}

private static Object gzipResponse(Request request, Response response) {
response.header("Content-Encoding", "gzip");
return jsonResponse(request, response);
Expand Down Expand Up @@ -248,7 +258,7 @@ public static void expectedPages(int expected) {
}

public static void main(String[] args){

}

public static void expectCookie(String name, String value) {
Expand Down

0 comments on commit ddac108

Please sign in to comment.