From 516c178a32ece477a21cc6be7a71b3ee5f007b3c Mon Sep 17 00:00:00 2001 From: Ben Peachey Date: Sat, 24 Aug 2024 22:04:39 +0200 Subject: [PATCH 1/3] Remove slashes from status codes in error.mock.mjs. --- src/mw/error.mock.mjs | 80 +++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/src/mw/error.mock.mjs b/src/mw/error.mock.mjs index 34cbde4..76448f6 100644 --- a/src/mw/error.mock.mjs +++ b/src/mw/error.mock.mjs @@ -23,47 +23,47 @@ const badRequest = (error) => { let error, expect, token const status = { - '/400/': 'Bad Request', - '/401/': 'Unauthorized', - '/402/': 'Payment Required', - '/403/': 'Forbidden', - '/404/': 'Not Found', - '/405/': 'Method Not Allowed', - '/406/': 'Not Acceptable', - '/407/': 'Proxy Authentication Required', - '/408/': 'Request Timeout', - '/409/': 'Conflict', - '/410/': 'Gone', - '/411/': 'Length Required', - '/412/': 'Precondition Failed', - '/413/': 'Payload Too Large', - '/414/': 'URI Too Long', - '/415/': 'Unsupported Media Type', - '/416/': 'Range Not Satisfiable', - '/417/': 'Expectation Failed', - '/418/': "I'm a teapot", - '/421/': 'Misdireceted Request', - '/422/': 'Unprocessable Content', - '/423/': 'Locked', - '/424/': 'Failed Dependency', - '/425/': 'Too Early', - '/426/': 'Upgrade Required', - '/428/': 'Precondition Required', - '/429/': 'Too Many Requests', - '/431/': 'Request Header Fields Too Large', - '/451/': 'Unavailable For Legal Reasons', + '400': 'Bad Request', + '401': 'Unauthorized', + '402': 'Payment Required', + '403': 'Forbidden', + '404': 'Not Found', + '405': 'Method Not Allowed', + '406': 'Not Acceptable', + '407': 'Proxy Authentication Required', + '408': 'Request Timeout', + '409': 'Conflict', + '410': 'Gone', + '411': 'Length Required', + '412': 'Precondition Failed', + '413': 'Payload Too Large', + '414': 'URI Too Long', + '415': 'Unsupported Media Type', + '416': 'Range Not Satisfiable', + '417': 'Expectation Failed', + '418': "I'm a teapot", + '421': 'Misdireceted Request', + '422': 'Unprocessable Content', + '423': 'Locked', + '424': 'Failed Dependency', + '425': 'Too Early', + '426': 'Upgrade Required', + '428': 'Precondition Required', + '429': 'Too Many Requests', + '431': 'Request Header Fields Too Large', + '451': 'Unavailable For Legal Reasons', - '/500/': 'Internal Server Error', - '/501/': 'Not Implemented', - '/502/': 'Bad Gateway', - '/503/': 'Service Unavailable', - '/504/': 'Gateway Timeout', - '/505/': 'HTTP Version Not Supported', - '/506/': 'Variant Also Negotiated', - '/507/': 'Insufficient Storage', - '/508/': 'Loop Detected', - '/510/': 'Not Extended', - '/511/': 'Network Authentication Required' + '500': 'Internal Server Error', + '501': 'Not Implemented', + '502': 'Bad Gateway', + '503': 'Service Unavailable', + '504': 'Gateway Timeout', + '505': 'HTTP Version Not Supported', + '506': 'Variant Also Negotiated', + '507': 'Insufficient Storage', + '508': 'Loop Detected', + '510': 'Not Extended', + '511': 'Network Authentication Required' } export default function errorMock(options) { From de1295cfec24440cd3e2296ad4a322c6192f3a21 Mon Sep 17 00:00:00 2001 From: Ben Peachey Date: Sat, 24 Aug 2024 22:06:38 +0200 Subject: [PATCH 2/3] Change error.mock.mjs to allow for nested status code paths. --- src/mw/error.mock.mjs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/mw/error.mock.mjs b/src/mw/error.mock.mjs index 76448f6..98e33a0 100644 --- a/src/mw/error.mock.mjs +++ b/src/mw/error.mock.mjs @@ -69,9 +69,11 @@ const status = { export default function errorMock(options) { return (req, next) => { let url = metro.url(req.url) - if (status[url.pathname]) { + const code = url.pathname.split('/').filter(Boolean).pop() + + if (status[code]) { let error = { - code: parseInt(url.pathname.substring(1)), + code: parseInt(code, 10), message: status[url.pathname] } return metro.response(badRequest(error)) From a31239e23ff522ef9a65a656b54122b6673e7e23 Mon Sep 17 00:00:00 2001 From: Ben Peachey Date: Sat, 24 Aug 2024 22:07:26 +0200 Subject: [PATCH 3/3] Change tests for error.mock.mjs to reflect changes from previous commit. --- test/thrower.mjs | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/test/thrower.mjs b/test/thrower.mjs index 1a081c7..1cac63e 100644 --- a/test/thrower.mjs +++ b/test/thrower.mjs @@ -3,13 +3,46 @@ import * as metro from '../src/metro.mjs' import throwermw from '../src/mw/thrower.mjs' import errormw from '../src/mw/error.mock.mjs' -tap.test('start', async t => { +tap.test('Status code in root with trailing slash', async t => { let c = metro.client().with(errormw()).with(throwermw()) try { let res = await c.get('/404/') - t.ok(false) + t.fail() } catch(e) { - t.ok(true) - } + t.pass() + } + t.end() +}) + +tap.test('Status code in root without trailing slash', async t => { + let c = metro.client().with(errormw()).with(throwermw()) + try { + let res = await c.get('/404') + t.fail() + } catch(e) { + t.pass() + } + t.end() +}) + +tap.test('Status code in nested path with trailing slash', async t => { + let c = metro.client().with(errormw()).with(throwermw()) + try { + let res = await c.get('/nested/404/') + t.fail() + } catch(e) { + t.pass() + } + t.end() +}) + +tap.test('Status code in nested path without trailing slash', async t => { + let c = metro.client().with(errormw()).with(throwermw()) + try { + let res = await c.get('/nested/404') + t.fail() + } catch(e) { + t.pass() + } t.end() })