【问题标题】:wiremock request matching from key=value pair来自键=值对的wiremock请求匹配
【发布时间】:2021-02-22 08:24:49
【问题描述】:

我正在使用 json 映射来匹配请求。请求以内容类型 application/x-www-form-urlencoded 的形式出现,这意味着作为 Key=value 对,并且该值包含 xml 数据。例如:

REQUEST=<?xml version="1.0" encoding="UTF-8"?>
    <n:request xmlns:n="schema uri" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="schema location">
        <header userId="userId" password="password" requesterId="123" version="100" language="de">
            <product>xxx</product>
        </header>
        <subject>
            <party externalIdentifier="1">
                <address externalIdentifier="11">
                    <person>
                        <firstName>rinku</firstName>
                        <lastName>chy</lastName>
                        <birthDate>1973-12-10</birthDate>
                    </person>
                    <street>street</street>
                    <number>12</number>
                    <countryCode>de</countryCode>
                    <zipCode>123</zipCode>
                    <city>city</city>
                </address>
            </party>
        </subject>
    </n:request>

目的是查找产品名称和人名。我已经尝试过 xpath 和查询参数表达式来匹配http://wiremock.org/docs/request-matching/ 中所述的请求。但还没有找到解决方案。例如

{
        "request": {
            "method": "POST",
            "urlPattern": "/mock.*",
            "queryParameters": {
                "product": {
                    "matches": "xxx"
                }
            }, 
// tried both seperately
            "bodyPatterns": [
                {
                    "matchesXPath": "//*[local-name()='request']/*[local-name()='header']/*[local-name()='product'][text()='xxx']"
                }
            ]
        },
        "response": {
            "status": 200,
            "bodyFileName": "response.xml",
            "headers": {
                "Content-Type": "text/xml; charset=UTF-8",
                "Content-Location": "response.xml"
            }
        }
    }

总是得到相同的错误“[WireMock] (qtp2017957857-34) 警告:无法解析 XML 文档。原因:prolog 中不允许内容。 谁能知道如何匹配这样的请求?

【问题讨论】:

    标签: wiremock


    【解决方案1】:

    我找到了解决方案。有一个选项可以拦截和修改请求。在“拦截和修改请求”部分访问 -> http://wiremock.org/docs/extending-wiremock/

    public class RequestFilter extends StubRequestFilter {
    
    @Override
    public RequestFilterAction filter(Request request) {
    
        // removed "REQUEST=" from request body
        Request modifyRequest = RequestWrapper.create()
                .transformBody(requestBody -> Body.fromOneOf(null, requestBody.asString().substring(8)), null, null))
                .wrap(request);
    
        return RequestFilterAction.continueWith(modifyRequest);
    }
    
    @Override
    public String getName() {
        return "my-request-filter";
    }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多