-
Notifications
You must be signed in to change notification settings - Fork 49
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
Changes from 7 commits
e2b89b2
3e7a604
9dc4602
9e6f2d2
dae997e
4381d91
b6c2a90
7caa37a
26519b9
c0092fb
e5b2c24
da55a5d
44a0137
a58a352
4b11a3e
39b862d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the case of using the Header Modifier without There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
|
@@ -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 /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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())); | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to https://github.com/lightbody/browsermob-proxy#rest-api docs:
Do those changes mean, that this is not true? There is even an example of setting the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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; | ||
} | ||
} |
There was a problem hiding this comment.
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?)