java - 使用 WireMock 设置 Content-Length HTTP header 作为响应

标签 java junit mocking wiremock

看起来wiremock 覆盖了Content-Length header 对象。我希望 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());
    }
}

最佳答案

当我通过向服务器发布请求来添加路由时,我能够让它尊重 header 。

@Test
public void wireMockTest() throws IOException{

    String route = "/test";
    String body = "Hello World!";

    //Build the mock server
    WireMockServer server = new WireMockServer(Options.DYNAMIC_PORT);     
    server.start();

    //Configure okHttp
    OkHttpClient client = new OkHttpClient.Builder().build();
    String mockServerURL = "http://localhost:" + server.port();

    //Add the route to the server using JSON
    okhttp3.Request configRequest = new okhttp3.Request.Builder().url(mockServerURL + "/__admin/mappings")
            .post(RequestBody.create(MediaType.parse("application/json"), 
                "{\n" +
                "    \"request\": {\n" +
                "        \"method\": \"GET\",\n" +
                "        \"url\": \"" +route + "\"" +
                "    },\n" +
                "    \"response\": {\n" +
                "        \"status\": \"200\",\n" +
                "        \"body\": \""+body+"\",\n" +
                "        \"headers\": {\n" +
                "            \"Content-Type\": \"text/plain\",\n" +
                "            \"Content-Length\": \""+body.length()+"\"\n" +
                "        }\n" +
                "    }\n" +
                "}")).build();
    client.newCall(configRequest).execute();

    //Hit the server with okHttp
    okhttp3.Request serverRequest = new okhttp3.Request.Builder().url(mockServerURL + route).build();
    Response resposne = client.newCall(serverRequest).execute();

    //Verify the content length is not set
    Assert.assertEquals("The content length was no read correctly", body.length(), resposne.body().contentLength());
}

关于java - 使用 WireMock 设置 Content-Length HTTP header 作为响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44687556/

相关文章:

android - 测试通过 maven 和 adb 运行,而不是 IntelliJ - 为什么?

java - 将 Matchers.eq() 与其他模拟值一起使用时出现 InvalidUseOfMatchersException

c++ - Google 测试框架中的可复制模拟

java - 如何在 java 中使用 bouncycaSTLe 将 PrivateKeyUsage 扩展添加到证书?

Java AES 加盐加密

java - JDK 8 中带有压缩参数的 GC 日志轮换

unit-testing - Grails集成测试:get问题

java - 无法在android中通过JSON将base64字符串图像发送到asp.net web服务

java - 如何使用 JUnit 在 Spring 中获取数据库连接?

php - 模拟 SUT 本身