From 7dc3fdc739bf057984b66986a1dff9121f792fa8 Mon Sep 17 00:00:00 2001 From: Artem Eroshenko Date: Mon, 26 Oct 2015 12:32:57 +0300 Subject: [PATCH] Fix command encoding problem --- pom.xml | 54 +++++++++---------- proxy/pom.xml | 8 ++- .../ru/qatools/gridrouter/ProxyServlet.java | 13 ++++- .../gridrouter/CommandDecodingTest.java | 50 +++++++++++++++++ 4 files changed, 95 insertions(+), 30 deletions(-) create mode 100644 proxy/src/test/java/ru/qatools/gridrouter/CommandDecodingTest.java diff --git a/pom.xml b/pom.xml index f70fa21..9f71b75 100644 --- a/pom.xml +++ b/pom.xml @@ -58,7 +58,33 @@ Jenkins http://ci.qatools.ru/ - + + + + + commons-io + commons-io + 2.4 + + + org.apache.commons + commons-lang3 + 3.4 + + + + junit + junit + 4.12 + + + org.hamcrest + hamcrest-all + 1.3 + + + + @@ -132,30 +158,4 @@ - - - - commons-io - commons-io - 2.4 - - - org.apache.commons - commons-lang3 - 3.4 - - - - junit - junit - 4.12 - - - org.hamcrest - hamcrest-all - 1.3 - - - - diff --git a/proxy/pom.xml b/proxy/pom.xml index 843ffb4..fe013cc 100644 --- a/proxy/pom.xml +++ b/proxy/pom.xml @@ -96,7 +96,7 @@ org.apache.httpcomponents httpclient - 4.3.6 + 4.5.1 @@ -122,6 +122,12 @@ hamcrest-all test + + org.mockito + mockito-all + 1.9.5 + test + org.mock-server mockserver-netty diff --git a/proxy/src/main/java/ru/qatools/gridrouter/ProxyServlet.java b/proxy/src/main/java/ru/qatools/gridrouter/ProxyServlet.java index ca647ec..6cf2cfa 100644 --- a/proxy/src/main/java/ru/qatools/gridrouter/ProxyServlet.java +++ b/proxy/src/main/java/ru/qatools/gridrouter/ProxyServlet.java @@ -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; @@ -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; @@ -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) { diff --git a/proxy/src/test/java/ru/qatools/gridrouter/CommandDecodingTest.java b/proxy/src/test/java/ru/qatools/gridrouter/CommandDecodingTest.java new file mode 100644 index 0000000..e76865c --- /dev/null +++ b/proxy/src/test/java/ru/qatools/gridrouter/CommandDecodingTest.java @@ -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 eroshenkoam@yandex-team.ru + */ +@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 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)); + } + + +}