Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow statusCodes to be passed as an array or a set #115

Merged
merged 4 commits into from
Jul 28, 2020
Merged
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
10 changes: 7 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ module.exports = mkrequest => (...args) => {
} else if (typeof arg === 'number') {
statusCodes.add(arg)
} else if (typeof arg === 'object') {
if (headers) {
throw new Error('Cannot set headers twice.')
if (Array.isArray(arg) || arg instanceof Set) {
arg.forEach(code => statusCodes.add(code))
} else {
if (headers) {
throw new Error('Cannot set headers twice.')
}
headers = arg
}
headers = arg
} else {
throw new Error(`Unknown type: ${typeof arg}`)
}
Expand Down
18 changes: 18 additions & 0 deletions test/test-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ if (process.browser) {
})
}

test('multiple status', async () => {
const request = bent('string', [200, 201])
const str200 = await request(u('/echo.js?body=ok'))
same(str200, 'ok')

const str201 = await request(u('/echo.js?statusCode=201&body=ok'))
same(str201, 'ok')

try {
await request(u('/echo.js?statusCode=202&body=ok'))
throw new Error('Call should have thrown.')
} catch (e) {
same(e.message, process.browser ? null : 'Accepted')
// basic header test
same(e.headers['content-length'], '2')
}
})

test('PUT stream', async () => {
const body = Buffer.from(Math.random().toString())
const request = bent('PUT', 'json')
Expand Down