Skip to content

Commit 421b9b0

Browse files
committed
Fix URLEncode to be backwards compatible (metafacture-fix#273)
Complements c093da1.
1 parent 4bfe2d0 commit 421b9b0

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

metamorph/src/main/java/org/metafacture/metamorph/functions/URLEncode.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
* Default is to convert a whitespace " "to a plus sign "+". This can be set so that a whitespace " " is escaped to
2626
* "%20".
2727
* Safe characters for this escaper are the ranges 0..9, a..z and A..Z. These are always safe and should not be
28-
* specified.
28+
* specified. Default safe characters are also ".", "-", "*", and "_", following URLEncoder.
29+
*
30+
* @see java.net.URLEncoder
2931
*
3032
* @author Markus Michael Geipel
3133
* @author Pascal Christoph (dr0i)
3234
*/
3335
public final class URLEncode extends AbstractSimpleStatelessFunction {
34-
private String safeChars = "";
36+
private String safeChars = ".-*_";
3537
private Boolean plusForSpace = true;
3638
private PercentEscaper percentEscaper = new PercentEscaper(safeChars, plusForSpace);
3739

@@ -50,6 +52,8 @@ public String process(final String value) {
5052
* Sets a URI escaper with the specified safe characters. The ranges 0..9, a..z and A..Z are always safe
5153
* and should not be specified.
5254
*
55+
* Default is also ".", "-", "*", and "_" , mimicking {@link java.net.URLEncoder}.
56+
*
5357
* @param safeChars the chars which will not be escaped
5458
*/
5559
public void setSafeChars(final String safeChars) {

metamorph/src/test/java/org/metafacture/metamorph/functions/URLEncodeTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.metafacture.metamorph.functions;
1818

19+
import java.io.UnsupportedEncodingException;
20+
import java.net.URLEncoder;
21+
1922
import static org.junit.Assert.*;
2023
import org.junit.Test;
2124

@@ -31,10 +34,15 @@ public final class URLEncodeTest {
3134
private static final String CAFE_ENCODED = "caf%C3%A9";
3235
private static final String SOME_CHARS = "/&%\\+";
3336
private static final String SOME_CHARS_ENCODED = "%2F%26%25%5C%2B";
37+
private static final String SPECIAL_CHARACTERS = ".-*_";
38+
private static final String URL =
39+
"http://lobid.org/resources/search?q=hasItem.hasItem.heldBy.id:\"http://lobid" +
40+
".org/organisations/DE-290#!\"&format=json";
3441
private static final String WHITESPACE = " ";
3542
private static final String WHITESPACE_AS_PLUS_ENCODED = "+";
3643
private static final String WHITESPACE_PERCENT_ENCODED = "%20";
3744

45+
URLEncoder urlEncoder;
3846
@Test
3947
public void testUtf8(){
4048
final URLEncode urlEncode = new URLEncode();
@@ -64,5 +72,15 @@ public void testSafeChars(){
6472
urlEncode.setSafeChars(SOME_CHARS);
6573
assertEquals(SOME_CHARS, urlEncode.process(SOME_CHARS));
6674
}
75+
@Test
76+
public void testSpecialChars(){
77+
final URLEncode urlEncode = new URLEncode();
78+
assertEquals(SPECIAL_CHARACTERS, urlEncode.process(SPECIAL_CHARACTERS));
79+
}
80+
@Test
81+
public void testBackwardsCompatibility() throws UnsupportedEncodingException {
82+
final URLEncode urlEncode = new URLEncode();
83+
assertEquals(urlEncode.process(URL), URLEncoder.encode(URL, "UTF-8"));
84+
}
6785

6886
}

0 commit comments

Comments
 (0)