-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathutil-post.test.js
More file actions
39 lines (32 loc) · 1 KB
/
util-post.test.js
File metadata and controls
39 lines (32 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const { test } = require('node:test')
const assert = require('assert')
const { EventEmitter } = require('events')
const Util = require('../src/server/util/Util')
test('Util.post sends 400 JSON when body is not valid JSON', async () => {
const req = new EventEmitter()
req.method = 'POST'
let settle
const done = new Promise((resolve) => {
settle = resolve
})
const res = {
statusCode: 200,
headers: {},
setHeader(name, value) {
this.headers[name] = value
},
end(payload) {
this.body = payload
settle()
}
}
Util.post(req, res, () => {
assert.fail('callback must not run when JSON.parse throws')
})
req.emit('data', Buffer.from('{not-json'))
req.emit('end')
await done
assert.strictEqual(res.statusCode, 400)
assert.strictEqual(res.headers['Content-Type'], 'application/json')
assert.deepStrictEqual(JSON.parse(res.body), { message: 'Invalid JSON body' })
})