Apache HttpClient + WireMock
See original GitHub issueHi,
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:
- Created 10 years ago
- Comments:18
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.
@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.