Hello all, I just joined the WireMock community......
# help
s
Hello all, I just joined the WireMock community... I'd been using MockServer for a while but because of scalability issues and lack of community support, the project is pretty much death, I started looking for alternatives and WireMock seems to be perfect. I've been trying to "translate" my MockServer expectations into WireMock's Stubs... and pretty much everything has been smooth, up to one case that I can't really figure out how to write it. Sorry I'm going to use an expectation from MockServer here to show what I mean.
Copy code
{
  "id": "get_profiles_inventory",
  "priority": 0,
  "httpRequest": {
    "method": "GET",
    "path": "/profiles/{profileId}/inventory",
    "pathParameters": {
      "profileId": "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
    },
    "queryStringParameters": {
      "param1": [
        "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
      ]
    }
  },
  "httpOverrideForwardedRequest": {
    "requestOverride": {
      "headers": {
        "Host": [
          "mynewhost.svc.cluster.local"
        ]
      },
      "secure": false
    },
    "requestModifier": {
      "path": {
        "regex": "/profiles/([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})/inventory",
        "substitution": "/vNext/totally/different/path/profiles/$1/inventory"
      }
    }
  }
}
So, this expectation is making MS work as a proxy redirecting the call to a new services with a new path. In this case, whenever there's a GET to /profiles/{profileId}/inventory, I want that request to be redirected to another host (mynewhost.svc.cluster.local) with also a different path, BUT mantaing in this case the profileId from the original path.
GET /profiles/f1ab4c06-6f9a-4d0d-958c-7718b4808f64/inventory => GET mynewhost.svc.cluster.local/vNext/totally/different/path/profiles/f1ab4c06-6f9a-4d0d-958c-7718b4808f64/inventory
Is that possible writing a stub in JSON? Thanks
This is what I've come up with so far... but it's not exactly working
Copy code
{
  "name": "inventory",
  "request": {
    "method": "GET",
    "urlPathTemplate": "/profiles/{profileId}/inventory",
    "pathParameters": {
      "profileId": {
        "matches": "[A-Za-z]*"
      }
    }
  },
  "response": {
    "proxyBaseUrl": "<https://google.com/vNext/totally/different/path/profiles/{{> request.path.profileId }}/inventory",
    "proxyUrlPrefixToRemove": "{{ request.path }}",
    "transformers": [
      "response-template"
    ]
  }
}
When I curl to
<http://example.com/profiles/thisisatest/inventory|example.com/profiles/thisisatest/inventory>
I get a response from google (expected because of my
proxyBaseUrl
with the following message
<p><b>404.</b> <ins>That’s an error.</ins>
<p>The requested URL <code>/vNext/totally/different/path/profiles/thisisatest/inventory/profiles/thisisatest/inventory</code> was not found on this server. <ins>That’s all we know.</ins>
So the redirection is being done as expected, and the profile is kept, BUT, the
proxyUrlPrefixToRemove
does not seem to be working
l
Does it work if you change the
proxyBaseUrl
to
<https://google.com/vNext/totally/different/path>
and then I think you can remove the
proxyUrlPrefixToRemove
part of the stub json. Then when you make a call to the stub it should use the base url specified in the proxy and the path of the matched stub to generate a url of
<https://google.com/vNext/totally/different/path/profiles/thisisatest/inventory>
s
Hello Lee, thanks for your quick reply. It does, but it's not really what I want... I oversimplified the example here, but I cannot just add one path at the oend of the other... unfortunately in the example that I wrote that's the case, but it's not my real case... I do need to remove the whole original path, that's why I expected this to work
Copy code
"proxyUrlPrefixToRemove": "{{ request.path }}"
l
Could you give an example of the full url you want to proxy to for a stub path of
/profiles/thisisatest/inventory
s
Sure, this is a more accurate example
Copy code
{
  "name": "inventory",
  "request": {
    "method": "GET",
    "urlPathTemplate": "/v1/profiles/{profileId}/inventory",
    "pathParameters": {
      "profileId": {
        "matches": "[A-Za-z]*"
      }
    }
  },
  "response": {
    "proxyBaseUrl": "<https://google.com/vNext/totally/different/path/profiles/{{> request.path.profileId }}/inventory",
    "proxyUrlPrefixToRemove": "{{ request.path }}",
    "transformers": [
      "response-template"
    ]
  }
}
Please nptice the
v1
at the begining of the original path... like I said, I cannot just concatenate paths together
l
I haven't played around with the proxy properties in a while but will this work:
Copy code
{
  "name": "inventory",
  "request": {
    "method": "GET",
    "urlPathTemplate": "/v1/profiles/{profileId}/inventory",
    "pathParameters": {
      "profileId": {
        "matches": "[A-Za-z]*"
      }
    }
  },
  "response": {
    "proxyBaseUrl": "<https://google.com/vNext/totally/different/path>",
    "proxyUrlPrefixToRemove": "/v1",
    "transformers": [
      "response-template"
    ]
  }
}
Similar to before but removing the
/v1
prefix. Sorry if I am still miss-understanding your requirements
s
you are right, and that works... I was kinda hoping that
"proxyUrlPrefixToRemove": "{{ request.path }}"
would work... is this expected or is it a bug?
l
I would need to check to see which of those fields are allowed to use the templating engine which would allow the use of handlebars
Glad it worked ultimately though
🙌 1