微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自key = value对的Wiremock请求匹配

如何解决来自key = value对的Wiremock请求匹配

我正在使用json映射来匹配请求。该请求以内容类型application / x-www-form-urlencoded的形式出现,这意味着作为键=值对,并且该值包含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文档。原因:序言中不允许内容。 谁能知道如何匹配这样的请求?

解决方法

我找到了解决方案。有一个选项可以拦截和修改请求。访问“拦截和修改请求”部分中的-> 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";
}}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。