Back to Repositories

Testing Response Content-Type Detection Implementation in koajs/koa

This test suite validates the response.is() method in Koa.js, which checks Content-Type matching and handling. The tests verify content type detection, parameter handling, and multiple type matching scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the response.is() functionality, examining multiple content type matching scenarios.

  • Tests parameter stripping from content types
  • Handles undefined type cases
  • Validates single and multiple type matching
  • Covers special content types like urlencoded

Implementation Analysis

The testing approach uses Node’s built-in test framework with describe/it blocks for structured test organization. Tests employ context helpers for response object creation and assert for validation.

  • Uses strict mode JavaScript
  • Implements BDD-style test structure
  • Leverages custom context helpers

Technical Details

  • Testing Framework: Node.js test module
  • Assertion Library: Node’s assert module
  • Helper Utilities: Custom context creation
  • Test Structure: Nested describe blocks

Best Practices Demonstrated

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

  • Hierarchical test organization
  • Isolated test contexts
  • Thorough type matching scenarios
  • Clear test case descriptions

koajs/koa

__tests__/response/is.test.js

            
'use strict'

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

describe('response.is(type)', () => {
  it('should ignore params', () => {
    const res = context().response
    res.type = 'text/html; charset=utf-8'

    assert.strictEqual(res.is('text/*'), 'text/html')
  })

  describe('when no type is set', () => {
    it('should return false', () => {
      const res = context().response

      assert.strictEqual(res.is(), false)
      assert.strictEqual(res.is('html'), false)
    })
  })

  describe('when given no types', () => {
    it('should return the type', () => {
      const res = context().response
      res.type = 'text/html; charset=utf-8'

      assert.strictEqual(res.is(), 'text/html')
    })
  })

  describe('given one type', () => {
    it('should return the type or false', () => {
      const res = context().response
      res.type = 'image/png'

      assert.strictEqual(res.is('png'), 'png')
      assert.strictEqual(res.is('.png'), '.png')
      assert.strictEqual(res.is('image/png'), 'image/png')
      assert.strictEqual(res.is('image/*'), 'image/png')
      assert.strictEqual(res.is('*/png'), 'image/png')

      assert.strictEqual(res.is('jpeg'), false)
      assert.strictEqual(res.is('.jpeg'), false)
      assert.strictEqual(res.is('image/jpeg'), false)
      assert.strictEqual(res.is('text/*'), false)
      assert.strictEqual(res.is('*/jpeg'), false)
    })
  })

  describe('given multiple types', () => {
    it('should return the first match or false', () => {
      const res = context().response
      res.type = 'image/png'

      assert.strictEqual(res.is('png'), 'png')
      assert.strictEqual(res.is('.png'), '.png')
      assert.strictEqual(res.is('text/*', 'image/*'), 'image/png')
      assert.strictEqual(res.is('image/*', 'text/*'), 'image/png')
      assert.strictEqual(res.is('image/*', 'image/png'), 'image/png')
      assert.strictEqual(res.is('image/png', 'image/*'), 'image/png')

      assert.strictEqual(res.is(['text/*', 'image/*']), 'image/png')
      assert.strictEqual(res.is(['image/*', 'text/*']), 'image/png')
      assert.strictEqual(res.is(['image/*', 'image/png']), 'image/png')
      assert.strictEqual(res.is(['image/png', 'image/*']), 'image/png')

      assert.strictEqual(res.is('jpeg'), false)
      assert.strictEqual(res.is('.jpeg'), false)
      assert.strictEqual(res.is('text/*', 'application/*'), false)
      assert.strictEqual(res.is('text/html', 'text/plain', 'application/json; charset=utf-8'), false)
    })
  })

  describe('when Content-Type: application/x-www-form-urlencoded', () => {
    it('should match "urlencoded"', () => {
      const res = context().response
      res.type = 'application/x-www-form-urlencoded'

      assert.strictEqual(res.is('urlencoded'), 'urlencoded')
      assert.strictEqual(res.is('json', 'urlencoded'), 'urlencoded')
      assert.strictEqual(res.is('urlencoded', 'json'), 'urlencoded')
    })
  })
})