From feca411e84e2e6af5f85a3c612032bd907107179 Mon Sep 17 00:00:00 2001 From: Taku Amano Date: Fri, 19 Apr 2024 20:41:49 +0900 Subject: [PATCH] test: add tests for invalid host header --- test/request.test.ts | 52 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/test/request.test.ts b/test/request.test.ts index 69fc587..999a371 100644 --- a/test/request.test.ts +++ b/test/request.test.ts @@ -4,6 +4,7 @@ import { Request as LightweightRequest, GlobalRequest, getAbortController, + RequestError, } from '../src/request' Object.defineProperty(global, 'Request', { @@ -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 () => { @@ -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', () => {