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

Commit

Permalink
refactor #22
Browse files Browse the repository at this point in the history
  • Loading branch information
Adven27 committed Oct 12, 2021
1 parent 0d26920 commit 830097d
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/main/java/io/adven/grpc/wiremock/HttpMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
Expand Down Expand Up @@ -81,7 +81,6 @@ public void destroy() {

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

private String getBody() {
try {
InputStream bodyStream = httpResponse.body();
if (httpResponse.headers().firstValue("Content-Encoding").orElse("").equals("gzip")) {
bodyStream = new GZIPInputStream(bodyStream);
}
return new String(bodyStream.readAllBytes());
try (InputStream is = isGzip() ? new GZIPInputStream(httpResponse.body()) : httpResponse.body()) {
return new String(is.readAllBytes());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private boolean isGzip() {
return httpResponse.headers().firstValue("Content-Encoding").orElse("").equals("gzip");
}
}

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

0 comments on commit 830097d

Please sign in to comment.