对相同的请求进行Wiremock不同的响应

时间:2018-01-17 06:39:09

标签: java testing wiremock

我有一种奇怪的情况,我必须使用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"
  }
 }
}

__文件

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数据包中,指标根据触发应用程序的内容从“上”或“下”更改。

应用程序发送&#34; Up&#34;当它在缓存中有数据的时候请求模拟,这很好,我可以独立测试UP请求。但有些情况下,&#34; DOWN&#34;请求被发送到模拟,然后是&#34; UP&#34;请求相同的&#34; id&#34;。对此&#34; UP&#34;的回应请求必须不同,但请求与默认的UP请求完全相同。

我已经尝试了几种不同的方法来匹配请求,但我要么得到404,要么我得到的结果是模拟响应不正确。

如果问题不明确或需要更多信息,我可以澄清一下。

如何解决此问题的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我已经使用ResponseDefinitionTransformer解决了这个问题,它完美无缺。

@Tom Akehurst,感谢这个很棒的工具。