Похоже, что wiremock перезаписывает объект заголовка Content-Length. Я ожидаю, что длина содержимого сможет быть прочитана resposne.body().contentLength(). См. тестовый пример ниже:
import com.github.tomakehurst.wiremock.WireMockServer;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import com.github.tomakehurst.wiremock.core.Options;
import com.google.common.net.HttpHeaders;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import org.junit.Assert;
import org.junit.Test;
public class WireMockTest {
@Test
public void wireMockTest() throws IOException{
String route = "/test";
long contentLength = 9;
//Build the mock server
WireMockServer server = new WireMockServer(Options.DYNAMIC_PORT);
server.stubFor(get(route)
.willReturn(aResponse()
.withHeader(HttpHeaders.CONTENT_TYPE, "image/png")
.withHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength)) //Does not work, gets overwritten with -1 by okhttp (means it's not found)
.withBody("TEST-BODY")
)
);
server.start();
//Hit the server with okHttp
String mockRequestURL = "http://localhost:" + server.port() + route;
okhttp3.Request serverRequest = new okhttp3.Request.Builder().url(mockRequestURL).build();
OkHttpClient client = new OkHttpClient.Builder().build();
Response resposne = client.newCall(serverRequest).execute();
//Verify the content length is not set
Assert.assertEquals("The content length was not read correctly", contentLength, resposne.body().contentLength());
}
}