Skip to content

Commit

Permalink
Merge pull request #9 from lanwen/path_param_decode
Browse files Browse the repository at this point in the history
fix - any param filter decodes query twice
  • Loading branch information
lanwen committed Feb 11, 2016
2 parents 6da7450 + be658f8 commit f328cc2
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.3</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -68,7 +69,7 @@ private String toQuery(Map<String, List<String>> params) {
List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
/**
* User: leonsabr
*/
public class Decoder {
public final class URLCoder {

private Decoder() {
private URLCoder() {
}

public static String decode(String s) {
Expand All @@ -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");
}
}
}
12 changes: 6 additions & 6 deletions src/main/java/ru/lanwen/diff/uri/core/util/UriPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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('#');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 15 additions & 4 deletions src/test/java/ru/lanwen/diff/uri/QueryWithEncodedURLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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={\""));
}
}
2 changes: 1 addition & 1 deletion src/test/java/ru/lanwen/diff/uri/UriDiffFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/test/java/ru/lanwen/diff/uri/UriDiffOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static Collection<Object[]> 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;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ru/lanwen/diff/uri/UriDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)))));
Expand Down
Original file line number Diff line number Diff line change
@@ -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)));
}

}

0 comments on commit f328cc2

Please sign in to comment.