Wiremock 对同一请求的不同响应
Wiremock different response for the same request
我有一个特殊的情况,我必须使用 wiremock 为同一个请求发送不同的响应。
我正在使用 "Scenario" 功能。我遇到的问题是一个默认请求,与场景中的请求类似。我会进一步解释......
映射
Default_Up_request.json
{
"request": {
"method": "POST",
"url": "/someservice.asmx",
"bodyPatterns": [
{
"matchesXPath": "//*[name()='status']/*
[name()='id'][text()='333602']"
},
{
"matchesXPath": "//*[name()='status']/*
[name()='indicator'][text()='Up']"
}
]
},
"response": {
"status": 200,
"bodyFileName": "default_response.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
Scenario_request_1.json
{
"scenarioName": "ChainedRequests",
"requiredScenarioState": "Started",
"newScenarioState": "down",
"request": {
"method": "POST",
"url": "/someservice.asmx",
"bodyPatterns": [
{
"matchesXPath": "//*[name()='status']/*
[name()='id'][text()='333602']"
},
{
"matchesXPath": "//*[name()='status']/*
[name()='indicator'][text()='Down']"
}
]
},
"response": {
"status": 200,
"bodyFileName": "down_response.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
Scenario_request_2.json
{
"scenarioName": "ChainedRequests",
"requiredScenarioState": "down",
"newScenarioState": "up",
"request": {
"method": "POST",
"url": "/someservice.asmx",
"bodyPatterns": [
{
"matchesXPath": "//*[name()='status']/*
[name()='id'][text()='333602']"
},
{
"matchesXPath": "//*[name()='status']/*
[name()='indicator'][text()='Up']"
}
]
},
"response": {
"status": 200,
"bodyFileName": "alternate_response.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
__files
default_response.xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Response mlns="http://something.net.local/">
<StatusResult>
<ids/>
<events>false</events>
<startTime>false</startTime>
<activation>false</activation>
<settles>false</settles>
</StatusResult>
</Response>
</soap:Body>
</soap:Envelope>
alternate_response.xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Response mlns="http://something.net.local/">
<StatusResult>
<ids/>
<events>true</events>
<startTime>false</startTime>
<activation>false</Activation>
<settles>false</settles>
</StatusResult>
</Response>
</soap:Body>
</soap:Envelope>
请求
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<status xmlns="http://something.net.local/">
<id>333602</id>
<indicator>(Up/Down)</indicator>
<events>false</events>
<startTime>false</startTime>
<activation>false</activation>
<over>false</over>
</status>
</soap:Body>
</soap:Envelope>
在 SOAP 数据包中,指示器根据对应用程序的触发从向上或向下变化。
应用程序在缓存中有数据时向模拟发送 "Up" 请求,这很好,我只能独立测试 UP 请求。但是在某些情况下,"DOWN" 请求被发送到 mock,随后是对相同 "id" 的 "UP" 请求。此 "UP" 请求的响应必须不同,但请求与默认 UP 请求完全相同。
我已经尝试了几种不同的方法来匹配请求,但我要么得到 404,要么我的关闭是模拟响应不正确。
如果问题不清楚或需要更多信息,我可以澄清。
任何有关如何解决此问题的帮助将不胜感激。
我已经用 ResponseDefinitionTransformer 解决了这个问题,它工作得很好。
@Tom Akehurst,感谢这个很棒的工具。
我有一个特殊的情况,我必须使用 wiremock 为同一个请求发送不同的响应。
我正在使用 "Scenario" 功能。我遇到的问题是一个默认请求,与场景中的请求类似。我会进一步解释......
映射
Default_Up_request.json
{
"request": {
"method": "POST",
"url": "/someservice.asmx",
"bodyPatterns": [
{
"matchesXPath": "//*[name()='status']/*
[name()='id'][text()='333602']"
},
{
"matchesXPath": "//*[name()='status']/*
[name()='indicator'][text()='Up']"
}
]
},
"response": {
"status": 200,
"bodyFileName": "default_response.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
Scenario_request_1.json
{
"scenarioName": "ChainedRequests",
"requiredScenarioState": "Started",
"newScenarioState": "down",
"request": {
"method": "POST",
"url": "/someservice.asmx",
"bodyPatterns": [
{
"matchesXPath": "//*[name()='status']/*
[name()='id'][text()='333602']"
},
{
"matchesXPath": "//*[name()='status']/*
[name()='indicator'][text()='Down']"
}
]
},
"response": {
"status": 200,
"bodyFileName": "down_response.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
Scenario_request_2.json
{
"scenarioName": "ChainedRequests",
"requiredScenarioState": "down",
"newScenarioState": "up",
"request": {
"method": "POST",
"url": "/someservice.asmx",
"bodyPatterns": [
{
"matchesXPath": "//*[name()='status']/*
[name()='id'][text()='333602']"
},
{
"matchesXPath": "//*[name()='status']/*
[name()='indicator'][text()='Up']"
}
]
},
"response": {
"status": 200,
"bodyFileName": "alternate_response.xml",
"headers": {
"Content-Type": "text/xml; charset=utf-8"
}
}
}
__files
default_response.xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Response mlns="http://something.net.local/">
<StatusResult>
<ids/>
<events>false</events>
<startTime>false</startTime>
<activation>false</activation>
<settles>false</settles>
</StatusResult>
</Response>
</soap:Body>
</soap:Envelope>
alternate_response.xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Response mlns="http://something.net.local/">
<StatusResult>
<ids/>
<events>true</events>
<startTime>false</startTime>
<activation>false</Activation>
<settles>false</settles>
</StatusResult>
</Response>
</soap:Body>
</soap:Envelope>
请求
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<status xmlns="http://something.net.local/">
<id>333602</id>
<indicator>(Up/Down)</indicator>
<events>false</events>
<startTime>false</startTime>
<activation>false</activation>
<over>false</over>
</status>
</soap:Body>
</soap:Envelope>
在 SOAP 数据包中,指示器根据对应用程序的触发从向上或向下变化。
应用程序在缓存中有数据时向模拟发送 "Up" 请求,这很好,我只能独立测试 UP 请求。但是在某些情况下,"DOWN" 请求被发送到 mock,随后是对相同 "id" 的 "UP" 请求。此 "UP" 请求的响应必须不同,但请求与默认 UP 请求完全相同。
我已经尝试了几种不同的方法来匹配请求,但我要么得到 404,要么我的关闭是模拟响应不正确。
如果问题不清楚或需要更多信息,我可以澄清。
任何有关如何解决此问题的帮助将不胜感激。
我已经用 ResponseDefinitionTransformer 解决了这个问题,它工作得很好。
@Tom Akehurst,感谢这个很棒的工具。