Skip to content
Merged
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
72 changes: 71 additions & 1 deletion __tests__/request/whatwg-url.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict'

const { describe, it } = require('node:test')
const request = require('../../test-helpers/context').request
const context = require('../../test-helpers/context')
const request = context.request
const assert = require('node:assert/strict')

describe('req.URL', () => {
Expand All @@ -22,4 +23,73 @@ describe('req.URL', () => {
req.header.host = 'invalid host'
assert.deepStrictEqual(req.URL, Object.create(null))
})

describe('absolute-form request target', () => {
it('should parse http absolute-form like href', () => {
const ctx = context({
url: 'http://example.com/foo?q=1',
headers: { host: '127.0.0.1' }
})
assert.strictEqual(ctx.href, 'http://example.com/foo?q=1')
assert.strictEqual(ctx.URL.href, 'http://example.com/foo?q=1')
assert.strictEqual(ctx.URL.pathname, '/foo')
assert.strictEqual(ctx.URL.search, '?q=1')
})

it('should parse https absolute-form', () => {
const ctx = context({
url: 'https://example.com/foo?q=1',
headers: { host: '127.0.0.1' }
})
assert.strictEqual(ctx.URL.href, 'https://example.com/foo?q=1')
assert.strictEqual(ctx.URL.protocol, 'https:')
})

it('should parse uppercase HTTP:// absolute-form', () => {
const ctx = context({
url: 'HTTP://example.com/foo',
headers: { host: '127.0.0.1' }
})
assert.strictEqual(ctx.href, 'HTTP://example.com/foo')
assert.strictEqual(ctx.URL.href, 'http://example.com/foo')
assert.strictEqual(ctx.URL.pathname, '/foo')
})

it('should parse absolute-form with port', () => {
const ctx = context({
url: 'http://example.com:8080/foo?q=1',
headers: { host: '127.0.0.1' }
})
assert.strictEqual(ctx.URL.href, 'http://example.com:8080/foo?q=1')
assert.strictEqual(ctx.URL.host, 'example.com:8080')
})

it('should parse absolute-form when Host matches the URL host', () => {
const ctx = context({
url: 'http://example.com/foo?q=1',
headers: { host: 'example.com' }
})
assert.strictEqual(ctx.URL.href, 'http://example.com/foo?q=1')
assert.strictEqual(ctx.URL.pathname, '/foo')
})

it('should parse absolute-form when Host is empty', () => {
const ctx = context({
url: 'http://example.com/foo',
headers: { host: '' }
})
assert.strictEqual(ctx.URL.href, 'http://example.com/foo')
})

it('should still follow originalUrl after path rewrite', () => {
const ctx = context({
url: 'http://example.com/foo?q=1',
headers: { host: '127.0.0.1' }
})
ctx.path = '/bar'
assert.strictEqual(ctx.url, 'http://example.com/bar?q=1')
assert.strictEqual(ctx.href, 'http://example.com/foo?q=1')
assert.strictEqual(ctx.URL.pathname, '/foo')
})
})
})
4 changes: 3 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,9 @@ module.exports = {
if (!this.memoizedURL) {
const originalUrl = this.originalUrl || '' // avoid undefined in template string
try {
this.memoizedURL = new URL(`${this.protocol}://${this.host}${originalUrl}`)
this.memoizedURL = /^https?:\/\//i.test(originalUrl)
? new URL(originalUrl)
: new URL(`${this.protocol}://${this.host}${originalUrl}`)
} catch (err) {
this.memoizedURL = Object.create(null)
}
Expand Down