-
Notifications
You must be signed in to change notification settings - Fork 88
fix: More strictly, determine when new URL() should be used #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
b46cd0e
de8c772
9147e43
9d03243
606a905
10b44e0
bc49af9
dd5a383
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -154,6 +154,17 @@ describe('Request', () => { | |
| }).toThrow(RequestError) | ||
| }) | ||
|
|
||
| it('Should throw error if host header port is invalid', async () => { | ||
| expect(() => { | ||
| newRequest({ | ||
| headers: { | ||
| host: 'localhost:65536', | ||
| }, | ||
| url: '/foo.txt', | ||
| } as IncomingMessage) | ||
| }).toThrow(RequestError) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to compare also the error content
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! |
||
| }) | ||
|
|
||
| it('Should be created request body from `req.rawBody` if it exists', async () => { | ||
| const rawBody = Buffer.from('foo') | ||
| const socket = new Socket() | ||
|
|
@@ -177,6 +188,87 @@ describe('Request', () => { | |
| expect(text).toBe('foo') | ||
| }) | ||
|
|
||
| describe('IPv6 host', () => { | ||
| it('Should throw error for unmatched closing bracket in host', async () => { | ||
| expect(() => { | ||
| newRequest({ | ||
| headers: { | ||
| host: 'host]', | ||
| }, | ||
| url: '/foo.txt', | ||
| } as IncomingMessage) | ||
| }).toThrow(RequestError) | ||
| }) | ||
|
|
||
| it('Should throw error for unmatched opening bracket in host', async () => { | ||
| expect(() => { | ||
| newRequest({ | ||
| headers: { | ||
| host: '[host', | ||
| }, | ||
| url: '/foo.txt', | ||
| } as IncomingMessage) | ||
| }).toThrow(RequestError) | ||
| }) | ||
| }) | ||
|
|
||
| describe('URL normalization', () => { | ||
| test.each([ | ||
| ['[::1]', '/foo.txt'], | ||
| ['[::1]:8080', '/foo.txt'], | ||
| ['localhost', '/'], | ||
| ['localhost', '/foo/bar/baz'], | ||
| ['localhost', '/foo_bar'], | ||
| ['localhost', '/foo//bar'], | ||
| ['localhost', '/static/%2e%2e/foo.txt'], | ||
| ['localhost', '/static\\..\\foo.txt'], | ||
| ['localhost', '/..'], | ||
| ['localhost', '/foo/.'], | ||
| ['localhost', '/foo/bar/..'], | ||
| ['localhost', '/a/b/../../c'], | ||
| ['localhost', '/a/../../../b'], | ||
| ['localhost', '/a/b/c/../../../'], | ||
| ['localhost', '/./foo.txt'], | ||
| ['localhost', '/foo/../bar.txt'], | ||
| ['localhost', '/a/./b/../c?q=%2E%2E#hash'], | ||
| ['localhost', '/foo/%2E/bar/../baz'], | ||
| ['localhost', '/hello%20world'], | ||
| ['localhost', '/foo%23bar'], | ||
| ['localhost', '/foo"bar'], | ||
| ['localhost', '/%2e%2E/foo'], | ||
| ['localhost', '/caf%C3%A9'], | ||
| ['localhost', '/foo%2fbar/..//baz'], | ||
| ['localhost', '/foo?q=../bar'], | ||
| ['localhost', '/path?q=hello%20world'], | ||
| ['localhost', '/file.txt'], | ||
| ['localhost', ''], | ||
| ['LOCALHOST', '/foo.txt'], | ||
| ['LOCALHOST:80', '/foo.txt'], | ||
| ['LOCALHOST:443', '/foo.txt'], | ||
| ['LOCALHOST:8080', '/foo.txt'], | ||
| ['Localhost:3000', '/foo.txt'], | ||
| ])('Should normalize %s to %s', async (host, url) => { | ||
| const req = newRequest({ | ||
| headers: { | ||
| host, | ||
| }, | ||
| url, | ||
| } as IncomingMessage) | ||
| expect(req.url).toBe(new URL(url, `http://${host}`).toString()) | ||
| }) | ||
|
|
||
| it('Should throw error for non-origin-form request-target', async () => { | ||
| expect(() => { | ||
| newRequest({ | ||
| headers: { | ||
| host: 'localhost', | ||
| }, | ||
| url: '*', | ||
| } as IncomingMessage) | ||
| }).toThrow(RequestError) | ||
| }) | ||
| }) | ||
|
|
||
| describe('absolute-form for request-target', () => { | ||
| it('should be created from valid absolute URL', async () => { | ||
| const req = newRequest({ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about creating
url.tsandurl.test.tsto include the code for a URL?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely right! I've moved it to a separate file.
9d03243