Testing Subdomain Processing Implementation in koajs/koa
This test suite validates subdomain handling functionality in Koa.js request objects, focusing on extracting and processing subdomain arrays from host headers. It ensures proper subdomain parsing across different configurations and edge cases.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
koajs/koa
__tests__/request/subdomains.test.js
'use strict'
const { describe, it } = require('node:test')
const assert = require('assert')
const request = require('../../test-helpers/context').request
describe('req.subdomains', () => {
it('should return subdomain array', () => {
const req = request()
req.header.host = 'tobi.ferrets.example.com'
req.app.subdomainOffset = 2
assert.deepStrictEqual(req.subdomains, ['ferrets', 'tobi'])
req.app.subdomainOffset = 3
assert.deepStrictEqual(req.subdomains, ['tobi'])
})
it('should work with no host present', () => {
const req = request()
assert.deepStrictEqual(req.subdomains, [])
})
it('should check if the host is an ip address, even with a port', () => {
const req = request()
req.header.host = '127.0.0.1:3000'
assert.deepStrictEqual(req.subdomains, [])
})
})