Testing HTTP Header Validation Methods in Koa.js
This test suite evaluates the header field checking functionality in Koa’s response object. It specifically tests the has() method which verifies the existence of HTTP headers in a case-insensitive manner. The suite ensures robust header validation across different scenarios.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
koajs/koa
__tests__/response/has.test.js
'use strict'
const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')
describe('ctx.response.has(name)', () => {
it('should check a field value, case insensitive way', () => {
const ctx = context()
ctx.set('X-Foo', '')
assert.ok(ctx.response.has('x-Foo'))
assert.ok(ctx.has('x-foo'))
})
it('should return false for non-existent header', () => {
const ctx = context()
assert.strictEqual(ctx.response.has('boo'), false)
ctx.set('x-foo', 5)
assert.strictEqual(ctx.has('x-boo'), false)
})
})