Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore proxying capability #209

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- Restore proxying capability, in the previous version the proxy settings were ignored. [#209](https://github.com/mozilla/zest/pull/209)

## [0.14.0] - 2019-11-07
### Fixed
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/mozilla/zest/impl/ComponentsHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ComponentsHttpClient implements ZestHttpClient {
private final HttpClient httpClient;
private final HttpClientContext httpContext;
private final RequestConfig defaultRequestConfig;
private RequestConfig requestConfig;
private ZestOutputWriter zestOutputWriter;

/**
Expand All @@ -76,6 +77,7 @@ class ComponentsHttpClient implements ZestHttpClient {
// it will also become the default in following HttpClient versions.
.setCookieSpec(CookieSpecs.STANDARD)
.build();
this.requestConfig = defaultRequestConfig;
}

private static HttpClient getHttpClient(boolean skipSSLCertificateCheck) {
Expand Down Expand Up @@ -118,15 +120,13 @@ public void addAuthentication(ZestAuthentication zestAuthentication) {

@Override
public void setProxy(String host, int port) {
RequestConfig localRequestConfig;
synchronized (defaultRequestConfig) {
localRequestConfig =
RequestConfig.copy(defaultRequestConfig)
.setProxy(new HttpHost(host, port))
.build();
}
synchronized (httpContext) {
httpContext.setRequestConfig(localRequestConfig);
requestConfig =
host == null || host.isEmpty()
? defaultRequestConfig
: RequestConfig.copy(defaultRequestConfig)
.setProxy(new HttpHost(host, port))
.build();
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ public ZestResponse send(ZestRequest req) throws IOException {
throw new IllegalArgumentException("Method not supported: " + req.getMethod());
}

method.setConfig(defaultRequestConfig);
method.setConfig(requestConfig);
setHeaders(method, req.getHeaders());

for (ZestCookie zestCookie : req.getCookies()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.byLessThan;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.net.URL;
import java.util.HashMap;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mozilla.zest.core.v1.ZestRequest;
import org.mozilla.zest.core.v1.ZestResponse;
Expand All @@ -26,6 +31,10 @@ public class ZestBasicRunnerUnitTest extends ServerBasedTest {

private static final String PATH_SERVER_FILE = "/test";

@Rule
public WireMockRule proxy =
new WireMockRule(options().dynamicPort().enableBrowserProxying(true), false);

@Before
public void before() {
server.stubFor(
Expand Down Expand Up @@ -124,4 +133,30 @@ public void shouldSendPutRequestWithBody() throws Exception {
server.verify(
putRequestedFor(urlMatching(PATH_SERVER_FILE)).withRequestBody(equalTo(data)));
}

@Test
public void shouldSendRequestThroughConfiguredProxy() throws Exception {
ZestScript script = new ZestScript();
ZestRequest request = new ZestRequest();
URL url = new URL(getServerUrl(PATH_SERVER_FILE));
String method = "GET";
request.setMethod(method);
request.setUrl(url);
script.add(request);
ZestBasicRunner runner = new ZestBasicRunner();
runner.setProxy("localhost", proxy.port());
// When
runner.run(script, new HashMap<String, String>());
// Then
request = runner.getLastRequest();
assertThat(request).isNotNull();
assertThat(request.getMethod()).isEqualTo(method);
assertThat(request.getUrl()).isEqualTo(url);
proxy.verify(
getRequestedFor(urlMatching(PATH_SERVER_FILE))
.withHeader("Host", matching(getHostPort())));
server.verify(
getRequestedFor(urlMatching(PATH_SERVER_FILE))
.withHeader("Host", matching(getHostPort())));
}
}