Skip to content

Commit

Permalink
Fix command encoding problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Eroshenko committed Oct 26, 2015
1 parent 665b95d commit 7dc3fdc
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 30 deletions.
54 changes: 27 additions & 27 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,33 @@
<system>Jenkins</system>
<url>http://ci.qatools.ru/</url>
</ciManagement>


<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -132,30 +158,4 @@
</pluginManagement>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
8 changes: 7 additions & 1 deletion proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
<version>4.5.1</version>
</dependency>

<!-- slf4j -->
Expand All @@ -122,6 +122,12 @@
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
Expand Down
13 changes: 11 additions & 2 deletions proxy/src/main/java/ru/qatools/gridrouter/ProxyServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLDecoder;

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

String remoteHost = getRemoteHost(request);

if (!isUriValid(uri)) {
LOGGER.warn("[{}] [{}] - request uri is {}", "INVALID_SESSION_HASH", remoteHost, uri);
return null;
Expand Down Expand Up @@ -144,7 +146,14 @@ protected String getRoute(String uri) {
}

protected String getCommand(String uri) {
return uri.substring(getUriPrefixLength());
String encodedCommand = uri.substring(getUriPrefixLength());
try {
return URLDecoder.decode(encodedCommand, UTF_8.name());
} catch (UnsupportedEncodingException e) {
LOGGER.error("[{}] - could not decode command: {}",
"UNABLE_TO_DECODE_COMMAND", encodedCommand, e);
return encodedCommand;
}
}

protected boolean isUriValid(String uri) {
Expand Down
50 changes: 50 additions & 0 deletions proxy/src/test/java/ru/qatools/gridrouter/CommandDecodingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ru.qatools.gridrouter;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collection;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.endsWith;
import static org.junit.Assert.assertThat;

/**
* @author Artem Eroshenko [email protected]
*/
@RunWith(Parameterized.class)
public class CommandDecodingTest {

public static final String SUFFIX = "http://host.com/wd/hub/session/8dec71ede39ad9ff3";

public static final String POSTFIX = "b3fbc03311bdc45282358f1-f09c-4c44-8057-4b82f4a53002/element/id/";

public String requestUri;

public String elementId;

public CommandDecodingTest(String elementId) throws Exception {
this.requestUri = String.format("%s%s%s", SUFFIX, POSTFIX, URLEncoder.encode(elementId, UTF_8.name()));
this.elementId = elementId;
}

@Parameterized.Parameters
public static Collection<Object[]> getData() {
return Arrays.asList(
new Object[]{"text_???"},
new Object[]{"text_&_not_text"}
);
}

@Test
public void testOutput() throws Exception {
ProxyServlet proxyServlet = new ProxyServlet();
String command = proxyServlet.getCommand(requestUri);
assertThat(command, endsWith(elementId));
}


}

0 comments on commit 7dc3fdc

Please sign in to comment.