Testing URL Origin Extraction Implementation in Koa.js
This test suite evaluates the origin functionality in Koa.js, specifically focusing on the ctx.origin property. It verifies how the application handles URL origin extraction and ensures consistent behavior across different URL patterns.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
koajs/koa
__tests__/request/origin.test.js
'use strict'
const { describe, it } = require('node:test')
const assert = require('assert')
const Stream = require('stream')
const context = require('../../test-helpers/context')
describe('ctx.origin', () => {
it('should return the origin of url', () => {
const socket = new Stream.Duplex()
const req = {
url: '/users/1?next=/dashboard',
headers: {
host: 'localhost'
},
socket,
__proto__: Stream.Readable.prototype
}
const ctx = context(req)
assert.strictEqual(ctx.origin, 'http://localhost')
// change it also work
ctx.url = '/foo/users/1?next=/dashboard'
assert.strictEqual(ctx.origin, 'http://localhost')
})
})