Back to Repositories

Testing IP Address Resolution Implementation in koajs/koa

This test suite validates IP address handling functionality in Koa’s request object, focusing on proper extraction and caching of client IP addresses from various sources including X-Forwarded-For headers and socket connections. The tests ensure robust IP resolution across different scenarios and proxy configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of IP address resolution scenarios in Koa requests.

Key areas tested include:
  • IP extraction from X-Forwarded-For headers when proxy is enabled
  • Fallback to socket.remoteAddress when no forwarded IPs exist
  • Empty string handling when no IP address is available
  • IP address caching behavior
  • Manual IP override functionality

Implementation Analysis

The testing approach uses Node’s native test framework with focused unit tests for each IP resolution scenario. Tests employ mock Request objects and Stream.Duplex sockets to simulate various network conditions.

Notable patterns include:
  • Proxy mode testing with X-Forwarded-For headers
  • Socket remoteAddress property manipulation
  • Lazy initialization verification
  • Property descriptor usage for edge case testing

Technical Details

Testing infrastructure includes:
  • Node.js built-in test runner (node:test)
  • Native assert module for assertions
  • Stream.Duplex for socket mocking
  • Custom Request helper from test-helpers/context
  • Koa application instance configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear, descriptive naming
  • Proper test organization using nested describe blocks
  • Thorough edge case coverage
  • Effective use of mocking for network components
  • Verification of caching behavior and state management

koajs/koa

__tests__/request/ip.test.js

            
'use strict'

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

describe('req.ip', () => {
  describe('with req.ips present', () => {
    it('should return req.ips[0]', () => {
      const app = new Koa()
      const req = { headers: {}, socket: new Stream.Duplex() }
      app.proxy = true
      req.headers['x-forwarded-for'] = '127.0.0.1'
      req.socket.remoteAddress = '127.0.0.2'
      const request = Request(req, undefined, app)
      assert.strictEqual(request.ip, '127.0.0.1')
    })
  })

  describe('with no req.ips present', () => {
    it('should return req.socket.remoteAddress', () => {
      const req = { socket: new Stream.Duplex() }
      req.socket.remoteAddress = '127.0.0.2'
      const request = Request(req)
      assert.strictEqual(request.ip, '127.0.0.2')
    })

    describe('with req.socket.remoteAddress not present', () => {
      it('should return an empty string', () => {
        const socket = new Stream.Duplex()
        Object.defineProperty(socket, 'remoteAddress', {
          get: () => undefined, // So that the helper doesn't override it with a reasonable value
          set: () => {}
        })
        assert.strictEqual(Request({ socket }).ip, '')
      })
    })
  })

  it('should be lazy inited and cached', () => {
    const req = { socket: new Stream.Duplex() }
    req.socket.remoteAddress = '127.0.0.2'
    const request = Request(req)
    assert.strictEqual(request.ip, '127.0.0.2')
    req.socket.remoteAddress = '127.0.0.1'
    assert.strictEqual(request.ip, '127.0.0.2')
  })

  it('should reset ip work', () => {
    const req = { socket: new Stream.Duplex() }
    req.socket.remoteAddress = '127.0.0.2'
    const request = Request(req)
    assert.strictEqual(request.ip, '127.0.0.2')
    request.ip = '127.0.0.1'
    assert.strictEqual(request.ip, '127.0.0.1')
  })
})