-
-
Notifications
You must be signed in to change notification settings - Fork 733
/
test_events.js
99 lines (75 loc) · 2.29 KB
/
test_events.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
'use strict'
const { expect } = require('chai')
const http = require('http')
const sinon = require('sinon')
const nock = require('..')
const got = require('./got_client')
require('./setup')
function ignore() {}
it('emits request and replied events when request has no body', async () => {
const scope = nock('http://example.test')
.get('/')
.reply()
const onRequest = sinon.spy()
const onReplied = sinon.spy()
scope.on('request', onRequest)
scope.on('replied', onReplied)
await got('http://example.test')
scope.done()
expect(onRequest).to.have.been.calledOnce()
expect(onReplied).to.have.been.calledOnce()
})
it('emits request and request body', async () => {
const data = 'example=123'
const scope = nock('http://example.test')
.post('/please')
.reply()
const onRequest = sinon.spy()
const onReplied = sinon.spy()
scope.on('request', function(req, interceptor, body) {
onRequest()
expect(req.path).to.equal('/please')
expect(interceptor.interceptionCounter).to.equal(0)
expect(body).to.deep.equal(data)
expect(onReplied).to.not.have.been.called()
})
scope.on('replied', function(req, interceptor) {
onReplied()
expect(req.path).to.equal('/please')
expect(interceptor.interceptionCounter).to.equal(1)
})
await got.post('http://example.test/please', {
body: data,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data),
},
})
scope.done()
expect(onRequest).to.have.been.calledOnce()
expect(onReplied).to.have.been.calledOnce()
})
it('emits no match when no match and no mock', done => {
nock.emitter.once('no match', () => {
done()
})
http.get('http://example.test/abc').once('error', ignore)
})
it('emits no match when no match and mocked', done => {
nock('http://example.test')
.get('/')
.reply(418)
nock.emitter.on('no match', req => {
expect(req.path).to.equal('/definitelymaybe')
done()
})
http.get('http://example.test/definitelymaybe').once('error', ignore)
})
it('emits no match when netConnect is disabled', done => {
nock.disableNetConnect()
nock.emitter.on('no match', req => {
expect(req.hostname).to.equal('example.test')
done()
})
http.get('http://example.test').once('error', ignore)
})