【问题标题】:Wiremock different response for the same requestWiremock 对同一请求的不同响应
【发布时间】:2018-01-17 06:39:09
【问题描述】:

我有一个特殊的情况,我必须使用wiremock为同一个请求发送不同的响应。

我正在使用“场景”功能。我遇到的问题是一个默认请求,它类似于场景中的请求。我再解释一下.....

映射

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 数据包中,指示器根据触发到应用程序的内容从 Up 或 Down 变化。

应用程序在缓存中有数据的那一刻向 mock 发送“Up”请求,这很好,我只能独立测试 UP 请求。但是在某些情况下,“DOWN”请求被发送到模拟,然后是对相同“id”的“UP”请求。此“UP”请求的响应必须不同,但该请求与默认 UP 请求完全相同。

我已经尝试了几种不同的方法来匹配请求,但是我得到了 404 或者我来的关闭是模拟响应不正确。

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

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

【问题讨论】:

    标签: java testing wiremock


    【解决方案1】:

    我已经用 ResponseDefinitionTransformer 解决了这个问题,并且效果很好。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多