-
-
Notifications
You must be signed in to change notification settings - Fork 733
/
test_events.js
96 lines (79 loc) · 2.32 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
'use strict'
const http = require('http')
const querystring = require('querystring')
const { test } = require('tap')
const nock = require('..')
require('./cleanup_after_each')()
function ignore() {}
test('emits request and replied events', function(t) {
const scope = nock('http://example.test')
.get('/please')
.reply(200)
scope.on('request', function(req, interceptor) {
t.equal(req.path, '/please')
t.equal(interceptor.interceptionCounter, 0)
scope.on('replied', function(req, interceptor) {
t.equal(req.path, '/please')
t.equal(interceptor.interceptionCounter, 1)
t.end()
})
})
http.get('http://example.test/please')
})
test('emits request and request body', function(t) {
const data = querystring.stringify({
example: 123,
})
const scope = nock('http://example.test')
.post('/please')
.reply(200)
scope.on('request', function(req, interceptor, body) {
t.equal(req.path, '/please')
t.equal(interceptor.interceptionCounter, 0)
t.equal(body, data)
scope.on('replied', function(req, interceptor) {
t.equal(req.path, '/please')
t.equal(interceptor.interceptionCounter, 1)
t.end()
})
})
const req = http.request({
hostname: 'example.test',
method: 'POST',
path: '/please',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(data),
},
})
req.write(data)
req.end()
})
test('emits no match when no match and no mock', function(t) {
nock.emitter.once('no match', function() {
t.end()
})
const req = http.get('http://example.test/abc')
req.once('error', ignore)
})
test('emits no match when no match and mocked', function(t) {
nock('http://example.test')
.get('/')
.reply(418)
const assertion = function(req) {
t.equal(req.path, '/definitelymaybe')
nock.emitter.removeAllListeners('no match')
t.end()
}
nock.emitter.on('no match', assertion)
http.get('http://example.test/definitelymaybe').once('error', ignore)
})
test('emits no match when netConnect is disabled', function(t) {
nock.disableNetConnect()
nock.emitter.on('no match', function(req) {
t.equal(req.hostname, 'example.test')
nock.emitter.removeAllListeners('no match')
t.end()
})
http.get('http://example.test').once('error', ignore)
})