Skip to content

Commit 638aadd

Browse files
author
Artem Eroshenko
committed
Merge pull request #6 from eroshenkoam/master
Fix command encoding problem
2 parents 665b95d + 7dc3fdc commit 638aadd

File tree

4 files changed

+95
-30
lines changed

4 files changed

+95
-30
lines changed

Diff for: pom.xml

+27-27
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,33 @@
5858
<system>Jenkins</system>
5959
<url>http://ci.qatools.ru/</url>
6060
</ciManagement>
61-
61+
62+
<dependencyManagement>
63+
<dependencies>
64+
<dependency>
65+
<groupId>commons-io</groupId>
66+
<artifactId>commons-io</artifactId>
67+
<version>2.4</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.apache.commons</groupId>
71+
<artifactId>commons-lang3</artifactId>
72+
<version>3.4</version>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>junit</groupId>
77+
<artifactId>junit</artifactId>
78+
<version>4.12</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.hamcrest</groupId>
82+
<artifactId>hamcrest-all</artifactId>
83+
<version>1.3</version>
84+
</dependency>
85+
</dependencies>
86+
</dependencyManagement>
87+
6288
<build>
6389
<plugins>
6490
<plugin>
@@ -132,30 +158,4 @@
132158
</pluginManagement>
133159
</build>
134160

135-
<dependencyManagement>
136-
<dependencies>
137-
<dependency>
138-
<groupId>commons-io</groupId>
139-
<artifactId>commons-io</artifactId>
140-
<version>2.4</version>
141-
</dependency>
142-
<dependency>
143-
<groupId>org.apache.commons</groupId>
144-
<artifactId>commons-lang3</artifactId>
145-
<version>3.4</version>
146-
</dependency>
147-
148-
<dependency>
149-
<groupId>junit</groupId>
150-
<artifactId>junit</artifactId>
151-
<version>4.12</version>
152-
</dependency>
153-
<dependency>
154-
<groupId>org.hamcrest</groupId>
155-
<artifactId>hamcrest-all</artifactId>
156-
<version>1.3</version>
157-
</dependency>
158-
</dependencies>
159-
</dependencyManagement>
160-
161161
</project>

Diff for: proxy/pom.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
<dependency>
9797
<groupId>org.apache.httpcomponents</groupId>
9898
<artifactId>httpclient</artifactId>
99-
<version>4.3.6</version>
99+
<version>4.5.1</version>
100100
</dependency>
101101

102102
<!-- slf4j -->
@@ -122,6 +122,12 @@
122122
<artifactId>hamcrest-all</artifactId>
123123
<scope>test</scope>
124124
</dependency>
125+
<dependency>
126+
<groupId>org.mockito</groupId>
127+
<artifactId>mockito-all</artifactId>
128+
<version>1.9.5</version>
129+
<scope>test</scope>
130+
</dependency>
125131
<dependency>
126132
<groupId>org.mock-server</groupId>
127133
<artifactId>mockserver-netty</artifactId>

Diff for: proxy/src/main/java/ru/qatools/gridrouter/ProxyServlet.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import javax.servlet.http.HttpServletRequest;
1919
import javax.servlet.http.HttpServletResponse;
2020
import java.io.IOException;
21+
import java.io.UnsupportedEncodingException;
2122
import java.net.URISyntaxException;
23+
import java.net.URLDecoder;
2224

2325
import static java.nio.charset.StandardCharsets.UTF_8;
2426
import static org.springframework.http.HttpMethod.DELETE;
@@ -78,7 +80,7 @@ protected String rewriteTarget(HttpServletRequest request) {
7880
String uri = request.getRequestURI();
7981

8082
String remoteHost = getRemoteHost(request);
81-
83+
8284
if (!isUriValid(uri)) {
8385
LOGGER.warn("[{}] [{}] - request uri is {}", "INVALID_SESSION_HASH", remoteHost, uri);
8486
return null;
@@ -144,7 +146,14 @@ protected String getRoute(String uri) {
144146
}
145147

146148
protected String getCommand(String uri) {
147-
return uri.substring(getUriPrefixLength());
149+
String encodedCommand = uri.substring(getUriPrefixLength());
150+
try {
151+
return URLDecoder.decode(encodedCommand, UTF_8.name());
152+
} catch (UnsupportedEncodingException e) {
153+
LOGGER.error("[{}] - could not decode command: {}",
154+
"UNABLE_TO_DECODE_COMMAND", encodedCommand, e);
155+
return encodedCommand;
156+
}
148157
}
149158

150159
protected boolean isUriValid(String uri) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ru.qatools.gridrouter;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.junit.runners.Parameterized;
6+
7+
import java.net.URLEncoder;
8+
import java.util.Arrays;
9+
import java.util.Collection;
10+
11+
import static java.nio.charset.StandardCharsets.UTF_8;
12+
import static org.hamcrest.Matchers.endsWith;
13+
import static org.junit.Assert.assertThat;
14+
15+
/**
16+
* @author Artem Eroshenko [email protected]
17+
*/
18+
@RunWith(Parameterized.class)
19+
public class CommandDecodingTest {
20+
21+
public static final String SUFFIX = "http://host.com/wd/hub/session/8dec71ede39ad9ff3";
22+
23+
public static final String POSTFIX = "b3fbc03311bdc45282358f1-f09c-4c44-8057-4b82f4a53002/element/id/";
24+
25+
public String requestUri;
26+
27+
public String elementId;
28+
29+
public CommandDecodingTest(String elementId) throws Exception {
30+
this.requestUri = String.format("%s%s%s", SUFFIX, POSTFIX, URLEncoder.encode(elementId, UTF_8.name()));
31+
this.elementId = elementId;
32+
}
33+
34+
@Parameterized.Parameters
35+
public static Collection<Object[]> getData() {
36+
return Arrays.asList(
37+
new Object[]{"text_???"},
38+
new Object[]{"text_&_not_text"}
39+
);
40+
}
41+
42+
@Test
43+
public void testOutput() throws Exception {
44+
ProxyServlet proxyServlet = new ProxyServlet();
45+
String command = proxyServlet.getCommand(requestUri);
46+
assertThat(command, endsWith(elementId));
47+
}
48+
49+
50+
}

0 commit comments

Comments
 (0)