/
test_scope.js
181 lines (142 loc) · 4.61 KB
/
test_scope.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict'
const path = require('path')
const { expect } = require('chai')
const sinon = require('sinon')
const proxyquire = require('proxyquire').preserveCache()
const Interceptor = require('../lib/interceptor')
const nock = require('..')
const got = require('./got_client')
require('./setup')
it('scope exposes interceptors', () => {
const scopes = nock.load(path.join(__dirname, 'fixtures', 'goodRequest.json'))
expect(scopes).to.be.an.instanceOf(Array)
expect(scopes).to.have.lengthOf.at.least(1)
scopes.forEach(scope => {
scope.interceptors.forEach(interceptor => {
expect(interceptor).to.be.an.instanceOf(Interceptor)
interceptor.delayConnection(100)
})
})
})
describe('`Scope#remove()`', () => {
it('removes an active mock', () => {
const scope = nock('http://example.test')
.get('/')
.reply(200)
const key = 'GET http://example.test:80/'
// Confidence check.
expect(scope.activeMocks()).to.deep.equal([key])
// Act.
scope.remove(key, scope.interceptors[0])
// Assert.
expect(scope.activeMocks()).to.deep.equal([])
})
it('when a mock is persisted, does nothing', () => {
const scope = nock('http://example.test')
.persist()
.get('/')
.reply(200)
const key = 'GET http://example.test:80/'
// Confidence check.
expect(scope.activeMocks()).to.deep.equal([key])
// Act.
scope.remove(key, scope.interceptors[0])
// Assert.
expect(scope.activeMocks()).to.deep.equal([key])
})
it('when the key is nonexistent, does nothing', () => {
const scope = nock('http://example.test')
.get('/')
.reply(200)
const key = 'GET http://example.test:80/'
// Confidence check.
expect(scope.activeMocks()).to.deep.equal([key])
// Act.
scope.remove('GET http://bogus.test:80/', scope.interceptors[0])
// Assert.
expect(scope.activeMocks()).to.deep.equal([key])
})
})
it('loadDefs throws expected when fs is not available', () => {
const { loadDefs } = proxyquire('../lib/scope', { fs: null })
expect(() => loadDefs()).to.throw(Error, 'No fs')
})
describe('`Scope#isDone()`', () => {
it('returns false while a mock is pending, and true after it is consumed', async () => {
const scope = nock('http://example.test')
.get('/')
.reply()
expect(scope.isDone()).to.be.false()
await got('http://example.test/')
expect(scope.isDone()).to.be.true()
scope.done()
})
})
describe('filteringPath()', function() {
it('filter path with function', async function() {
const scope = nock('http://example.test')
.filteringPath(() => '/?a=2&b=1')
.get('/?a=2&b=1')
.reply()
const { statusCode } = await got('http://example.test/', {
query: { a: '1', b: '2' },
})
expect(statusCode).to.equal(200)
scope.done()
})
it('filter path with regexp', async () => {
const scope = nock('http://example.test')
.filteringPath(/\d/g, '3')
.get('/?a=3&b=3')
.reply()
const { statusCode } = await got('http://example.test/', {
query: { a: '1', b: '2' },
})
expect(statusCode).to.equal(200)
scope.done()
})
it('filteringPath with invalid argument throws expected', () => {
expect(() => nock('http://example.test').filteringPath('abc123')).to.throw(
Error,
'Invalid arguments: filtering path should be a function or a regular expression'
)
})
})
describe('filteringRequestBody()', () => {
it('filter body with function', async () => {
const onFilteringRequestBody = sinon.spy()
const scope = nock('http://example.test')
.filteringRequestBody(body => {
onFilteringRequestBody()
expect(body).to.equal('mamma mia')
return 'mamma tua'
})
.post('/', 'mamma tua')
.reply()
const { statusCode } = await got('http://example.test/', {
body: 'mamma mia',
})
expect(statusCode).to.equal(200)
expect(onFilteringRequestBody).to.have.been.calledOnce()
scope.done()
})
it('filter body with regexp', async () => {
const scope = nock('http://example.test')
.filteringRequestBody(/mia/, 'nostra')
.post('/', 'mamma nostra')
.reply(200, 'Hello World!')
const { statusCode } = await got('http://example.test/', {
body: 'mamma mia',
})
expect(statusCode).to.equal(200)
scope.done()
})
it('filteringRequestBody with invalid argument throws expected', () => {
expect(() =>
nock('http://example.test').filteringRequestBody('abc123')
).to.throw(
Error,
'Invalid arguments: filtering request body should be a function or a regular expression'
)
})
})