Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Change error mock to be more lenient #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 44 additions & 42 deletions src/mw/error.mock.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,57 @@ 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) {
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))
Expand Down
41 changes: 37 additions & 4 deletions test/thrower.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})