Back to Repositories

Testing Protocol Detection Mechanisms in Koa.js

This test suite examines the protocol detection functionality in Koa’s request handling system. It verifies correct protocol identification for both secure and non-secure connections, as well as handling of X-Forwarded-Proto headers in proxy scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of protocol detection scenarios in Koa’s request handling:

  • Encrypted socket connections returning HTTPS
  • Unencrypted socket connections returning HTTP
  • X-Forwarded-Proto header handling with trusted and untrusted proxies
  • Edge cases like empty X-Forwarded-Proto values

Implementation Analysis

The testing approach uses Node’s native test framework with describe/it blocks for clear test organization. Each test case isolates specific protocol detection scenarios by manipulating request socket properties and headers. The implementation leverages mock request objects through test helpers for consistent test environments.

Technical Details

  • Node.js native test framework (node:test)
  • Assert module for assertions
  • Custom test helpers for request context creation
  • Socket encryption simulation
  • Header manipulation for proxy testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolation of test cases, clear test descriptions, and comprehensive scenario coverage. Each test focuses on a single aspect of protocol detection, using descriptive nested describe blocks for context and maintaining clean test setup and teardown.

koajs/koa

__tests__/request/protocol.test.js

            
'use strict'

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

describe('req.protocol', () => {
  describe('when encrypted', () => {
    it('should return "https"', () => {
      const req = request()
      req.req.socket = { encrypted: true }
      assert.strictEqual(req.protocol, 'https')
    })
  })

  describe('when unencrypted', () => {
    it('should return "http"', () => {
      const req = request()
      req.req.socket = {}
      assert.strictEqual(req.protocol, 'http')
    })
  })

  describe('when X-Forwarded-Proto is set', () => {
    describe('and proxy is trusted', () => {
      it('should be used', () => {
        const req = request()
        req.app.proxy = true
        req.req.socket = {}
        req.header['x-forwarded-proto'] = 'https, http'
        assert.strictEqual(req.protocol, 'https')
      })

      describe('and X-Forwarded-Proto is empty', () => {
        it('should return "http"', () => {
          const req = request()
          req.app.proxy = true
          req.req.socket = {}
          req.header['x-forwarded-proto'] = ''
          assert.strictEqual(req.protocol, 'http')
        })
      })
    })

    describe('and proxy is not trusted', () => {
      it('should not be used', () => {
        const req = request()
        req.req.socket = {}
        req.header['x-forwarded-proto'] = 'https, http'
        assert.strictEqual(req.protocol, 'http')
      })
    })
  })
})