Skip to content
Open
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
11 changes: 10 additions & 1 deletion packages/core-internal/src/shared/uriTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,18 @@ export class UriTemplate {
private encodeValue(value: string, operator: string): string {
UriTemplate.validateLength(value, MAX_VARIABLE_LENGTH, 'Variable value');
if (operator === '+' || operator === '#') {
// Reserved expansion keeps RFC 3986 reserved characters — including
// the sub-delims "!" "'" "(" ")" "*" — and passes existing
// pct-triplets through, so encodeURI is the right encoder here.
return encodeURI(value);
}
return encodeURIComponent(value);
// encodeURIComponent leaves "!" "'" "(" ")" "*" unescaped, but RFC 6570
// §3.2.1 expands everything outside ALPHA / DIGIT / "-" / "." / "_" / "~",
// so those five have to be percent-encoded explicitly.
return encodeURIComponent(value).replaceAll(/[!'()*]/g, char => {
const codePoint = char.codePointAt(0)!;
return `%${codePoint.toString(16).toUpperCase()}`;
});
}

private expandPart(
Expand Down
11 changes: 11 additions & 0 deletions packages/core-internal/test/shared/uriTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ describe('UriTemplate', () => {
const template = new UriTemplate('{var}');
expect(template.expand({ var: 'value with spaces' })).toBe('value%20with%20spaces');
});

it('should encode the sub-delimiters encodeURIComponent leaves alone', () => {
const template = new UriTemplate('{var}');
expect(template.expand({ var: "a!b'c(d)e*f" })).toBe('a%21b%27c%28d%29e%2Af');
expect(template.expand({ var: 'Hello World!' })).toBe('Hello%20World%21');
});

it('should keep sub-delimiters for reserved and fragment expansion', () => {
expect(new UriTemplate('{+var}').expand({ var: 'Hello World!' })).toBe('Hello%20World!');
expect(new UriTemplate('{#var}').expand({ var: 'a(b)!' })).toBe('#a(b)!');
});
});

describe('reserved expansion', () => {
Expand Down
Loading