0

我试图模拟 zip 文件的 http 响应,com.github.paweladamski:HttpClientMock:1.8.1但我java.io.EOFException在下载 http 响应时得到了,看起来流中没有数据可供使用。我可能做错了什么。你能就这个问题提出任何线索吗?请随意建议任何其他可以解决此问题的模拟客户端 - 要求是模拟 zip 文件的 http 响应。

示例代码

Action action = new Action() {
    @Override
    public HttpResponse getResponse(Request request) throws IOException {
        BasicHttpResponse response = new BasicHttpResponse(new ProtocolVersion("http", 1, 1), status, statusMessage);
        FileEntity entity = new FileEntity(new File("C:/sample-response.zip"));
        response.setEntity(entity);
        return response;
    }
};

HttpClientMock httpClientMock = new HttpClientMock();
Unirest.config().httpClient(ApacheClient.builder(httpClientMock));

httpClientMock.onGet("http://example.com").doAction(action);

// Rest Request Initiated
// Used code to download

HttpGet httpGet = new HttpGet(downloadLocation);
httpGet.addHeader(AUTHORIZATION, "TOKEN");
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();

InputStream content = entity.getContent();
LOGGER.info("** Stream length '{}'", content.available());  // 'content.available()' throwing java.io.EOFException

异常堆栈跟踪

java.io.EOFException
    at java.util.zip.GZIPInputStream.readUByte(GZIPInputStream.java:268)
    at java.util.zip.GZIPInputStream.readUShort(GZIPInputStream.java:258)
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:164)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
    at org.apache.http.client.entity.GZIPInputStreamFactory.create(GZIPInputStreamFactory.java:61)
    at org.apache.http.client.entity.LazyDecompressingInputStream.initWrapper(LazyDecompressingInputStream.java:51)
    at org.apache.http.client.entity.LazyDecompressingInputStream.available(LazyDecompressingInputStream.java:86)

Maven 详细信息 HttpClientMock- 1.8.1

4

0 回答 0