From 3261d6dad6da1b06d941317d4dc54d13b30ea9bb Mon Sep 17 00:00:00 2001 From: Alessandro Grosselle Date: Sat, 12 Jul 2025 17:03:35 +0200 Subject: [PATCH 1/2] fix: on layer.js set also statusCode --- lib/layer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/layer.js b/lib/layer.js index 6a4408f..fe0e346 100644 --- a/lib/layer.js +++ b/lib/layer.js @@ -226,7 +226,7 @@ function decodeParam (val) { } catch (err) { if (err instanceof URIError) { err.message = 'Failed to decode param \'' + val + '\'' - err.status = 400 + err.status = err.statusCode = 400 } throw err From 7bfbb4f07bd65207b38b2bc2d988dc4ed8c6183c Mon Sep 17 00:00:00 2001 From: Alessandro Grosselle Date: Wed, 16 Jul 2025 15:55:45 +0200 Subject: [PATCH 2/2] chore: unit test implemented --- test/layer.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/layer.js diff --git a/test/layer.js b/test/layer.js new file mode 100644 index 0000000..bc01212 --- /dev/null +++ b/test/layer.js @@ -0,0 +1,42 @@ +'use strict' +const { it, describe } = require('mocha') +const assert = require('assert') +const Layer = require('../lib/layer') + +describe('decodeParam', function () { + it('should set statusCode to 400 on URIError', function () { + const layer = new Layer('/:param', {}, function handle () {}) + + // Create a malformed URI component that will cause a decoding error + const malformedParam = '%Z' + + try { + // Using match will internally call decodeParam with our malformed value + layer.match('/' + malformedParam) + + // If we reach here, no error was thrown, which is a failure + assert.fail('Expected URIError to be thrown') + } catch (err) { + // Verify that the URIError was enhanced with the correct status properties + assert.strictEqual(err.status, 400, 'Expected err.status to be 400') + assert.strictEqual(err.statusCode, 400, 'Expected err.statusCode to be 400') + assert.strictEqual(err.message, `Failed to decode param '${malformedParam}'`, 'Expected proper error message') + } + }) + + it('should successfully decode valid parameters', function () { + // Create a layer with a parameter route - note the need to provide a handler function + const layer = new Layer('/:param', {}, function handle () {}) + + // Create a valid encoded parameter + const original = 'test value' + const encoded = encodeURIComponent(original) + + // Match will internally call decodeParam with our encoded value + const matched = layer.match('/' + encoded) + + // Verify the parameter was properly decoded + assert.strictEqual(matched, true, 'Route should match') + assert.strictEqual(layer.params.param, original, 'Parameter should be properly decoded') + }) +})