Pavel Rappo
03/06/2025, 10:21 AMPavel Rappo
03/06/2025, 10:49 AMpublic static void main(String[] args) {
var wireMockServer = new WireMockServer(options().enableBrowserProxying(true).port(1080));
wireMockServer.startRecording(new RecordSpecBuilder().build());
wireMockServer.start();
try {
try {
Thread.sleep(Duration.ofSeconds(20));
} catch (InterruptedException ignored) { }
wireMockServer.getStubMappings().forEach(System.out::println);
} finally {
wireMockServer.stop();
}
}
then I run curl
to simulate the said communication
curl --proxy localhost:1080 <http://google.com>
and observe this output from the server:
{
"id" : "3405af3a-bc9d-44a3-901e-7be961c29c25",
"name" : "",
"request" : {
"url" : "/",
"method" : "GET"
},
"response" : {
"status" : 200,
"body" : ...
"headers" : {
"Alt-Svc" : "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000",
"Server" : "gws",
"P3P" : "CP=\"This is not a P3P policy! See <http://g.co/p3phelp|g.co/p3phelp> for more info.\"",
"Date" : "Thu, 06 Mar 2025 09:52:19 GMT",
"Accept-Ranges" : "none",
"X-Frame-Options" : "SAMEORIGIN",
"Cache-Control" : "private, max-age=0",
"Vary" : "Accept-Encoding",
"Set-Cookie" : ...],
"Expires" : "-1",
"X-XSS-Protection" : "0",
"Accept-CH" : "Sec-CH-Prefers-Color-Scheme",
"Content-Security-Policy-Report-Only" : "object-src 'none';base-uri 'self';script-src '...' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri <https://csp.withgoogle.com/csp/gws/other-hp>",
"Content-Type" : "text/html; charset=ISO-8859-1"
}
},
"uuid" : ...,
"persistent" : true
}
There's no mention of google.com in either request or response. Why?Lee Turner
03/06/2025, 11:58 AMwireMockServer.startRecording("<https://wc-echo.wiremockapi.cloud>");
The second thing you might want to try is to stop recording:
wireMockServer.stopRecording();
If I then put all that together using this code:
public static void main(String[] args) {
var wireMockServer = new WireMockServer(options().port(1080));
wireMockServer.start();
wireMockServer.startRecording("<https://wc-echo.wiremockapi.cloud>");
try {
Thread.sleep(Duration.ofSeconds(30));
wireMockServer.stopRecording();
wireMockServer.getStubMappings().forEach(System.out::println);
} catch (InterruptedException e) {
// ignore
}
finally {
wireMockServer.stop();
}
}
I then make a request to the local wiremock server:
curl localhost:1080/foo/bar
I get the following output in the terminal from the echo server via the local wiremock server:
Received the following request
GET /foo/bar HTTP/1.1
Accept: */*
Connection: keep-alive
Host: wc-echo.wiremockapi.cloud
User-Agent: Apache-HttpClient/5.4.2 (Java/23.0.2)
I then wait for the Thread.sleep
to end and I see this in the console where I am running the code:
{
"id": "c17bfca4-0eda-4935-bbdd-0d40d9965cce",
"name": "foo_bar",
"request": {
"url": "/foo/bar",
"method": "GET"
},
"response": {
"status": 200,
"body": "Received the following request\n\nGET /foo/bar HTTP/1.1\nAccept: */*\nConnection: keep-alive\nHost: wc-echo.wiremockapi.cloud\nUser-Agent: Apache-HttpClient/5.4.2 (Java/23.0.2)\n\n",
"headers": {
"Matched-Stub-Id": "bece70e6-7ee6-4926-ac9c-161270f6e07b",
"Matched-Stub-Name": "WireMock Cloud Live Echo",
"Vary": "Origin",
"Content-Type": "text/plain"
}
},
"uuid": "c17bfca4-0eda-4935-bbdd-0d40d9965cce",
"persistent": true
}
The example to specify where to record to and to stop recording can be found on the docs page here - https://wiremock.org/docs/record-playback/#recordingPavel Rappo
03/06/2025, 12:20 PMPavel Rappo
03/06/2025, 12:30 PMPavel Rappo
03/06/2025, 12:33 PMLee Turner
03/06/2025, 12:42 PMPavel Rappo
03/06/2025, 12:49 PMWireMockServer(options().enableBrowserProxying(true).port(1080));
The problem with that is when I gather the recordings they lack some HTTP headers, such as Host.Pavel Rappo
03/06/2025, 12:57 PMvar wireMockServer = new WireMockServer(options()
.preserveHostHeader(true)
.recordRequestHeadersForMatching(List.of("host"))
.enableBrowserProxying(true)
.port(1080));
Pavel Rappo
03/06/2025, 12:58 PMPavel Rappo
03/06/2025, 1:32 PMwireMockServer.startRecording(new RecordSpecBuilder().captureHeader("host").build());
Pavel Rappo
03/06/2025, 1:52 PMLee Turner
03/06/2025, 1:55 PMPavel Rappo
03/06/2025, 1:56 PM"name" : "",
"request" : {
"url" : "/",
"method" : "GET",
"headers" : {
"host" : {
"equalTo" : "<http://www.google.com|www.google.com>"
}
}
},
Pavel Rappo
03/06/2025, 1:56 PMLee Turner
03/06/2025, 1:56 PM