question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apache HttpClient + WireMock

See original GitHub issue

Hi,

I have observed strange behavior when accessing a mocked server instance of WireMock with the Apache HttpClient. When I debug the test, and go to the mocked server address in my browser, I got the expected response. However accessing the same address with the Apache HttpClient leads to a 404 response.

Necessary Information about the Test (not the whole test, only WireMock related configurations):

 @RunWith(SpringJUnit4ClassRunner.class)
 public class Test {

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(9999);

    @Before
    public void reset() {
        WireMock.reset();
    }

    @Test
    public void test() {
        String request = "Any Request";
        stubFor(get(urlEqualTo("/")).willReturn(aResponse().withStatus(200).withBody(response.getBytes())));

        verify(postRequestedFor(urlEqualTo("/"))
            .withHeader("Content-Type", equalTo("text/xml; charset=UTF-8"))
            .withHeader("SOAPAction", equalTo("")).withRequestBody(equalTo(request)));
     }
 }

Nice to know is, that the verification is valid. So the mock is obviously called correctly.

HttpClient-Code calling the mocked server address:

HttpClient client = new DefaultHttpClient();
ByteArrayEntity requestEntity = new ByteArrayEntity(_requestStr_.getBytes());
requestEntity.setContentType("text/xml; charset=UTF-8");
requestEntity.setContentEncoding("UTF-8");

HttpPost httpPost = new HttpPost("http://localhost:9999");
httpPost.addHeader("SOAPAction", "");
httpPost.setEntity(requestEntity);
HttpResponse response = client.execute(httpPost);

HttpEntity entity = response.getEntity();
if (entity != null) {
    InputStream in = entity.getContent();
    try {
        System.out.print(new String(IOUtils.toByteArray(in)));
    } finally {
        in.close();
    }
}

Now I got the 404 Error html page from jetty, although the browser shows the right mocked response:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 404 NOT_FOUND</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /. Reason:
<pre>    NOT_FOUND</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                
<br/>                                                

</body>
</html>

Any suggestions why I got the right answer in my firefox browser and got a 404 via the Apache HttpClient? Perhaps are there any headers which are assumed to be in the request in order to let WireMock match the called URL and provide a declared response?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:18

github_iconTop GitHub Comments

1reaction
tomakehurstcommented, Jul 2, 2013

I’m lacking access to a computer at the moment so I can’t test this but it could be because your stub definition has a trailing slash but your http post doesn’t

BTW, your @before block is unnecessary as reset() is called by the rule.

0reactions
sensoryorgancommented, Feb 3, 2015

@tomakehurst Is it possible to exclude certain query parameter while running wiremock standalone? tobe more precise while invoking an url e.g. http:\search.twitter.com?userid=1236524&name=stella&date=… Can I do it in a way so that for all the urls invoked “userid” would not be considered while recording the response. So that if userid changes and rest of the url remains same, wiremock wont record the response again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apache HttpClient + WireMock · Issue #41 - GitHub
Hi, I have observed strange behavior when accessing a mocked server instance of WireMock with the Apache HttpClient. When I debug the test, ......
Read more >
WireMock doesn't receive the HTTP request issued by HTTP ...
I am using wireMock to mock the service and HTTP client to issue a request to dates-services-svc , but the requests issued by...
Read more >
Introduction to WireMock - Baeldung
A quick and practical guide to stubbing REST APIs with WireMock. ... This tutorial makes use of the Apache HttpClient API to represent...
Read more >
https - WireMock
WireMock can optionally accept requests over HTTPS. By default it will serve its own self-signed TLS certificate.
Read more >
Mocking a HTTP server with WireMock - Kohei Nozaki's blog
WireMock dependency brings many its dependencies, but I guess that are not necessary because it is classified as standalone, so I just add ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found