From f5e75ea2e7be009c950fe0c5e287bb193df8e6b5 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 7 May 2017 09:48:03 +0200 Subject: [PATCH] Created Proxying (markdown) --- Proxying.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Proxying.md diff --git a/Proxying.md b/Proxying.md new file mode 100644 index 0000000..8f6091c --- /dev/null +++ b/Proxying.md @@ -0,0 +1,39 @@ +# Proxying + +WireMock.NET has the ability to selectively proxy requests through to other hosts. This supports a proxy/intercept setup where requests are by default proxied to another (possibly real, live) service, but where specific stubs are configured these are returned in place of the remote service’s response. Responses that the live service can’t be forced to generate on demand can thus be injected for testing. + +## 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. + +stubFor(get(urlMatching("/other/service/.*")) + .willReturn(aResponse().proxiedFrom("http://otherhost.com/approot"))); + +The JSON equivalent would be: + +{ + "request": { + "method": "GET", + "urlPattern": "/other/service/.*" + }, + "response": { + "proxyBaseUrl" : "http://otherhost.com/approot" + } +} + +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 +stubFor(get(urlMatching(".*")).atPriority(10) + .willReturn(aResponse().proxiedFrom("http://otherhost.com"))); + + +// High priority stub will send a Service Unavailable response +// if the specified URL is requested +stubFor(get(urlEqualTo("/api/override/123")).atPriority(1) + .willReturn(aResponse().withStatus(503))); +