我有一个 API A,UI 使用它来显示一些数据。调用了一些内部 API,数据由 API A 收集并返回。所以,流程是这样的:
API A ---> API B
在顺利的情况下,API B 返回所有属性,但在糟糕的一天,相同的 API B 可能会排除某些属性,这反过来会改变 API A 的响应。API A 的处理会解决丢失的属性并执行必要的操作步骤。
我们计划使用wiremock 来测试这一点。问题是我们最终以太多的wiremock 存根来解决缺失属性的情况。如果我们以编程方式执行此操作,则会容易得多,因为我们可以只有一个输入,并且可以根据测试用例更改响应。
有没有一种方法可以通过文件存根来做同样的事情?,即我们有一个存根映射,但模拟响应是在测试用例的设置中编辑的?
是的,您可以通过编程方式覆盖响应文件中的多个测试用例/缺失的属性。在存根中,您只需要添加响应模板转换器。这就是映射存根的样子:
{
"priority": 1,
"request": {
"method": "POST",
"urlPathPattern": "/program"
},
"response": {
"status": 200,
"bodyFileName": "upsertGeneral.xml",
"transformers": [
"response-template"
],
"headers": {
"Content-Type": "text/xml"
}
}
}
这就是响应文件 upsertGeneral.xml 的样子。基本上,您只需分配“变量”并提供它们的路径。如果它们存在,您可以定义需要返回的内容,否则如果它们不存在:
<soapenv:Envelope>
<soapenv:Body>
<upsertResponse>
{{#assign 'upsert'}}{{xPath request.body '/Envelope/Body/upsert'}}{{/assign}}
{{#if upsert}}
<result>
{{#assign 'username'}}{{xPath request.body '/Envelope/Body/upsert/username/text()'}}{{/assign}}
{{#if username}}
<created>UsernamePresent</created>
<id>{{xPath request.body '/Envelope/Body/upsert/username/text()'}}</id>
{{else}}
<created>UsernameNotPresent</created>
<id>generating random username: {{randomValue length=18 type='ALPHANUMERIC'}}</id>
{{/if}}
{{/if}}
<success>true</success>
</result>
</upsertResponse>
</soapenv:Body>
</soapenv:Envelope>
请求示例:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<upsert>
<username>testusername</username>
</upsert>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
回应:
<soapenv:Envelope>
<soapenv:Body>
<upsertResponse>
<result>
<created>UsernamePresent</created>
<id>testusername</id>
<success>true</success>
</result>
</upsertResponse>
</soapenv:Body>
</soapenv:Envelope>
缺少用户名的请求示例:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<upsert>
<username></username>
</upsert>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
结果其他路径:
<soapenv:Envelope>
<soapenv:Body>
<upsertResponse>
<result>
<created>UsernameNotPresent</created>
<id>generating random username: rtqorbniv0hz0d1j5o</id>
<success>true</success>
</result>
</upsertResponse>
</soapenv:Body>
</soapenv:Envelope>
希望这有帮助:)