问题描述
我正在使用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";
}}