Skip to content

Commit 2e3d2ba

Browse files
authored
Merge pull request #393 from wiremock/proxy-hostname-rewriting
feat: Add hostname rewriting documentation and example
2 parents c14fc57 + 9f43741 commit 2e3d2ba

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/content/docs/docs/proxying.mdx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,51 @@ The JSON equivalent would be:
5151
}
5252
```
5353

54+
## Hostname rewriting
55+
56+
Often API responses contain absolute links and other content that refers to the
57+
domain name of the API's origin. When proxying to another API this can be undesirable
58+
as the domain running WireMock is different from the proxy target and thus a client
59+
following such a link would make its next request directly to the proxy target rather
60+
than to WireMock.
61+
62+
To remedy this issue, as of WireMock `4.0.0-beta.20`, we can enable hostname rewriting,
63+
which will replace any instances of the proxy target's domain name in the response
64+
headers or body with the domain name where WireMock is running.
65+
66+
For instance, if WireMock was running on `http://localhost:8081` to a proxy target
67+
of `https://api.github.com` and a proxied response body contained
68+
`"self": "https://api.github.com/users/123"`, with hostname rewriting enabled this
69+
would be changed to `"self": "https://localhost:8081/users/123"`.
70+
71+
To enable hostname rewriting, we can add a transformer to the response definition:
72+
73+
```java
74+
stubFor(any(anyUrl())
75+
.willReturn(aResponse()
76+
.withTransformers("proxied-hostname-rewrite")
77+
.proxiedFrom("https://api.github.com")
78+
)
79+
);
80+
```
81+
82+
The JSON equivalent would be:
83+
84+
```json
85+
{
86+
"request": {
87+
"method": "ANY"
88+
},
89+
"response": {
90+
"transformers": ["proxied-hostname-rewrite"],
91+
"proxyBaseUrl": "https://api.github.com"
92+
}
93+
}
94+
```
95+
96+
The `proxied-hostname-rewrite` transformer also supports host name rewriting within
97+
response bodies that are gzipped.
98+
5499
## Proxy/intercept
55100

56101
The proxy/intercept pattern described above is achieved by adding a low

0 commit comments

Comments
 (0)