Skip to content

Commit

Permalink
Merge #337 from remote-tracking branch 'origin/273-fixUrlEncode'
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed Nov 23, 2023
2 parents 49e3fb1 + a55c352 commit b129d44
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,19 @@ upcase("<sourceField>")

Encodes a field value as URI. Aka percent-encoding.

Options:

- `plus_for_space`: Sets whether "space" (` `) will be substituted by a "plus" (`+`) or be percent escaped (`%20`). (Default: `true`)
- `safe_chars`: Sets characters that won't be escaped. Safe characters are the ranges 0..9, a..z and A..Z. These are always safe and should not be specified. (Default: `.-*_`)

```perl
uri_encode("<sourceField>"[, <options>...])
```

E.g.:

```perl
uri_encode("<sourceField>")
uri_encode("path.to.field", plus_for_space:"false", safe_chars:"")
```

### Selectors
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ subprojects {
'jquery': '3.3.1-1',
'junit_jupiter': '5.8.2',
'junit_platform': '1.4.2',
'metafacture': '5.7.0-rc1',
'metafacture': '5.7.0-rc2',
'mockito': '2.27.0',
'requirejs': '2.3.6',
'slf4j': '1.7.21',
Expand Down
3 changes: 2 additions & 1 deletion metafix/src/main/java/org/metafacture/metafix/FixMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,8 @@ public void apply(final Metafix metafix, final Record record, final List<String>
@Override
public void apply(final Metafix metafix, final Record record, final List<String> params, final Map<String, String> options) {
final URLEncode urlEncoder = new URLEncode();
urlEncoder.setPlusForSpace(false);
withOption(options, "safe_chars", urlEncoder::setSafeChars);
withOption(options, "plus_for_space", urlEncoder::setPlusForSpace, this::getBoolean);

record.transform(params.get(0), urlEncoder::process);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4015,12 +4015,48 @@ public void shouldUriEncodePathSegment() {
),
i -> {
i.startRecord("1");
i.literal("id", "slash/990223521400206441:DE-A96:61 TYD 16(3)#!");
i.literal("id", "/DE-A96:% (3)#!");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("id", "slash%2F990223521400206441%3ADE%2DA96%3A61%20TYD%2016%283%29%23%21");
o.get().literal("id", "%2FDE-A96%3A%25+%283%29%23%21");
o.get().endRecord();
}
);
}

@Test
public void shouldUriEncodePathSegmentWithoutPlusForSpace() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uri_encode('id', plus_for_space:'false')"
),
i -> {
i.startRecord("1");
i.literal("id", "/DE-A96:% (3)#!");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("id", "%2FDE-A96%3A%25%20%283%29%23%21");
o.get().endRecord();
}
);
}

@Test
public void shouldUriEncodePathSegmentWithoutSafeChars() {
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
"uri_encode('id', safe_chars:'')"
),
i -> {
i.startRecord("1");
i.literal("id", "/DE-A96:% (3)#!");
i.endRecord();
},
o -> {
o.get().startRecord("1");
o.get().literal("id", "%2FDE%2DA96%3A%25+%283%29%23%21");
o.get().endRecord();
}
);
Expand Down

0 comments on commit b129d44

Please sign in to comment.