forked from wiremock/wiremock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow stubbing & recording of forward proxy https traffic
This allows WireMock to act as a forward (browser) proxy for HTTPS as well as HTTP origins whilst stubbing & recording. Step towards implementing wiremock#401.
- Loading branch information
Showing
8 changed files
with
311 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...rc/main/java/com/github/tomakehurst/wiremock/jetty94/ManInTheMiddleSslConnectHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.github.tomakehurst.wiremock.jetty94; | ||
|
||
import org.eclipse.jetty.io.Connection; | ||
import org.eclipse.jetty.io.EndPoint; | ||
import org.eclipse.jetty.server.HttpConnection; | ||
import org.eclipse.jetty.server.Request; | ||
import org.eclipse.jetty.server.SslConnectionFactory; | ||
import org.eclipse.jetty.server.handler.AbstractHandler; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
import static org.eclipse.jetty.http.HttpMethod.CONNECT; | ||
|
||
/** | ||
* A Handler for the HTTP CONNECT method that, instead of opening up a | ||
* TCP tunnel between the downstream and upstream sockets, | ||
* 1) captures the | ||
* and | ||
* 2) turns the connection into an SSL connection allowing this server to handle | ||
* it. | ||
* | ||
* | ||
*/ | ||
class ManInTheMiddleSslConnectHandler extends AbstractHandler { | ||
|
||
private final SslConnectionFactory sslConnectionFactory; | ||
|
||
ManInTheMiddleSslConnectHandler(SslConnectionFactory sslConnectionFactory) { | ||
this.sslConnectionFactory = sslConnectionFactory; | ||
} | ||
|
||
@Override | ||
protected void doStart() throws Exception { | ||
super.doStart(); | ||
sslConnectionFactory.start(); | ||
} | ||
|
||
@Override | ||
protected void doStop() throws Exception { | ||
super.doStop(); | ||
sslConnectionFactory.stop(); | ||
} | ||
|
||
@Override | ||
public void handle( | ||
String target, | ||
Request baseRequest, | ||
HttpServletRequest request, | ||
HttpServletResponse response | ||
) throws IOException { | ||
if (CONNECT.is(request.getMethod())) { | ||
baseRequest.setHandled(true); | ||
handleConnect(baseRequest, response); | ||
} | ||
} | ||
|
||
private void handleConnect( | ||
Request baseRequest, | ||
HttpServletResponse response | ||
) throws IOException { | ||
sendConnectResponse(response); | ||
final HttpConnection transport = (HttpConnection) baseRequest.getHttpChannel().getHttpTransport(); | ||
EndPoint endpoint = transport.getEndPoint(); | ||
Connection connection = sslConnectionFactory.newConnection(transport.getConnector(), endpoint); | ||
endpoint.setConnection(connection); | ||
connection.onOpen(); | ||
} | ||
|
||
private void sendConnectResponse(HttpServletResponse response) throws IOException { | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
response.getOutputStream().close(); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
java8/src/test/java/com/github/tomakehurst/wiremock/Http2BrowserProxyAcceptanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright (C) 2011 Thomas Akehurst | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.tomakehurst.wiremock; | ||
|
||
import com.github.tomakehurst.wiremock.common.SingleRootFileSource; | ||
import com.github.tomakehurst.wiremock.junit.WireMockClassRule; | ||
import com.github.tomakehurst.wiremock.testsupport.TestFiles; | ||
import com.github.tomakehurst.wiremock.testsupport.WireMockTestClient; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.ClassRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.*; | ||
import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked; | ||
import static com.github.tomakehurst.wiremock.core.WireMockApp.FILES_ROOT; | ||
import static com.github.tomakehurst.wiremock.core.WireMockApp.MAPPINGS_ROOT; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class Http2BrowserProxyAcceptanceTest { | ||
|
||
private static final String CERTIFICATE_NOT_TRUSTED_BY_TEST_CLIENT = TestFiles.KEY_STORE_PATH; | ||
|
||
@ClassRule | ||
public static WireMockClassRule target = new WireMockClassRule(wireMockConfig() | ||
.httpDisabled(true) | ||
.keystorePath(CERTIFICATE_NOT_TRUSTED_BY_TEST_CLIENT) | ||
.dynamicHttpsPort() | ||
); | ||
|
||
@Rule | ||
public WireMockClassRule instanceRule = target; | ||
|
||
private WireMockServer proxy; | ||
private WireMockTestClient testClient; | ||
|
||
@Before | ||
public void addAResourceToProxy() { | ||
testClient = new WireMockTestClient(target.httpsPort()); | ||
|
||
proxy = new WireMockServer(wireMockConfig() | ||
.dynamicPort() | ||
.fileSource(new SingleRootFileSource(setupTempFileRoot())) | ||
.enableBrowserProxying(true)); | ||
proxy.start(); | ||
} | ||
|
||
@After | ||
public void stopServer() { | ||
if (proxy.isRunning()) { | ||
proxy.stop(); | ||
} | ||
} | ||
|
||
@Test | ||
public void canProxyHttpsInBrowserProxyMode() throws Exception { | ||
target.stubFor(get(urlEqualTo("/whatever")).willReturn(aResponse().withBody("Got it"))); | ||
|
||
assertThat(testClient.getViaProxy(target.url("/whatever"), proxy.port()).content(), is("Got it")); | ||
} | ||
|
||
@Test | ||
public void canStubHttpsInBrowserProxyMode() throws Exception { | ||
target.stubFor(get(urlEqualTo("/stubbed")).willReturn(aResponse().withBody("Should Not Be Returned"))); | ||
proxy.stubFor(get(urlEqualTo("/stubbed")).willReturn(aResponse().withBody("Stubbed Value"))); | ||
target.stubFor(get(urlEqualTo("/not_stubbed")).willReturn(aResponse().withBody("Should be served from target"))); | ||
|
||
assertThat(testClient.getViaProxy(target.url("/stubbed"), proxy.port()).content(), is("Stubbed Value")); | ||
assertThat(testClient.getViaProxy(target.url("/not_stubbed"), proxy.port()).content(), is("Should be served from target")); | ||
} | ||
|
||
@Test | ||
public void canRecordHttpsInBrowserProxyMode() throws Exception { | ||
|
||
// given | ||
proxy.startRecording(target.baseUrl()); | ||
String recordedEndpoint = target.url("/record_me"); | ||
|
||
// and | ||
target.stubFor(get(urlEqualTo("/record_me")).willReturn(aResponse().withBody("Target response"))); | ||
|
||
// then | ||
assertThat(testClient.getViaProxy(recordedEndpoint, proxy.port()).content(), is("Target response")); | ||
|
||
// when | ||
proxy.stopRecording(); | ||
|
||
// and | ||
target.stop(); | ||
|
||
// then | ||
assertThat(testClient.getViaProxy(recordedEndpoint, proxy.port()).content(), is("Target response")); | ||
} | ||
|
||
private static File setupTempFileRoot() { | ||
try { | ||
File root = java.nio.file.Files.createTempDirectory("wiremock").toFile(); | ||
new File(root, MAPPINGS_ROOT).mkdirs(); | ||
new File(root, FILES_ROOT).mkdirs(); | ||
return root; | ||
} catch (IOException e) { | ||
return throwUnchecked(e, File.class); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.