Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

wiremock - Scenario: First response success, but second same request return 404 error code

I need when do first request then get stub response code = 401.

When do second SAME request then get stub response code = 200.

To do this I use scenario.

Here my WireMock mapping in ONE file:

{
  "scenarioName": "Request transfer, 401",
  "requiredScenarioState": "Started",
  "newScenarioState": "Triggered Once",
  "request": {
    "method": "POST",
    "urlPattern": "/api/T2A"
  },
  "response": {
    "headers": {
      "Content-Type": "application/json"
    },
    "status": 401
  }
}

{
  "scenarioName": "Request transfer, 200",
  "requiredScenarioState": "Triggered Once",
  "newScenarioState": "Started",
  "request": {
    "method": "POST",
    "urlPattern": "/api/T2A"
  },
  "response": {
    "headers": {
      "Content-Type": "application/json"
    },
    "status": 200,
    "bodyFileName": "VB/back/request_transfer.json"
  }
}

When do first request I get stub response = 401.

Nice. But when do second same request WireMock return error code = 404.

                                               Request was not matched
                                               =======================

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
POST                                                       | POST
/api/T2A                                                   | /api/T2A
                                                           |
                                                           |
-----------------------------------------------------------------------------------------------------------------------

2021-03-02 22:44:34.802 Request received:
127.0.0.1 - POST /api/T2A

{"MTS":"2be192ca-dabe-47b9-b","IDNP":"1"}


Matched response definition:
(no response definition configured)

Response:
HTTP/1.1 404
(no headers)


2021-03-02 22:44:34.805 Received request to /not-matched with body 

Why second SAME request not return stub response status = 200?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your scenarios have to have the same name. This is how WireMock identifies that the mapping belongs to that scenario.

You are getting a 404 because there is no mapping for the Scenario Request transfer, 401 and in the ScenarioState Triggered Once.

Changing the scenario names to Request transfer for both mappings would resolve your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...