Back to Repositories

Testing HTTP Status Code Response Handling in koajs/koa

This test suite validates the HTTP status code handling functionality in Koa’s response module. It ensures proper status code setting, validation, and header field management across different HTTP versions and status types.

Test Coverage Overview

The test suite provides comprehensive coverage of status code handling scenarios in Koa responses.

  • Valid status code assignment and validation
  • Invalid status code handling
  • Custom status code support
  • HTTP/2 specific behavior
  • Content header stripping for specific status codes (204, 205, 304)

Implementation Analysis

The testing approach uses Jest’s describe/it blocks to organize test cases hierarchically. It implements both synchronous and asynchronous testing patterns using supertest for HTTP request simulation.

  • Mock response objects for isolated testing
  • Assertion-based validation
  • HTTP request simulation
  • Error case validation

Technical Details

  • Testing Framework: Node’s native test module
  • Assertion Library: Node’s assert module
  • HTTP Testing: SuperTest
  • Status Code Library: statuses module
  • Test Helpers: Custom context helpers

Best Practices Demonstrated

The test suite exhibits strong testing practices through organized test structure and comprehensive edge case coverage.

  • Isolated test cases with clear descriptions
  • Proper setup and teardown management
  • Thorough error condition testing
  • HTTP protocol compliance verification
  • Consistent assertion patterns

koajs/koa

__tests__/response/status.test.js

            
'use strict'

const { describe, it, beforeEach } = require('node:test')
const response = require('../../test-helpers/context').response
const request = require('supertest')
const statuses = require('statuses')
const assert = require('assert')
const Koa = require('../..')

describe('res.status=', () => {
  describe('when a status code', () => {
    describe('and valid', () => {
      it('should set the status', () => {
        const res = response()
        res.status = 403
        assert.strictEqual(res.status, 403)
      })

      it('should not throw', () => {
        response().status = 403
      })
    })

    describe('and invalid', () => {
      it('should throw', () => {
        assert.throws(() => {
          response().status = 99
        }, /invalid status code: 99/)
      })
    })

    describe('and custom status', () => {
      beforeEach(() => { statuses['700'] = 'custom status' })

      it('should set the status', () => {
        const res = response()
        res.status = 700
        assert.strictEqual(res.status, 700)
      })

      it('should not throw', () => {
        response().status = 700
      })
    })

    describe('and HTTP/2', () => {
      it('should not set the status message', () => {
        const res = response({
          httpVersionMajor: 2,
          httpVersion: '2.0'
        })
        res.status = 200
        assert(!res.res.statusMessage)
      })
    })
  })

  describe('when a status string', () => {
    it('should throw', () => {
      assert.throws(() => { response().status = 'forbidden' }, /status code must be a number/)
    })
  })

  function strip (status) {
    it('should strip content related header fields', async () => {
      const app = new Koa()

      app.use(ctx => {
        ctx.body = { foo: 'bar' }
        ctx.set('Content-Type', 'application/json; charset=utf-8')
        ctx.set('Content-Length', '15')
        ctx.set('Transfer-Encoding', 'chunked')
        ctx.status = status
        assert(ctx.response.header['content-type'] == null)
        assert(ctx.response.header['content-length'] == null)
        assert(ctx.response.header['transfer-encoding'] == null)
      })

      const res = await request(app.callback())
        .get('/')
        .expect(status)

      assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'Content-Type'), false)
      assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'content-length'), false)
      assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'content-encoding'), false)
      assert.strictEqual(res.text.length, 0)
    })

    it('should strip content related header fields after status set', async () => {
      const app = new Koa()

      app.use(ctx => {
        ctx.status = status
        ctx.body = { foo: 'bar' }
        ctx.set('Content-Type', 'application/json; charset=utf-8')
        ctx.set('Content-Length', '15')
        ctx.set('Transfer-Encoding', 'chunked')
      })

      const res = await request(app.callback())
        .get('/')
        .expect(status)

      assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'Content-Type'), false)
      assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'content-length'), false)
      assert.strictEqual(Object.prototype.hasOwnProperty.call(res.headers, 'content-encoding'), false)
      assert.strictEqual(res.text.length, 0)
    })
  }

  describe('when 204', () => strip(204))

  describe('when 205', () => strip(205))

  describe('when 304', () => strip(304))
})