Skip to content

Commit d8aacec

Browse files
committed
UriTemplate reserved expansion does not escape reserved chars
1 parent f1da9e7 commit d8aacec

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

google-http-client/src/main/java/com/google/api/client/util/escape/PercentEscaper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ public class PercentEscaper extends UnicodeEscaper {
6464
public static final String SAFEPATHCHARS_URLENCODER = "-_.!~*'()@:$&,;=+";
6565

6666
/**
67-
* Contains the safe characters plus all reserved characters. This happens to be the safe path
68-
* characters plus those characters which are reserved for URI segments, namely '/' and '?'.
67+
* A string of characters that do not need to be encoded when used in URI Templates reserved
68+
* expansion, as specified in RFC 6570. This includes the safe characters plus all reserved characters.
69+
*
70+
* <p>For details on escaping URI Templates using the reserved expansion, see <a
71+
* href="https://www.rfc-editor.org/rfc/rfc6570#section-3.2.3">RFC 6570 - section 3.2.3</a>.
6972
*/
70-
public static final String SAFE_PLUS_RESERVED_CHARS_URLENCODER = SAFEPATHCHARS_URLENCODER + "/?";
73+
public static final String SAFE_PLUS_RESERVED_CHARS_URLENCODER = SAFEPATHCHARS_URLENCODER + "/?#[]";
7174

7275
/**
7376
* A string of characters that do not need to be encoded when used in URI user info part, as

google-http-client/src/test/java/com/google/api/client/http/UriTemplateTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,26 @@ public void testExpandSeveralTemplatesNoParametersUsed() {
322322
SortedMap<String, Object> map = Maps.newTreeMap();
323323
assertEquals("", UriTemplate.expand("{?id,uid}", map, false));
324324
}
325+
326+
public void testExpandTemplates_reservedExpansion_mustNotEscapeReservedCharSet() {
327+
328+
String reservedSet = ":/?#[]@!$&'()*+,;=";
329+
330+
SortedMap<String, Object> requestMap = Maps.newTreeMap();
331+
requestMap.put("var", reservedSet);
332+
333+
assertEquals("Reserved expansion must not escape chars from reserved set according to rfc6570#section-3.2.3",
334+
reservedSet, UriTemplate.expand("{+var}", requestMap, false));
335+
}
336+
337+
public void testExpandTemplates_reservedExpansion_mustNotEscapeUnreservedCharSet() {
338+
339+
String unReservedSet = "-._~";
340+
341+
SortedMap<String, Object> requestMap = Maps.newTreeMap();
342+
requestMap.put("var", unReservedSet);
343+
344+
assertEquals("Reserved expansion must not escape chars from unreserved set according to rfc6570#section-3.2.3",
345+
unReservedSet, UriTemplate.expand("{+var}", requestMap, false));
346+
}
325347
}

0 commit comments

Comments
 (0)