wiremock - 来自键=值对的wiremock请求匹配
问题描述
我正在使用 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 中不允许内容。谁能知道如何匹配这样的请求?
解决方案
我找到了解决方案。有一个选项可以拦截和修改请求。在“拦截和修改请求”部分访问 -> 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";
}}
推荐阅读
- kubernetes - GCP K8s (kubectl) 错误消息(需要“container.leases.get”权限)
- angular - Primeng FilterUtils 每个组件都是唯一的?
- angular - 如何在 Angular 8 组件中声明枚举在 Angular 8 中声明枚举时出现错误
- javascript - Vue.js 不会告诉我错误的行号
- php - jVectormap从数据库添加标记只添加一个标记
- php - Laravel 开发服务器关闭
- javascript - 是否可以将 Javascript 和 erb 交织在一起?
- javascript - 将 Nodejs 与已经制作的 HTML/CSS 一起使用
- mysql - 选择在哪里为空
- dataset - 如何为 Mask RCNN 创建自己的数据集?