diff --git a/src/httpbis/index.ts b/src/httpbis/index.ts index 005bd41..6970fc2 100644 --- a/src/httpbis/index.ts +++ b/src/httpbis/index.ts @@ -93,7 +93,9 @@ export function deriveComponent(component: string, params: Map encodeURIComponent(value)); } case '@status': { if (isRequest(context)) { diff --git a/test/httpbis/httpbis.spec.ts b/test/httpbis/httpbis.spec.ts index 879aea5..fa1edef 100644 --- a/test/httpbis/httpbis.spec.ts +++ b/test/httpbis/httpbis.spec.ts @@ -168,12 +168,36 @@ describe('httpbis', () => { expect(httpbis.deriveComponent('@path', new Map(), req)).to.deep.equal([ '/path', ]); + expect(httpbis.deriveComponent('@path', new Map(), { + ...req, + url: 'https://www.example.com/path%7D?param=value', + })).to.deep.equal([ + '/path%7D', + ]); + expect(httpbis.deriveComponent('@path', new Map(), { + ...req, + url: 'https://www.example.com', + })).to.deep.equal([ + '/', + ]); expect(httpbis.deriveComponent('@path', new Map(), { ...req, url: new URL(req.url as string), })).to.deep.equal([ '/path', ]); + expect(httpbis.deriveComponent('@path', new Map(), { + ...req, + url: new URL('https://www.example.com/path%7D?param=value'), + })).to.deep.equal([ + '/path%7D', + ]); + expect(httpbis.deriveComponent('@path', new Map(), { + ...req, + url: new URL('https://www.example.com'), + })).to.deep.equal([ + '/', + ]); }); it('derives @query', () => { const req: Request = { @@ -198,6 +222,18 @@ describe('httpbis', () => { })).to.deep.equal([ '?', ]); + expect(httpbis.deriveComponent('@query', new Map(), { + ...req, + url: 'https://www.example.com//path?param=value&foo=bar&baz=bat%2Dman', + })).to.deep.equal([ + '?param=value&foo=bar&baz=bat%2Dman', + ]); + expect(httpbis.deriveComponent('@query', new Map(), { + ...req, + url: 'https://www.example.com/path', + })).to.deep.equal([ + '?', + ]); // with URL objects expect(httpbis.deriveComponent('@query', new Map(), { ...req, @@ -211,6 +247,12 @@ describe('httpbis', () => { })).to.deep.equal([ '?queryString', ]); + expect(httpbis.deriveComponent('@query', new Map(), { + ...req, + url: new URL('https://www.example.com//path?param=value&foo=bar&baz=bat%2Dman'), + })).to.deep.equal([ + '?param=value&foo=bar&baz=bat%2Dman', + ]); expect(httpbis.deriveComponent('@query', new Map(), { ...req, url: new URL('https://www.example.com/path'), @@ -242,6 +284,31 @@ describe('httpbis', () => { 'value', 'value2', ]); + expect(httpbis.deriveComponent('@query-param', new Map([['name', 'param']]), { + ...req, + url: 'https://example.com/path?param=value%7D¶m=value2%7D', + })).to.deep.equal([ + 'value%7D', + 'value2%7D', + ]); + expect(httpbis.deriveComponent('@query-param', new Map([['name', 'var']]), { + ...req, + url: 'https://example.com/parameters?var=this%20is%20a%20big%0Amultiline%20value&bar=with+plus+whitespace&fa%C3%A7ade%22%3A%20=something', + })).to.deep.equal([ + 'this%20is%20a%20big%0Amultiline%20value', + ]); + expect(httpbis.deriveComponent('@query-param', new Map([['name', 'bar']]), { + ...req, + url: 'https://example.com/parameters?var=this%20is%20a%20big%0Amultiline%20value&bar=with+plus+whitespace&fa%C3%A7ade%22%3A%20=something', + })).to.deep.equal([ + 'with%20plus%20whitespace', + ]); + expect(httpbis.deriveComponent('@query-param', new Map([['name', 'fa%C3%A7ade%22%3A%20']]), { + ...req, + url: 'https://example.com/parameters?var=this%20is%20a%20big%0Amultiline%20value&bar=with+plus+whitespace&fa%C3%A7ade%22%3A%20=something', + })).to.deep.equal([ + 'something', + ]); // with URL objects expect(httpbis.deriveComponent('@query-param', new Map([['name', 'baz']]), { ...req, @@ -268,6 +335,31 @@ describe('httpbis', () => { 'value', 'value2', ]); + expect(httpbis.deriveComponent('@query-param', new Map([['name', 'param']]), { + ...req, + url: new URL('https://example.com/path?param=value%7D¶m=value2%7D'), + })).to.deep.equal([ + 'value%7D', + 'value2%7D', + ]); + expect(httpbis.deriveComponent('@query-param', new Map([['name', 'var']]), { + ...req, + url: new URL('https://example.com/parameters?var=this%20is%20a%20big%0Amultiline%20value&bar=with+plus+whitespace&fa%C3%A7ade%22%3A%20=something'), + })).to.deep.equal([ + 'this%20is%20a%20big%0Amultiline%20value', + ]); + expect(httpbis.deriveComponent('@query-param', new Map([['name', 'bar']]), { + ...req, + url: new URL('https://example.com/parameters?var=this%20is%20a%20big%0Amultiline%20value&bar=with+plus+whitespace&fa%C3%A7ade%22%3A%20=something'), + })).to.deep.equal([ + 'with%20plus%20whitespace', + ]); + expect(httpbis.deriveComponent('@query-param', new Map([['name', 'fa%C3%A7ade%22%3A%20']]), { + ...req, + url: new URL('https://example.com/parameters?var=this%20is%20a%20big%0Amultiline%20value&bar=with+plus+whitespace&fa%C3%A7ade%22%3A%20=something'), + })).to.deep.equal([ + 'something', + ]); }); it('derives @status', () => { const req: Request = {