Skip to content

Commit

Permalink
test: add tests for invalid host header
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma committed Apr 19, 2024
1 parent 76beee0 commit feca411
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions test/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Request as LightweightRequest,
GlobalRequest,
getAbortController,
RequestError,
} from '../src/request'

Object.defineProperty(global, 'Request', {
Expand Down Expand Up @@ -40,15 +41,15 @@ describe('Request', () => {
expect(req.url).toBe('http://localhost/foo.txt')
})

it('Should resolve double dots in host header', async () => {
expect(() => {
newRequest({
headers: {
host: 'localhost/..',
},
url: '/foo.txt',
} as IncomingMessage)
}).toThrow('Invalid host header')
it('Should accept hostname and port in host header', async () => {
const req = newRequest({
headers: {
host: 'localhost:8080',
},
url: '/static/../foo.txt',
} as IncomingMessage)
expect(req).toBeInstanceOf(global.Request)
expect(req.url).toBe('http://localhost:8080/foo.txt')
})

it('should generate only one `AbortController` per `Request` object created', async () => {
Expand Down Expand Up @@ -78,6 +79,39 @@ describe('Request', () => {
expect(z).not.toBe(x)
expect(z).not.toBe(y)
})

it('Should throw error if host header contains path', async () => {
expect(() => {
newRequest({
headers: {
host: 'localhost/..',
},
url: '/foo.txt',
} as IncomingMessage)
}).toThrow(RequestError)
})

it('Should throw error if host header is empty', async () => {
expect(() => {
newRequest({
headers: {
host: '',
},
url: '/foo.txt',
} as IncomingMessage)
}).toThrow(RequestError)
})

it('Should throw error if host header contains query parameter', async () => {
expect(() => {
newRequest({
headers: {
host: 'localhost?foo=bar',
},
url: '/foo.txt',
} as IncomingMessage)
}).toThrow(RequestError)
})
})

describe('GlobalRequest', () => {
Expand Down

0 comments on commit feca411

Please sign in to comment.