Wiremock - redirect to proxy when first mapping error

I'm trying to use Wiremock 2.24.1 to dynamically return different bodies files, depending on request path, and if not found locally I would like to call an external resource (as a proxy).

I'm using this mapping file:

{
  "priority": 1,
  "request" : {
    "urlPattern" : "/rest/v2/name/.*",
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "rest_v2_name_{{request.requestLine.pathSegments.[3]}}_body.json",
    "headers" : {
      "Content-Type" : "application/json;charset=utf-8"
    },
    "transformers": ["response-template"]
  }
}

And I have one file named rest_v2_name_springfield_body.json with this content

[
    {
        "name": "Springfield",
        "now": "{{now}}",
        "yesterday"; "{{now offset='-1 days' format='yyyy-MM-dd HH:mm:ssZ'}},
        "tomorrow"; "{{now offset='1 days' format='yyyy-MM-dd HH:mm:ssZ'}}
    }
]

returned when I call curl http://localhost:8099/rest/v2/name/springfield :

[
    {
        "name": "Springfield",
        "now": "2019-08-17T00:23:12Z",
        "yesterday"; "2019-08-15 21:23:12-0300,
        "tomorrow"; "2019-08-17 21:23:12-0300
    }
]

If I call with another path value (like http://localhost:8099/rest/v2/name/brasil) I want to be handled by this other mapping:

{
  "priority": 10,
  "request" : {
    "urlPattern" : "/rest/v2/name/.*",
    "method" : "GET"
  },
  "response" : {
    "proxyBaseUrl" : "http://restcountries.eu"
  }
}

But instead of answering the response of http://restcountries.eu/rest/v2/name/brasil, I'm getting a local HTTP 500 Error with this message (I believe it's from the first matching mapping):

java.io.FileNotFoundException: ./__files/rest_v2_name_brasil_body.json

I tried this alternatives

Is there a way to solve this scenario without coding ?

Upvotes: 0

Views: 1823

Answers (1)

Utkarsh Bhatt
Utkarsh Bhatt

Reputation: 143

Regardless of the proxy on/off, Wiremock will always follow the priorities.

Considering that you have the same request pattern,

Case-I : When you have Proxy(brasil) at priority 1 and stub-mapping(spingfield) at priority 10 : You will be getting all the responses through Proxy only. There is no failover check for other mappings kind of thing.

Case-I : When you have Proxy(brasil) at priority 10 and stub-mapping(spingfield) at priority 1 : You will be getting the same response as stub-mapping you have configured since every request will be going to satisfy the springfield rule.

To solve this, you need to record every request when you are on Proxy.

Once you stop the recording, you can check for the mappings and add additional mappings as per you requirement !

Upvotes: 0

Related Questions