-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
63 lines (59 loc) · 1.94 KB
/
test.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const Tom = require('test-runner').Tom
const RequestMonitor = require('./')
const Lws = require('lws')
const fetch = require('node-fetch')
const a = require('assert').strict
const LwsBodyParser = require('lws-body-parser')
const tom = module.exports = new Tom('request-monitor')
tom.test('GET', async function () {
const port = 8000 + this.index
const actuals = []
const lws = Lws.create({ port, stack: [ LwsBodyParser, RequestMonitor] })
lws.on('verbose', (key, value) => {
if (key === 'server.request' || key === 'server.response') {
actuals.push(key)
}
})
const response = await fetch(`http://localhost:${port}/`)
lws.server.close()
a.deepEqual(actuals, [ 'server.request', 'server.response' ])
})
tom.test('POST with form body', async function () {
const port = 8000 + this.index
const actuals = []
const lws = Lws.create({ port, stack: [ LwsBodyParser, RequestMonitor] })
lws.on('verbose', (key, value) => {
if (key === 'server.request' || key === 'server.response') {
actuals.push(key)
}
})
const response = await fetch(`http://localhost:${port}/`, {
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
body: 'one=1'
})
lws.server.close()
a.deepEqual(actuals, [ 'server.request', 'server.response' ])
// check the `reqInfo` emitted contains the request body
})
tom.test('POST with json body', async function () {
const port = 8000 + this.index
const actuals = []
const lws = Lws.create({ port, stack: [ LwsBodyParser, RequestMonitor] })
lws.on('verbose', (key, value) => {
if (key === 'server.request' || key === 'server.response') {
actuals.push(key)
}
})
const response = await fetch(`http://localhost:${port}/`, {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({ one: 1 })
})
lws.server.close()
a.deepEqual(actuals, [ 'server.request', 'server.response' ])
})