Skip to content

Commit

Permalink
Merge #507 from branch 'metafacture-fix-273-fixUrlEncode' of github.c…
Browse files Browse the repository at this point in the history
…om:metafacture/metafacture-core
  • Loading branch information
dr0i committed Nov 16, 2023
2 parents 4bfe2d0 + 604b064 commit dd3ca08
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
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"));
}

}

0 comments on commit dd3ca08

Please sign in to comment.