Permalink
Browse files

Merge branch 'https-bind-address-bug'

2 parents c7597a7 + bfb5bbe commit 9d3485942960ded496401e9c581ad3ad29868623 @tomakehurst committed Jan 6, 2017
@@ -78,6 +78,7 @@
if (options.httpsSettings().enabled()) {
httpsConnector = createHttpsConnector(
+ options.bindAddress(),
options.httpsSettings(),
options.jettySettings(),
networkTrafficListenerAdapter);
@@ -158,16 +159,18 @@ private ServerConnector createHttpConnector(
HttpConfiguration httpConfig = createHttpConfig(jettySettings);
ServerConnector connector = createServerConnector(
+ bindAddress,
jettySettings,
port,
listener,
new HttpConnectionFactory(httpConfig)
);
- connector.setHost(bindAddress);
+
return connector;
}
private ServerConnector createHttpsConnector(
+ String bindAddress,
HttpsSettings httpsSettings,
JettySettings jettySettings,
NetworkTrafficListener listener) {
@@ -190,8 +193,8 @@ private ServerConnector createHttpsConnector(
final int port = httpsSettings.port();
-
return createServerConnector(
+ bindAddress,
jettySettings,
port,
listener,
@@ -212,7 +215,10 @@ private HttpConfiguration createHttpConfig(JettySettings jettySettings) {
return httpConfig;
}
- private ServerConnector createServerConnector(JettySettings jettySettings, int port, NetworkTrafficListener listener, ConnectionFactory... connectionFactories) {
+ private ServerConnector createServerConnector(String bindAddress,
+ JettySettings jettySettings,
+ int port, NetworkTrafficListener listener,
+ ConnectionFactory... connectionFactories) {
int acceptors = jettySettings.getAcceptors().or(2);
NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(
jettyServer,
@@ -232,6 +238,8 @@ private ServerConnector createServerConnector(JettySettings jettySettings, int p
setJettySettings(jettySettings, connector);
+ connector.setHost(bindAddress);
+
return connector;
}
@@ -15,9 +15,11 @@
*/
package com.github.tomakehurst.wiremock;
+import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
import java.net.Inet4Address;
import java.net.InetAddress;
@@ -26,6 +28,12 @@
import java.util.Collections;
import java.util.Enumeration;
+import com.github.tomakehurst.wiremock.common.HttpClientUtils;
+import com.github.tomakehurst.wiremock.http.HttpClientFactory;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.RequestBuilder;
+import org.apache.http.util.EntityUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -41,26 +49,25 @@
private String nonBindAddress;
private WireMockServer wireMockServer;
- @Test
- public void shouldRespondInTheBindAddressOnly() throws Exception {
- executeGetIn(localhost);
- try {
- executeGetIn(nonBindAddress);
- Assert.fail("Should not accept the connection in [" + nonBindAddress + "]");
- } catch (Exception ex) {
- }
- }
+ final HttpClient client = HttpClientFactory.createClient();
@Before
public void prepare() throws Exception {
nonBindAddress = getIpAddressOtherThan(localhost);
if (nonBindAddress == null) {
- Assert.fail("Impossible to validate the binding address. This machine has only a one Ip address ["
+ fail("Impossible to validate the binding address. This machine has only a one Ip address ["
+ localhost + "]");
}
- wireMockServer = new WireMockServer(wireMockConfig().bindAddress(localhost).dynamicPort());
+ wireMockServer = new WireMockServer(wireMockConfig()
+ .bindAddress(localhost)
+ .dynamicPort()
+ .dynamicHttpsPort()
+ );
wireMockServer.start();
+
+ wireMockServer.stubFor(get(urlPathEqualTo("/bind-test"))
+ .willReturn(aResponse().withStatus(200)));
}
@After
@@ -70,11 +77,43 @@ public void stop() {
}
}
+ @Test
+ public void shouldRespondInTheBindAddressOnlyOnHttp() throws Exception {
+ executeGetIn(localhost);
+ try {
+ executeGetIn(nonBindAddress);
+ fail("Should not accept HTTP connection to [" + nonBindAddress + "]");
+ } catch (Exception ex) {
+ }
+ }
+
+ @Test
+ public void shouldRespondInTheBindAddressOnlyOnHttps() throws Exception {
+ int localhostStatus = getStatusViaHttps(localhost);
+ assertThat(localhostStatus, is(200));
+
+ try {
+ getStatusViaHttps(nonBindAddress);
+ fail("Should not accept HTTPS connection to [" + nonBindAddress + "]");
+ } catch (Exception e) {
+ }
+ }
+
+ private int getStatusViaHttps(String host) throws Exception {
+ HttpResponse localhostResponse = client.execute(RequestBuilder
+ .get("https://" + host + ":" + wireMockServer.httpsPort() + "/bind-test")
+ .build()
+ );
+
+ int status = localhostResponse.getStatusLine().getStatusCode();
+ EntityUtils.consume(localhostResponse.getEntity());
+ return status;
+ }
+
private void executeGetIn(String address) {
WireMockTestClient wireMockClient = new WireMockTestClient(wireMockServer.port(), address);
- wireMockClient.addResponse(MappingJsonSamples.BASIC_MAPPING_REQUEST_WITH_RESPONSE_HEADER);
- WireMockResponse response = wireMockClient.get("/a/registered/resource");
- assertThat(response.statusCode(), is(401));
+ WireMockResponse response = wireMockClient.get("/bind-test");
+ assertThat(response.statusCode(), is(200));
}
private String getIpAddressOtherThan(String lopbackAddress) throws SocketException {

0 comments on commit 9d34859

Please sign in to comment.