Skip to content

Proxying

Stef Heyenrath edited this page May 7, 2017 · 12 revisions

Proxy stub mappings

Proxy responses are defined in exactly the same manner as stubs, meaning that the same request matching criteria can be used.

The following code will proxy all GET requests made to http://:/other/service/.* to http://otherservice.com/approot, e.g. when running WireMock.NET locally a request to http://localhost:9000/other/service/doc/123 would be forwarded to http://otherservice.com/approot/other/service/doc/123.

server
  .Given(
    Request.Create()
      .WithPath("/other/service")
  )
  .RespondWith(
    Response.Create()
      .WithProxy("http://otherhost.com/approot")
  );

The JSON equivalent would be:

{
    "Request": {
      "Path": {
        "Matchers": [
          {
            "Name": "WildcardMatcher",
            "Pattern": "/google"
          }
        ]
      },
      "Methods": [
        "get"
      ]
    },
    "Response": {
      "UseTransformer": false,
      "ProxyUrl": "http://www.google.com"
    }
}

Proxy/intercept

The proxy/intercept pattern described above is achieved by adding a low priority proxy mapping with a broad URL match and any number of higher priority stub mappings e.g.

// Low priority catch-all proxies to otherhost.com by default
server
  .Given(
    Request.Create()
      .WithPath("/*")
      .AtPriority(10)
  )
  .RespondWith(
    Response.Create()
      .WithProxy("http://otherhost.com")
  );

// High priority stub will send a Service Unavailable response if the specified URL is requested:
server
  .Given(
    Request.Create()
      .WithPath("/api/override/123")
      .AtPriority(1)
  )
  .RespondWith(
    Response.Create()
      .WithStatusCode(503)
      .WithBody("ERROR")
  );
Clone this wiki locally