-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_net_connect.js
127 lines (99 loc) · 3.51 KB
/
test_net_connect.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
'use strict'
const http = require('http')
const { expect } = require('chai')
const assertRejects = require('assert-rejects')
const sinon = require('sinon')
const nock = require('..')
const got = require('./got_client')
require('./setup')
describe('`disableNetConnect()`', () => {
it('prevents connection to unmocked hosts', async () => {
nock.disableNetConnect()
nock('http://www.example.test')
.get('/')
.reply(200)
await assertRejects(
got('https://other.example.test/'),
/Nock: Disallowed net connect for "other.example.test:443\/"/
)
})
it('prevents connections when no hosts are mocked', async () => {
nock.disableNetConnect()
await assertRejects(got('http://example.test'), err => {
expect(err).to.include({
code: 'ENETUNREACH',
message: 'Nock: Disallowed net connect for "example.test:80/"',
})
expect(err.stack).to.be.a('string')
return true
})
})
})
describe('`enableNetConnect()`', () => {
it('enables real HTTP request only for specified domain, via string', async () => {
const onResponse = sinon.spy()
const server = http.createServer((request, response) => {
onResponse()
response.writeHead(200)
response.end()
})
await new Promise(resolve => server.listen(resolve))
nock.enableNetConnect('localhost')
await got(`http://localhost:${server.address().port}/`)
expect(onResponse).to.have.been.calledOnce()
server.close()
})
it('disallows request for other domains, via string', async () => {
nock.enableNetConnect('localhost')
await assertRejects(
got('https://example.test/'),
/Nock: Disallowed net connect for "example.test:443\/"/
)
})
it('enables real HTTP request only for specified domain, via regexp', async () => {
const onResponse = sinon.spy()
const server = http.createServer((request, response) => {
onResponse()
response.writeHead(200)
response.end()
})
await new Promise(resolve => server.listen(resolve))
nock.enableNetConnect(/ocalhos/)
await got(`http://localhost:${server.address().port}/`)
expect(onResponse).to.have.been.calledOnce()
server.close()
})
it('disallows request for other domains, via regexp', async () => {
nock.enableNetConnect(/ocalhos/)
await assertRejects(
got('https://example.test/'),
/Nock: Disallowed net connect for "example.test:443\/"/
)
})
it('enables real HTTP request only for specified domain, via function', async () => {
const onResponse = sinon.spy()
const server = http.createServer((request, response) => {
onResponse()
response.writeHead(200)
response.end()
})
await new Promise(resolve => server.listen(resolve))
nock.enableNetConnect(host => host.includes('ocalhos'))
await got(`http://localhost:${server.address().port}/`)
expect(onResponse).to.have.been.calledOnce()
server.close()
})
it('disallows request for other domains, via function', async () => {
nock.enableNetConnect(host => host.includes('ocalhos'))
await assertRejects(
got('https://example.test/'),
/Nock: Disallowed net connect for "example.test:443\/"/
)
})
it('passes the domain to be tested, via function', async () => {
const matcher = sinon.stub().returns(false)
nock.enableNetConnect(matcher)
await got('https://example.test/').catch(() => undefined) // ignore rejection, expected
expect(matcher).to.have.been.calledOnceWithExactly('example.test:443')
})
})