【问题标题】:WireMock - invalid content-length after modify body by ResponseTransformerWireMock - ResponseTransformer 修改正文后内容长度无效
【发布时间】:2018-03-23 18:51:16
【问题描述】:

我正在尝试在记录模式下使用wiremock 修改响应。 我正在扩展 ResponseTransformer 并替换绝对 url 引用。 Wiremock 返回修改后的响应,但不更新 Content-Length 标头,因此 chrome 认为要下载的字节数很少。 当我尝试通过以下方式添加 Content-Length 标头时:

... Response.Builder.like(response).headers(updateContentLength(response.getHeaders(), modifiedBody.length()));
...

private HttpHeaders updateContentLength(HttpHeaders headers, int bodyLength) {
    HttpHeaders newHttpHeaders = HttpHeaders.noHeaders();

    for (HttpHeader header : headers.all()) {

        if(header.key().equalsIgnoreCase("Content-Length"))
        {
            newHttpHeaders.plus(new HttpHeader(header.key(), bodyLength +""));
        }
        else
        {
            newHttpHeaders.plus(header);
        }
    }
    return  newHttpHeaders;
}

一些像 Set-Cookie 这样的标题消失了 并且额外添加了“Transfer-Encoding: chunked”。

使用: wiremock-standalone-2.14.0.jar

【问题讨论】:

标签: wiremock


【解决方案1】:

1) 在 github.com/tomakehurst/wiremock/issues/907 上报告了该错误

2) 我的解决方法是不通过在客户忽略它们的地方添加白色字符来更改实际内容长度,例如

<script src="https://example123456.com/aaa"></script>

替换为:

<script src=      "https://example.com/aaa"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2019-07-10
    • 1970-01-01
    • 2020-11-05
    • 2018-09-15
    • 2020-07-29
    相关资源
    最近更新 更多