Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix URLEncode to be backwards compatible #507

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
* Default is to convert a whitespace " "to a plus sign "+". This can be set so that a whitespace " " is escaped to
* "%20".
* Safe characters for this escaper are the ranges 0..9, a..z and A..Z. These are always safe and should not be
* specified.
* specified. Default safe characters are also ".", "-", "*", and "_", following URLEncoder.
*
* @see java.net.URLEncoder
*
* @author Markus Michael Geipel
* @author Pascal Christoph (dr0i)
*/
public final class URLEncode extends AbstractSimpleStatelessFunction {
private String safeChars = "";
private String safeChars = ".-*_";
private Boolean plusForSpace = true;
private PercentEscaper percentEscaper = new PercentEscaper(safeChars, plusForSpace);

Expand All @@ -50,6 +52,8 @@ public String process(final String value) {
* Sets a URI escaper with the specified safe characters. The ranges 0..9, a..z and A..Z are always safe
* and should not be specified.
*
* Default is also ".", "-", "*", and "_" , mimicking {@link java.net.URLEncoder}.
*
* @param safeChars the chars which will not be escaped
*/
public void setSafeChars(final String safeChars) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.metafacture.metamorph.functions;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import static org.junit.Assert.*;
import org.junit.Test;

Expand All @@ -31,6 +34,10 @@ public final class URLEncodeTest {
private static final String CAFE_ENCODED = "caf%C3%A9";
private static final String SOME_CHARS = "/&%\\+";
private static final String SOME_CHARS_ENCODED = "%2F%26%25%5C%2B";
private static final String SPECIAL_CHARACTERS = ".-*_";
private static final String URL =
"http://lobid.org/resources/search?q=hasItem.hasItem.heldBy.id:\"http://lobid" +
".org/organisations/DE-290#!\"&format=json";
private static final String WHITESPACE = " ";
private static final String WHITESPACE_AS_PLUS_ENCODED = "+";
private static final String WHITESPACE_PERCENT_ENCODED = "%20";
Expand Down Expand Up @@ -64,5 +71,15 @@ public void testSafeChars(){
urlEncode.setSafeChars(SOME_CHARS);
assertEquals(SOME_CHARS, urlEncode.process(SOME_CHARS));
}
@Test
public void testSpecialChars(){
final URLEncode urlEncode = new URLEncode();
assertEquals(SPECIAL_CHARACTERS, urlEncode.process(SPECIAL_CHARACTERS));
}
@Test
public void testBackwardsCompatibility() throws UnsupportedEncodingException {
final URLEncode urlEncode = new URLEncode();
assertEquals(urlEncode.process(URL), URLEncoder.encode(URL, "UTF-8"));
}

}