Testing Search Query String Manipulation in Koa.js
This test suite validates the search query string functionality in Koa’s request context, focusing on how the ctx.search property handles URL query parameters. It ensures proper integration between URL manipulation and query string parsing within the Koa framework.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
koajs/koa
__tests__/request/search.test.js
'use strict'
const { describe, it } = require('node:test')
const assert = require('assert')
const context = require('../../test-helpers/context')
describe('ctx.search=', () => {
it('should replace the search', () => {
const ctx = context({ url: '/store/shoes' })
ctx.search = '?page=2&color=blue'
assert.strictEqual(ctx.url, '/store/shoes?page=2&color=blue')
assert.strictEqual(ctx.search, '?page=2&color=blue')
})
it('should update ctx.querystring and ctx.query', () => {
const ctx = context({ url: '/store/shoes' })
ctx.search = '?page=2&color=blue'
assert.strictEqual(ctx.url, '/store/shoes?page=2&color=blue')
assert.strictEqual(ctx.querystring, 'page=2&color=blue')
assert.strictEqual(ctx.query.page, '2')
assert.strictEqual(ctx.query.color, 'blue')
})
it('should change .url but not .originalUrl', () => {
const ctx = context({ url: '/store/shoes' })
ctx.search = '?page=2&color=blue'
assert.strictEqual(ctx.url, '/store/shoes?page=2&color=blue')
assert.strictEqual(ctx.originalUrl, '/store/shoes')
assert.strictEqual(ctx.request.originalUrl, '/store/shoes')
})
describe('when missing', () => {
it('should return ""', () => {
const ctx = context({ url: '/store/shoes' })
assert.strictEqual(ctx.search, '')
})
})
})