Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.

Commit 830097d

Browse files
committed
refactor #22
1 parent 0d26920 commit 830097d

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

src/main/java/io/adven/grpc/wiremock/HttpMock.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import org.springframework.stereotype.Component;
1414

1515
import javax.annotation.PreDestroy;
16-
import java.io.InputStream;
1716
import java.io.IOException;
17+
import java.io.InputStream;
1818
import java.io.UncheckedIOException;
1919
import java.lang.reflect.InvocationTargetException;
2020
import java.net.URI;
@@ -81,7 +81,6 @@ public void destroy() {
8181

8282
public Response request(String path, Object message, Map<String, String> headers) throws IOException, InterruptedException {
8383
headers.putAll(HEADERS.get());
84-
headers.remove("accept-encoding");
8584
LOG.info("Grpc request {}:\nHeaders: {}\nMessage:\n{}", path, headers, message);
8685
return new Response(
8786
httpClient.send(
@@ -118,16 +117,16 @@ public int streamSize() {
118117
}
119118

120119
private String getBody() {
121-
try {
122-
InputStream bodyStream = httpResponse.body();
123-
if (httpResponse.headers().firstValue("Content-Encoding").orElse("").equals("gzip")) {
124-
bodyStream = new GZIPInputStream(bodyStream);
125-
}
126-
return new String(bodyStream.readAllBytes());
120+
try (InputStream is = isGzip() ? new GZIPInputStream(httpResponse.body()) : httpResponse.body()) {
121+
return new String(is.readAllBytes());
127122
} catch (IOException e) {
128123
throw new UncheckedIOException(e);
129124
}
130125
}
126+
127+
private boolean isGzip() {
128+
return httpResponse.headers().firstValue("Content-Encoding").orElse("").equals("gzip");
129+
}
131130
}
132131

133132
private HttpRequest.BodyPublisher asJson(Object arg) throws IOException {

0 commit comments

Comments
 (0)