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

Bugfix/header modifier doesn't work for user agent property #516

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface ProxyServerWrapper {

void setCaptureContent(boolean captureContent);

void addHeader(String name, String value);
void addHeader(String name, String value, Boolean override);

void stop() throws ProxyException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ public class HeaderModifier implements CollectorJob {

private static final String PARAM_VALUE = "value";

private static final String PARAM_OVERRIDE = "override";

private final WebCommunicationWrapper webCommunicationWrapper;

private String key;

private String value;

private Boolean override;

public HeaderModifier(WebCommunicationWrapper webCommunicationWrapper) {
this.webCommunicationWrapper = webCommunicationWrapper;
}
Expand All @@ -46,7 +50,7 @@ public CollectorStepResult collect() throws ProcessingException {
if (!webCommunicationWrapper.isUseProxy()) {
throw new ProcessingException("Cannot modify header without using proxy");
}
webCommunicationWrapper.getProxyServer().addHeader(key, value);
webCommunicationWrapper.getProxyServer().addHeader(key, value, override);
webCommunicationWrapper.getHttpRequestExecutor().addHeader(key, value);
return CollectorStepResult.newModifierResult();
}
Expand All @@ -59,6 +63,11 @@ public void setParameters(Map<String, String> params) throws ParametersException
} else {
throw new ParametersException("Missing Key or Value on Header Modifier");
}
if (params.containsKey(PARAM_OVERRIDE)) {
override = Boolean.parseBoolean(params.get(PARAM_OVERRIDE));
} else {
override = false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void testCollect() throws Exception {
headerModifier.setParameters(ImmutableMap.of("key", "header", "value", "value1"));
headerModifier.collect();

verify(proxyServer).addHeader("header", "value1");
verify(proxyServer).addHeader("header", "value1", false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way to test the override approach?
Maybe if not the unit test, some integration test (e.g. page displaying User-Agent header value?)

verify(requestExecutor).addHeader("header", "value1");
}

Expand Down
4 changes: 4 additions & 0 deletions documentation/src/main/wiki/HeaderModifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ In order to use this modifier it must be declared before the open module in the
| --------- | ----- | ----------- | --------- |
| `key` | x | Key for the header | yes |
| `value` | y | Value for the header | yes |
| `override` | `true`/`false` | Replace existing header instead of adding new one | no (default false) |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the case of using the Header Modifier without override=true?
Is that the case, that the user wants to have more than one header's value?
If that's the case, override=true should be the default option and eventually let the user add more header values using override=false.
Could you please also make it more clear, in the docs, that override=false means, that if the header already occurs in the request, it will not be modified (like with User-Agent example, where Chrome instance sets it).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Override false is use when you want add new header, true option is used to change headers that are add by Chrome instance. This is why false is default strategy.

I'll make documentation more clear about that.


##### Example Usage

Expand All @@ -23,6 +24,9 @@ In order to use this modifier it must be declared before the open module in the
<test name="header-modify-test" useProxy="rest">
<collect>
...
<header key="User-Agent" value="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20120101 Firefox/33.0"
override="true"/>
<header key="Accept" value="application/json" override="true"/>
<header key="Authorization" value="Basic emVuT2FyZXVuOnozbkdAckQZbiE=" />
...
<open />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,46 @@

import com.cognifide.aet.job.api.collector.ProxyServerWrapper;
import com.cognifide.aet.proxy.exceptions.UnableToAddHeaderException;
import com.cognifide.aet.proxy.headers.HeaderRequestFactory;
import com.github.detro.browsermobproxyclient.BMPCProxy;
import com.github.detro.browsermobproxyclient.exceptions.BMPCUnableToConnectException;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Date;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.browsermob.core.har.Har;
import org.json.JSONObject;
import org.openqa.selenium.Proxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RestProxyServer implements ProxyServerWrapper {
import java.io.IOException;
import java.util.Date;

private static final String HTTP = "http";
public class RestProxyServer implements ProxyServerWrapper {

public static final int STATUS_CODE_OK = 200;

private BMPCProxy server;
private final BMPCProxy server;

private boolean captureContent;

private boolean captureHeaders;

private RestProxyManager proxyManager;

private final HeaderRequestFactory requestFactory;

private static final Logger LOGGER = LoggerFactory.getLogger(RestProxyServer.class);

public RestProxyServer(BMPCProxy bmpcProxy, RestProxyManager restProxyManager) {
this.server = bmpcProxy;
this.proxyManager = restProxyManager;
this.requestFactory = new HeaderRequestFactory(bmpcProxy);
}

@Override
public Proxy seleniumProxy() throws UnknownHostException {
public Proxy seleniumProxy() {
return server.asSeleniumProxy()
.setHttpProxy(String.format("%s:%d", proxyManager.getServer(), getPort()))
.setSslProxy(String.format("%s:%d", proxyManager.getServer(), getPort()));
Expand Down Expand Up @@ -95,18 +94,10 @@ public void setCaptureContent(boolean captureContent) {
}

@Override
public void addHeader(String name, String value) {
public void addHeader(String name, String value, Boolean override) {
CloseableHttpClient httpClient = HttpClients.createSystem();
try {
URIBuilder uriBuilder = new URIBuilder().setScheme(HTTP).setHost(server.getAPIHost())
.setPort(server.getAPIPort());
// Request BMP to add header
HttpPost request = new HttpPost(uriBuilder.setPath(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://github.com/lightbody/browsermob-proxy#rest-api docs:
POST /proxy/[port]/headers

Set and override HTTP Request headers

Do those changes mean, that this is not true? There is even an example of setting the User-Agent header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this doesn't work anymore. You can't use this to change some headers like User-agent. Using this you can only add new header or override header that you add before.

String.format("/proxy/%d/headers", server.getProxyPort())).build());
request.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put(name, value);
request.setEntity(new StringEntity(json.toString()));
HttpPost request = requestFactory.create(name, value, override);
// Execute request
CloseableHttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
Expand All @@ -133,5 +124,4 @@ public void stop() {
server.close();
proxyManager.detach(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* AET
*
* Copyright (C) 2013 Cognifide Limited
*
* 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.cognifide.aet.proxy.headers;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

public class AddHeader implements HeaderRequestStrategy {

private final String apiHost;

private final int apiPort;

private final int proxyPort;

public AddHeader(String apiHost, int apiPort, int proxyPort) {
this.apiHost = apiHost;
this.apiPort = apiPort;
this.proxyPort = proxyPort;
}

@Override
public HttpPost createRequest(String name, String value)
throws URISyntaxException, UnsupportedEncodingException {
URIBuilder uriBuilder = new URIBuilder().setScheme("http")
.setHost(apiHost).setPort(apiPort);
HttpPost request = new HttpPost(uriBuilder.setPath(
String.format("/proxy/%d/headers", proxyPort)).build());
request.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put(name, value);
request.setEntity(new StringEntity(json.toString()));
return request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* AET
*
* Copyright (C) 2013 Cognifide Limited
*
* 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.cognifide.aet.proxy.headers;

import com.github.detro.browsermobproxyclient.BMPCProxy;
import org.apache.http.client.methods.HttpPost;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

public class HeaderRequestFactory {

private final BMPCProxy server;

public HeaderRequestFactory(BMPCProxy server) {
this.server = server;
}

public HttpPost create(String name, String value, Boolean override) throws UnsupportedEncodingException, URISyntaxException {
HeaderRequestStrategy headerRequestStrategy;
if (override) {
headerRequestStrategy = new OverrideHeader(server.getAPIHost(), server.getAPIPort(),
server.getProxyPort());
} else {
headerRequestStrategy = new AddHeader(server.getAPIHost(), server.getAPIPort(),
server.getProxyPort());
}
return headerRequestStrategy.createRequest(name, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* AET
*
* Copyright (C) 2013 Cognifide Limited
*
* 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.cognifide.aet.proxy.headers;

import org.apache.http.client.methods.HttpPost;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

public interface HeaderRequestStrategy {
HttpPost createRequest(String name, String value) throws URISyntaxException, UnsupportedEncodingException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* AET
*
* Copyright (C) 2013 Cognifide Limited
*
* 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.cognifide.aet.proxy.headers;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

public class OverrideHeader implements HeaderRequestStrategy {

private final String apiHost;

private final int apiPort;

private final int proxyPort;

public OverrideHeader(String apiHost, int apiPort, int proxyPort) {
this.apiHost = apiHost;
this.apiPort = apiPort;
this.proxyPort = proxyPort;
}

@Override
public HttpPost createRequest(String name, String value)
throws URISyntaxException, UnsupportedEncodingException {
URIBuilder uriBuilder = new URIBuilder().setScheme("http")
.setHost(apiHost).setPort(apiPort);
HttpPost request = new HttpPost(uriBuilder.setPath(
String.format("/proxy/%d/filter/request", proxyPort)).build());
request.setHeader("Content-Type", "text/plain");
String data = String.format(
"request.headers().remove('%s'); request.headers().add('%s', '%s');",
name, name, value);
request.setEntity(new StringEntity(data));
return request;
}
}