From 57480c746af47dc921f9b3f1a8cac2a68e24e84e Mon Sep 17 00:00:00 2001 From: Kirill Merkushev Date: Thu, 11 Feb 2016 20:15:18 +0300 Subject: [PATCH 1/2] fix - any param filter decodes query twice --- pom.xml | 2 +- .../uri/core/filters/AnyParamValueFilter.java | 3 ++- .../core/util/{Decoder.java => URLCoder.java} | 12 +++++++-- .../lanwen/diff/uri/core/util/UriPrinter.java | 12 ++++----- .../diff/uri/core/util/UriSplitter.java | 2 +- .../diff/uri/QueryWithEncodedURLTest.java | 19 +++++++++++--- .../ru/lanwen/diff/uri/UriDiffFilterTest.java | 2 +- .../ru/lanwen/diff/uri/UriDiffOutputTest.java | 1 + .../java/ru/lanwen/diff/uri/UriDiffTest.java | 2 +- .../core/filters/AnyParamValueFilterTest.java | 25 +++++++++++++++++++ 10 files changed, 63 insertions(+), 17 deletions(-) rename src/main/java/ru/lanwen/diff/uri/core/util/{Decoder.java => URLCoder.java} (54%) create mode 100644 src/test/java/ru/lanwen/diff/uri/core/filters/AnyParamValueFilterTest.java diff --git a/pom.xml b/pom.xml index 2ca7557..bdf8329 100644 --- a/pom.xml +++ b/pom.xml @@ -134,7 +134,7 @@ maven-compiler-plugin - 3.1 + 3.3 true 1.7 diff --git a/src/main/java/ru/lanwen/diff/uri/core/filters/AnyParamValueFilter.java b/src/main/java/ru/lanwen/diff/uri/core/filters/AnyParamValueFilter.java index 6ea003f..b5ddefd 100644 --- a/src/main/java/ru/lanwen/diff/uri/core/filters/AnyParamValueFilter.java +++ b/src/main/java/ru/lanwen/diff/uri/core/filters/AnyParamValueFilter.java @@ -13,6 +13,7 @@ import static java.util.Arrays.asList; import static jersey.repackaged.com.google.common.base.Joiner.on; import static ru.lanwen.diff.uri.core.Delimiters.QUERY_NAME_VALUE_SEPARATOR; +import static ru.lanwen.diff.uri.core.util.URLCoder.encode; /** * User: lanwen @@ -68,7 +69,7 @@ private String toQuery(Map> params) { List pairs = new ArrayList<>(); for (String key : params.keySet()) { for (String value : params.get(key)) { - pairs.add(on(QUERY_NAME_VALUE_SEPARATOR).join(asList(key, value))); + pairs.add(on(QUERY_NAME_VALUE_SEPARATOR).join(asList(key, encode(value)))); } } return on(UriPart.QUERY.getJoiner()).join(pairs); diff --git a/src/main/java/ru/lanwen/diff/uri/core/util/Decoder.java b/src/main/java/ru/lanwen/diff/uri/core/util/URLCoder.java similarity index 54% rename from src/main/java/ru/lanwen/diff/uri/core/util/Decoder.java rename to src/main/java/ru/lanwen/diff/uri/core/util/URLCoder.java index 86ea2a4..2bc328d 100644 --- a/src/main/java/ru/lanwen/diff/uri/core/util/Decoder.java +++ b/src/main/java/ru/lanwen/diff/uri/core/util/URLCoder.java @@ -5,9 +5,9 @@ /** * User: leonsabr */ -public class Decoder { +public final class URLCoder { - private Decoder() { + private URLCoder() { } public static String decode(String s) { @@ -17,4 +17,12 @@ public static String decode(String s) { throw new IllegalArgumentException("UTF-8 encoding is not supported"); } } + + public static String encode(String s) { + try { + return java.net.URLEncoder.encode(s, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new IllegalArgumentException("UTF-8 encoding is not supported"); + } + } } diff --git a/src/main/java/ru/lanwen/diff/uri/core/util/UriPrinter.java b/src/main/java/ru/lanwen/diff/uri/core/util/UriPrinter.java index c13676d..7f385e7 100644 --- a/src/main/java/ru/lanwen/diff/uri/core/util/UriPrinter.java +++ b/src/main/java/ru/lanwen/diff/uri/core/util/UriPrinter.java @@ -72,12 +72,12 @@ public String toString() { // sb.append("//"); // sb.append(authority); // } - if (path != null) - sb.append(path); - if (query != null) { - sb.append('?'); - sb.append(query); - } + } + if (path != null) + sb.append(path); + if (query != null) { + sb.append('?'); + sb.append(query); } if (fragment != null) { sb.append('#'); diff --git a/src/main/java/ru/lanwen/diff/uri/core/util/UriSplitter.java b/src/main/java/ru/lanwen/diff/uri/core/util/UriSplitter.java index 764c846..de27063 100644 --- a/src/main/java/ru/lanwen/diff/uri/core/util/UriSplitter.java +++ b/src/main/java/ru/lanwen/diff/uri/core/util/UriSplitter.java @@ -22,7 +22,7 @@ import static ru.lanwen.diff.uri.core.UriPart.PORT; import static ru.lanwen.diff.uri.core.UriPart.QUERY; import static ru.lanwen.diff.uri.core.UriPart.SCHEME; -import static ru.lanwen.diff.uri.core.util.Decoder.decode; +import static ru.lanwen.diff.uri.core.util.URLCoder.decode; /** * User: lanwen diff --git a/src/test/java/ru/lanwen/diff/uri/QueryWithEncodedURLTest.java b/src/test/java/ru/lanwen/diff/uri/QueryWithEncodedURLTest.java index 79e1db2..62e1086 100644 --- a/src/test/java/ru/lanwen/diff/uri/QueryWithEncodedURLTest.java +++ b/src/test/java/ru/lanwen/diff/uri/QueryWithEncodedURLTest.java @@ -2,23 +2,34 @@ import org.junit.Test; +import static ru.lanwen.diff.uri.core.util.URLCoder.encode; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; +import static ru.lanwen.diff.uri.matchers.UriDiffMatchers.changes; /** * User: leonsabr */ public class QueryWithEncodedURLTest { - public static final String URL_EXP = "https://www.yandex.com/yandsearch?text=procter%26gamble"; - public static final String URL_ACT = "https://www.yandex.com/yandsearch?text=m%26m%27s"; - @Test public void parameterValueWithAmpersandIsParsedCorrectly() throws Exception { - UriDiffer differ = UriDiffer.diff().actual(URL_ACT).expected(URL_EXP); + String actual = "https://www.yandex.com/yandsearch?text=m%26m%27s"; + String expected = "https://www.yandex.com/yandsearch?text=procter%26gamble"; + + UriDiffer differ = UriDiffer.diff().actual(actual).expected(expected); assertThat(differ.queryDeltas(), hasSize(1)); assertThat((String) differ.queryDeltas().get(0).getRevised().getLines().get(0), equalTo("text=m&m's")); } + @Test + public void shouldNormallyEncodeDecodeBrackets() throws Exception { + String encode = "/?y=" + encode("{\""); + UriDiffer differ = UriDiffer.diff().expected("/?y=1").actual(encode); + + assertThat(differ.changes(), changes(hasSize(1))); + assertThat(differ.queryDeltas(), hasSize(1)); + assertThat((String) differ.queryDeltas().get(0).getRevised().getLines().get(0), equalTo("y={\"")); + } } diff --git a/src/test/java/ru/lanwen/diff/uri/UriDiffFilterTest.java b/src/test/java/ru/lanwen/diff/uri/UriDiffFilterTest.java index 3e56365..67d4a29 100644 --- a/src/test/java/ru/lanwen/diff/uri/UriDiffFilterTest.java +++ b/src/test/java/ru/lanwen/diff/uri/UriDiffFilterTest.java @@ -5,9 +5,9 @@ import java.net.URI; import static org.hamcrest.MatcherAssert.assertThat; -import static ru.lanwen.diff.uri.matchers.ViewCompareMatcher.comparingTo; import static ru.lanwen.diff.uri.core.filters.AnyParamValueFilter.param; import static ru.lanwen.diff.uri.core.filters.SchemeFilter.scheme; +import static ru.lanwen.diff.uri.matchers.ViewCompareMatcher.comparingTo; /** * User: lanwen diff --git a/src/test/java/ru/lanwen/diff/uri/UriDiffOutputTest.java b/src/test/java/ru/lanwen/diff/uri/UriDiffOutputTest.java index eacfa78..7c77916 100644 --- a/src/test/java/ru/lanwen/diff/uri/UriDiffOutputTest.java +++ b/src/test/java/ru/lanwen/diff/uri/UriDiffOutputTest.java @@ -58,6 +58,7 @@ public static Collection data() { data.add(new Object[]{"http://ya.ru/?q=1#123neo", "http://ya.ru/?q=1#neo", "http://ya.ru/?q=1#[-123]neo"}); data.add(new Object[]{"http://ya.ru/?q=1#noo", "http://ya.ru/?q=1#neo", "http://ya.ru/?q=1#n[+e]o[-o]"}); data.add(new Object[]{"http://ya.ru/?q=1#n00oo", "http://ya.ru/?q=1#neo", "http://ya.ru/?q=1#n[00->e]oo[-o]"}); + data.add(new Object[]{"/path?q=1", "/changed?q=2", "/[path->changed]?[q=1->q=2]"}); data.add(new Object[]{UriDiffTest.URL_EXP, UriDiffTest.URL_ACT, "http://disk.yandex.com.tr/?[auth=1->auth=1?retpath=http://mail.yandex.com.tr/neo2/#disk]&auth=2&[-retpath=http://mail.yandex.com.tr/neo2/#disk]"}); return data; diff --git a/src/test/java/ru/lanwen/diff/uri/UriDiffTest.java b/src/test/java/ru/lanwen/diff/uri/UriDiffTest.java index 4272261..91a6ca9 100644 --- a/src/test/java/ru/lanwen/diff/uri/UriDiffTest.java +++ b/src/test/java/ru/lanwen/diff/uri/UriDiffTest.java @@ -136,7 +136,7 @@ public void fragmentChange() throws Exception { UriDiffer differ = UriDiffer.diff().actual(actual).expected(expected); UriDiff diff = differ.changes(); - assertThat(differ.fragmentDeltas().size(), equalTo(1)); + assertThat(differ.fragmentDeltas(), hasSize(1)); assertThat(diff.hasChanges(), equalTo(Boolean.TRUE)); assertThat(diff, changes(hasSize(1))); assertThat(diff, changes(hasItem(changeType(equalTo(FRAGMENT))))); diff --git a/src/test/java/ru/lanwen/diff/uri/core/filters/AnyParamValueFilterTest.java b/src/test/java/ru/lanwen/diff/uri/core/filters/AnyParamValueFilterTest.java new file mode 100644 index 0000000..8c35492 --- /dev/null +++ b/src/test/java/ru/lanwen/diff/uri/core/filters/AnyParamValueFilterTest.java @@ -0,0 +1,25 @@ +package ru.lanwen.diff.uri.core.filters; + +import org.junit.Test; + +import java.net.URI; + +import static org.hamcrest.Matchers.containsString; +import static org.junit.Assert.assertThat; +import static ru.lanwen.diff.uri.core.filters.AnyParamValueFilter.param; +import static ru.lanwen.diff.uri.core.util.URLCoder.encode; + +/** + * @author lanwen (Merkushev Kirill) + */ +public class AnyParamValueFilterTest { + + @Test // #8 + public void shouldNotDecodeQueryTwiceWithParamFilter() throws Exception { + String templatedQueryPart = "{\""; + String encode = "/?y=" + encode(templatedQueryPart); + URI filtered = param("z").apply(URI.create(encode)); + assertThat(filtered.toString(), containsString(encode(templatedQueryPart))); + } + +} From be658f890658e905bad5af9e95cca20c76327f48 Mon Sep 17 00:00:00 2001 From: Kirill Merkushev Date: Thu, 11 Feb 2016 20:19:22 +0300 Subject: [PATCH 2/2] update readme with example with empty host --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index accb47c..e8bea70 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ What it returns: | `http://yandex.com` | `http://yandex.com.tr` | `http://yandex.com.[+tr]` | | `http://yandex.com.tr` | `http://yandex.com` | `http://yandex.com.[-tr]` | | `http://yandex.com.tr.fr` | `http://yandex.com` | `http://yandex.com.[-tr.fr]` | +| `/path?q=1` | `/changed?q=2` | `/[path->changed]?[q=1->q=2]` | ##Use filters: