https://linen.dev logo
#wiremock-java
Title
# wiremock-java
s

Shruti Sagar Mishra

02/15/2023, 5:25 PM
Hey Everyone, I am trying to setup wiremock with my emulator to capture and modify(similar to what mitmproxy does) http and https request for android native app. 1. I have installed the certificate in emulator. 2. I want to only intercept 2-3 any call (there are 100s of different call to different host). So basically want to whitelist every other call except the ones I wan to put stub for.
Copy code
HashMap<ExecutionDataKeys, Object> executionStates = new HashMap<>();
WireMockServer wireMockServer = new WireMockServer(
        new WireMockConfiguration().port(8080)
                .notifier(new Slf4jNotifier(true))
);
wireMockServer.start();
ArrayList<String> hostNames = new ArrayList<>(
        Arrays.asList(
                "<https://host1.com>",
                "<https://host2.com>",
        ));

hostNames.forEach(host->{
    wireMockServer.stubFor(any(urlMatching(host+".*"))
            .atPriority(10)
            .willReturn(aResponse().proxiedFrom(host)));
});
I have also set proxy in emulator to localhost:8080. But it seems whitlisting is not working. all of my request are getting blocked. P.S. I am trying to use appium along with wiremock to test state changes in application [10:53 PM] I have already waster 3-4 days on this, and feeling kind exausted in this. [10:53 PM] Please advice
r

Rob Elliot

02/15/2023, 6:05 PM
urlMatching(host+".*")
won't work - it only matches on path
You'll need a separate WireMock to pretend to be each remote host
You could try this:
Copy code
List<String> hostNames = Arrays.asList(
    "<https://example.com>",
    "<https://host2.com>"
);

int i = 0;
for (String host : hostNames) {
    WireMockServer wireMockServer = new WireMockServer(
            new WireMockConfiguration().port(8080 + i)
                    .notifier(new Slf4jNotifier(true))
    );
    wireMockServer.start();
    wireMockServer.stubFor(any(urlMatching(".*"))
            .atPriority(10)
            .willReturn(aResponse().proxiedFrom(host)));
    i++;
}
Or are you trying to run WireMock as a browser proxy? If so you just run one instance of WireMock, as so:
Copy code
WireMockServer wireMockServer = new WireMockServer(
    wireMockConfig()
        .enableBrowserProxying(true)
        .port(8080)
        .notifier(new Slf4jNotifier(true))
);
wireMockServer.start();
When you
enableBrowserProxying(true)
you don't need to create the low priority
any
stub at all, WireMock will proxy unless told otherwise.
However, you will need to set
<http://localhost:8080>
as your proxy in your emulator.
You can then setup stubs for particular hosts by matching on the host header:
Copy code
wireMockServer.stubFor(
    get(urlPathEqualTo("/foo"))
        .withHeader("Host", equalTo("<http://host2.com|host2.com>"))
        .willReturn(aResponse().withBody("replacement for <https://host2.com/foo>"))
);
s

Shruti Sagar Mishra

02/15/2023, 7:03 PM
I beleive below does the same as adding for all host.
Copy code
hostNames.forEach(host->{
    wireMockServer.stubFor(any(urlMatching(host+".*"))
            .atPriority(10)
            .willReturn(aResponse().proxiedFrom(host)));
});
r

Rob Elliot

02/15/2023, 7:03 PM
No, it doesn't
I wrote the browser proxying feature for WireMock
s

Shruti Sagar Mishra

02/15/2023, 7:04 PM
can we do a quick call if at all possible
r

Rob Elliot

02/15/2023, 7:05 PM
I think that's a level of support beyond what can be expected for free
s

Shruti Sagar Mishra

02/15/2023, 7:07 PM
I totally understand
It's documented here: https://wiremock.org/docs/proxying/ under "Running as a browser proxy"
s

Shruti Sagar Mishra

02/15/2023, 7:09 PM
I am not doing browser proxy.
I am doing proxy for my android emulator
which has react native app
all of the call are being made by this app
r

Rob Elliot

02/15/2023, 7:09 PM
For "browser proxy" read "forward proxy"
It's the same principle - your app calls the real hostname, but it is served via WireMock as a proxy
Your other options are: • A WireMock per remote API, and change the hostname your client uses to the WireMock hostname. Set up the
any
stub for
/.*
to proxy to the real service. • A single WireMock and set up multiple
any
stubs, one for each remote API, matching on the
Host
header - but this will require you to override the
Host
header when making the request, which your HTTP client may make quite difficult. I wouldn't recommend the second one.
s

Shruti Sagar Mishra

02/15/2023, 7:41 PM
can you provide paid support for this problem. Would love to buy a coffee
i believe it would hardly take 10-15 mins of ur time
r

Rob Elliot

02/16/2023, 2:50 PM
Hey I contacted you directly to see if I can help further - waiting for your answer.
Hi, I've been thinking about this further - I suggest you: 1. Download WireMock standalone from https://repo1.maven.org/maven2/com/github/tomakehurst/wiremock-jre8-standalone/2.35.0/wiremock-jre8-standalone-2.35.0.jar 2. Run it as so:
java -jar wiremock-jre8-standalone-2.35.0.jar --enable-browser-proxying --port 8080 --verbose
. Then WireMock will be running before the emulator starts. 3. Start the emulator and configure the proxy 4. Try opening a browser inside the emulator and see if you can access https://example.com from it, with WireMock seeing the traffic. At least the browser should give you more feedback about what is wrong - whether it is a certificate problem or something else.
s

Shruti Sagar Mishra

02/19/2023, 7:28 PM
image.png
r

Rob Elliot

02/19/2023, 7:34 PM
That's odd... it looks like the emulator is actually making the request to WireMock instead of making the request to example.com with WireMock as the proxy! Can you see the traffic in your console where you're running WireMock?
I'd like to see what the request looked like from WireMock's point of view, see if I can reproduce it. Could you post your proxy settings, too?
s

Shruti Sagar Mishra

02/19/2023, 8:01 PM
PS C:\Users\Apollo247\IdeaProjects\Mobile_Tests> java -jar wiremock-jre8-standalone-2.35.0.jar --enable-browser-proxying --port 8080 --verbose
2023-02-20 012814.807 Verbose logging enabled 2023-02-20 012821.967 Verbose logging enabled 2023-02-20 012822.076 Verbose logging enabled /$$ /$$ /$$ /$$ /$$ /$$ | $$ /$ | $$|__/ | $$$ /$$$ | $$ | $$ /$$$| $$ /$$ /$$$$$$ /$$$$$$ | $$$$ /$$$$ /$$$$$$ /$$$$$$$| $$ /$$ | $$/$$ $$ $$| $$ /$$__ $$ /$$__ $$| $$ $$/$$ $$ /$$__ $$ /$$_____/| $$ /$$/ | $$$$_ $$$$| $$| $$ \__/| $$$$$$$$| $$ $$$| $$| $$ \ $$| $$ | $$$$$$/ | $$$/ \ $$$| $$| $$ | $$_____/| $$\ $ | $$| $$ | $$| $$ | $$_ $$ | $$/ \ $$| $$| $$ | $$$$$$$| $$ \/ | $$| $$$$$$/| $$$$$$$| $$ \ $$ |__/ \__/|__/|__/ \_______/|__/ |__/ \______/ \_______/|__/ \__/ port: 8080 enable-browser-proxying: true trust-all-proxy-targets: false ca-keystore: C:\Users\Apollo247\.wiremock\ca-keystore.jks ca-keystore-type: jks disable-banner: false no-request-journal: false verbose: true 2023-02-20 012900.148 Request received: 127.0.0.1 - GET /openapi/v2 Host: [localhost:8080] Connection: [close] Matched response definition: (no response definition configured) Response: HTTP/1.1 404 (no headers) 2023-02-20 012900.266 Admin request received: 127.0.0.1 - GET /not-matched Host: [localhost:8080] Connection: [close] 2023-02-20 012900.361 Request was not matched ======================= ----------------------------------------------------------------------------------------------------------------------- | Closest stub | Request | ----------------------------------------------------------------------------------------------------------------------- | gen_204 | | GET | GET /gen_204 | /openapi/v2 <<<<< URL does not match | | ----------------------------------------------------------------------------------------------------------------------- 2023-02-20 012921.089 Proxying: GET http://clientservices.googleapis.com/chrome-variations/seed?osname=android&amp;channel=stable&amp;milestone=83 2023-02-20 012922.113 Request received: 127.0.0.1 - GET /chrome-variations/seed?osname=android&channel=stable&milestone=83 (via browser proxy request) Host: [clientservices.googleapis.com] Connection: [keep-alive] If-None-Match: [Cl/fVXpCtfFgoatvvVRS9Fi/y9JOWXENT7GoqNoBTzAsehGfZazB8zNUu1L1EYgfVqWzjtOuuuCuCwAtvGng/ah3dFEY2wDmyNeZDKdLojRDxU4/WSEsa6i1pkjuCgsg1hABGiAv46mVYQKm1JQwjkhe3OJivOCguSiLjxHlV0t+4nhRJiAB] A-IM: [x-bm,gzip] User-Agent: [Mozilla/5.0 (Linux; Android 11; sdk_gphone_x86_64_arm64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36] Accept-Encoding: [gzip, deflate] Matched response definition: { "status" : 200 } Response: HTTP/1.1 200 Im: [gzip] X-Content-Type-Options: [nosniff] X-Country: [in] X-Frame-Options: [SAMEORIGIN] X-Seed-Signature: [MEUCIEPwddanATRBn3BeNXgMIfOALtgpzCfVRbaGNfEgMRQoAiEA6nz6uEmotvpddo0YtSPX76AA+zkOE0FkkQLrHZNGy1g=] X-Xss-Protection: [0] Date: [Sun, 19 Feb 2023 195922 GMT] Content-Type: [application/x-gzip] 2023-02-20 013002.636 Request received: 127.0.0.1 - GET /generate_204 (via browser proxy request) Connection: [close] User-Agent: [Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.32 Safari/537.36] Host: [connectivitycheck.gstatic.com] Accept-Encoding: [gzip] Matched response definition: { "status" : 500, "base64Body" : "TmV0d29yayBmYWlsdXJlIHRyeWluZyB0byBtYWtlIGEgcHJveGllZCByZXF1ZXN0IGZyb20gV2lyZU1vY2sgdG8gMTkyLjE2OC4xLjM4L2dlbmVyYXRlXzIwNA0KVGFyZ2V0IGhvc3QgaXMgbm90IHNwZWNpZmllZA==" } Response: HTTP/1.1 500 Matched-Stub-Id: [193032b0-72d9-4bf6-a4c4-388e3542104f] Matched-Stub-Name: [generate_204] 2023-02-20 013002.703 Request received: 127.0.0.1 - GET /generate_204 (via browser proxy request) Connection: [close] User-Agent: [Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.32 Safari/537.36] Host: [play.googleapis.com] Accept-Encoding: [gzip] Matched response definition: { "status" : 500, "base64Body" : "TmV0d29yayBmYWlsdXJlIHRyeWluZyB0byBtYWtlIGEgcHJveGllZCByZXF1ZXN0IGZyb20gV2lyZU1vY2sgdG8gMTkyLjE2OC4xLjM4L2dlbmVyYXRlXzIwNA0KVGFyZ2V0IGhvc3QgaXMgbm90IHNwZWNpZmllZA==" } Response: HTTP/1.1 500 Matched-Stub-Id: [193032b0-72d9-4bf6-a4c4-388e3542104f] Matched-Stub-Name: [generate_204] 2023-02-20 013015.588 Request received: 127.0.0.1 - GET / (via browser proxy request) Host: [mitm.it] Connection: [keep-alive] Upgrade-Insecure-Requests: [1] User-Agent: [Mozilla/5.0 (Linux; Android 11; sdk_gphone_x86_64_arm64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36] Accept: [text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9] Purpose: [prefetch] Accept-Encoding: [gzip, deflate] Accept-Language: [en-US,en;q=0.9] Matched response definition: { "status" : 500, "base64Body" : "TmV0d29yayBmYWlsdXJlIHRyeWluZyB0byBtYWtlIGEgcHJveGllZCByZXF1ZXN0IGZyb20gV2lyZU1vY2sgdG8gMTkyLjE2OC4xLjM4Lw0KVGFyZ2V0IGhvc3QgaXMgbm90IHNwZWNpZmllZA==" } Response: HTTP/1.1 500 Matched-Stub-Id: [a57fdb68-b507-4a58-8a2d-982a675b42bc] Matched-Stub-Name: [] 2023-02-20 013018.880 Proxying: GET http://mitm.it/favicon.ico 2023-02-20 013019.430 Request received: 127.0.0.1 - GET /favicon.ico (via browser proxy request) Host: [mitm.it] Connection: [keep-alive] User-Agent: [Mozilla/5.0 (Linux; Android 11; sdk_gphone_x86_64_arm64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36] Accept: [image/webp,image/apng,image/*,*/*;q=0.8] Referer: [http://mitm.it/] Accept-Encoding: [gzip, deflate] Accept-Language: [en-US,en;q=0.9] Matched response definition: { "status" : 200 } Response: HTTP/1.1 403 Content-Type: [application/xml] Date: [Sun, 19 Feb 2023 200020 GMT] Server: [AmazonS3] X-Cache: [Error from cloudfront] Via: [1.1 118076cb0590da05a105adf088b49476.cloudfront.net (CloudFront)] X-Amz-Cf-Pop: [DEL54-C1] X-Amz-Cf-Id: [ppwoANi4jVLdso8EkB60v7s2-CwZckhepQpC11MYJyqjycHF8ExYBg==] 2023-02-20 013034.353 Request received: 127.0.0.1 - GET / (via browser proxy request) Host: [example.com] Connection: [keep-alive] Upgrade-Insecure-Requests: [1] User-Agent: [Mozilla/5.0 (Linux; Android 11; sdk_gphone_x86_64_arm64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36] Accept: [text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9] Accept-Encoding: [gzip, deflate] Accept-Language: [en-US,en;q=0.9] Matched response definition: { "status" : 500, "base64Body" : "TmV0d29yayBmYWlsdXJlIHRyeWluZyB0byBtYWtlIGEgcHJveGllZCByZXF1ZXN0IGZyb20gV2lyZU1vY2sgdG8gMTkyLjE2OC4xLjM4Lw0KVGFyZ2V0IGhvc3QgaXMgbm90IHNwZWNpZmllZA==" } Response: HTTP/1.1 500 Matched-Stub-Id: [1f44f953-f075-45ad-acdf-c9a0362e72c8] Matched-Stub-Name: [] 2023-02-20 013034.700 Proxying: GET http://example.com/favicon.ico 2023-02-20 013035.138 Request received: 127.0.0.1 - GET /favicon.ico (via browser proxy request) Host: [example.com] Connection: [keep-alive] User-Agent: [Mozilla/5.0 (Linux; Android 11; sdk_gphone_x86_64_arm64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Mobile Safari/537.36] Accept: [image/webp,image/apng,image/*,*/*;q=0.8] Referer: [http://example.com/] Accept-Encoding: [gzip, deflate] Accept-Language: [en-US,en;q=0.9] Matched response definition: { "status" : 200 } Response: HTTP/1.1 404 Content-Encoding: [gzip] Accept-Ranges: [bytes] Age: [586122] Cache-Control: [max-age=604800] Content-Type: [text/html; charset=UTF-8] Date: [Sun, 19 Feb 2023 200035 GMT] Expires: [Sun, 26 Feb 2023 200035 GMT] Last-Modified: [Mon, 13 Feb 2023 011153 GMT] Server: [ECS (nyb/1D25)] Vary: [Accept-Encoding] X-Cache: [404-HIT] Content-Length: [648]
image.png
r

Rob Elliot

02/19/2023, 8:07 PM
Do you have a proxy configured for the Windows host system?
s

Shruti Sagar Mishra

02/19/2023, 8:10 PM
no
do we even need that'
r

Rob Elliot

02/19/2023, 8:10 PM
No, definitely not
I'm a bit baffled as to what is going on.
s

Shruti Sagar Mishra

02/19/2023, 8:10 PM
we would be proxying only the emulator
r

Rob Elliot

02/19/2023, 8:13 PM
I don't understand where WireMock is getting 192.168.1.38 from. You are requesting https://example.com proxied by http://127.0.0.1:8080 , which is WireMock. The request is being intercepted by WireMock. However, instead of calling example.com WireMock is... calling 192.168.1.38 ?!
Do you have any stubs configured? Visit http://localhost:8080/__admin/mappings and tell me what it says
s

Shruti Sagar Mishra

02/19/2023, 8:27 PM
this is my local ip
192.168.1.38
r

Rob Elliot

02/19/2023, 8:28 PM
Yes, but I don't understand why WireMock is trying to make requests to it.
s

Shruti Sagar Mishra

02/19/2023, 8:29 PM
with http i got
image.png
r

Rob Elliot

02/19/2023, 8:30 PM
Do you have any stubs configured? Visit http://localhost:8080/__admin/mappings and tell me what it says
s

Shruti Sagar Mishra

02/19/2023, 8:31 PM
image.png
with https it say
image.png
r

Rob Elliot

02/19/2023, 8:32 PM
Hang on, that's not what you showed me before
3 Views