Skip to content

Commit

Permalink
Merge pull request #516 from Cognifide/bugfix/header-modifier-doesn't…
Browse files Browse the repository at this point in the history
…-work-for-User-Agent-property

Bugfix/header modifier doesn't work for user agent property
  • Loading branch information
tkaik authored Aug 12, 2019
2 parents ec6d9db + 39b862d commit 03cd733
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ All notable changes to AET will be documented in this file.
- [PR-480](https://github.com/Cognifide/aet/pull/480) Test summary stats on the main report page. ([#474](https://github.com/Cognifide/aet/issues/474))
- [PR-459](https://github.com/Cognifide/aet/pull/459) Print more meaningful error messages when suite.xml is malformed ([#436](https://github.com/Cognifide/aet/issues/436))
- [PR-517](https://github.com/Cognifide/aet/pull/517) Single URL rerun fixed for named URLs ([#487](https://github.com/Cognifide/aet/issues/487))
- [PR-516](https://github.com/Cognifide/aet/pull/516) Added option to change headers added by Chrome instance. ([#515](https://github.com/Cognifide/aet/issues/515))

## Version 3.2.2

Expand Down
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);
verify(requestExecutor).addHeader("header", "value1");
}

Expand Down
8 changes: 8 additions & 0 deletions documentation/src/main/wiki/HeaderModifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ 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` | Override option should be set at `true` when your want replace header added by Chrome instance, otherwise it should by `false` | no (default false) |

Important note.

When you want add another value to Accept header and leave the default value create by Chrome instance, you can use `override=false`.

##### Example Usage

Expand All @@ -23,6 +28,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
@@ -0,0 +1,41 @@
<%--
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.
--%>
<%@ page import="java.util.*" %>
<html>

<head>
<title>Headers test</title>
</head>

<body>
<h2>HTTP Request Headers Received</h2>
<table>
<% Enumeration enumeration = request.getHeaderNames(); while (enumeration.hasMoreElements())
{
String name=(String) enumeration.nextElement();
String value = request.getHeader(name); %>
<tr>
<td><%=name %></td>
<td id="<%=name %>"><%=value %></td>
</tr>
<% } %>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
@Modules(GuiceModule.class)
public class HomePageTilesTest {

private static final int TESTS = 149;
private static final int TESTS = 152;

private static final int EXPECTED_TESTS_SUCCESS = 85;
private static final int EXPECTED_TESTS_SUCCESS = 88;

private static final int EXPECTED_TESTS_CONDITIONALLY_PASSED = 11;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ Feature: Tests Results Filtering
Scenario: Filtering Tests Results: header
Given I have opened sample tests report page
When I search for tests containing "header"
Then There are 1 tiles visible
And Statistics text contains "1 ( 0 / 0 / 1 (0) / 0 )"
Then There are 4 tiles visible
And Statistics text contains "4 ( 0 / 0 / 4 (0) / 0 )"

Scenario: Filtering Tests Results: sleep-modifier
Given I have opened sample tests report page
Expand Down
51 changes: 51 additions & 0 deletions integration-tests/test-suite/partials/header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,56 @@
<url href="modifiers/header/private/basic_auth_required.jsp" />
</urls>
</test>
<test name="S-modifier-User-Agent-Header" 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"/>
<open />
<resolution width="1920"/>
<sleep duration="1000"/>
<screen name="Desktop"/>
<source/>
</collect>
<compare>
<screen comparator="layout"/>
<source comparator="source"/>
</compare>
<urls>
<url href="modifiers/header/show_headers.jsp" />
</urls>
</test>
<test name="S-modifier-accept-Header-replace" useProxy="rest">
<collect>
<header key="accept" value="test" override="true"/>
<open />
<resolution width="1920"/>
<sleep duration="1000"/>
<screen name="Desktop"/>
<source/>
</collect>
<compare>
<screen comparator="layout"/>
<source comparator="source"/>
</compare>
<urls>
<url href="modifiers/header/show_headers.jsp" />
</urls>
</test>
<test name="S-modifier-accept-Header-add" useProxy="rest">
<collect>
<header key="accept" value="test" override="false"/>
<open />
<resolution width="1920"/>
<sleep duration="1000"/>
<screen name="Desktop"/>
<source/>
</collect>
<compare>
<screen comparator="layout"/>
<source comparator="source"/>
</compare>
<urls>
<url href="modifiers/header/show_headers.jsp" />
</urls>
</test>
<!-- Header-Modifier END -->
</suite>
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(
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);
}
}
Loading

0 comments on commit 03cd733

Please sign in to comment.