/
test_fetch.js
93 lines (74 loc) · 2.38 KB
/
test_fetch.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
'use strict'
const { expect } = require('chai')
const nock = require('..')
const assertRejects = require('assert-rejects')
const { startHttpServer } = require('./servers')
describe('Native Fetch', () => {
it('input is string', async () => {
const scope = nock('http://example.test').get('/').reply()
const { status } = await fetch('http://example.test/')
expect(status).to.equal(200)
scope.done()
})
it('input is URL', async () => {
const scope = nock('http://example.test').get('/').reply()
const { status } = await fetch(new URL('http://example.test/'))
expect(status).to.equal(200)
scope.done()
})
it('input is Request object', async () => {
const scope = nock('http://example.test').get('/').reply()
const { status } = await fetch(new Request('http://example.test/'))
expect(status).to.equal(200)
scope.done()
})
it('filter by body', async () => {
const scope = nock('http://example.test')
.post('/', { test: 'fetch' })
.reply()
const { status } = await fetch('http://example.test/', {
method: 'POST',
body: JSON.stringify({ test: 'fetch' }),
})
expect(status).to.equal(200)
scope.done()
})
it('filter by request body', async () => {
const scope = nock('http://example.test')
.post('/', { test: 'fetch' })
.reply()
const { status } = await fetch(
new Request('http://example.test/', {
method: 'POST',
body: JSON.stringify({ test: 'fetch' }),
}),
)
expect(status).to.equal(200)
scope.done()
})
it('no match', async () => {
nock('http://example.test').get('/').reply()
await assertRejects(
fetch('http://example.test/wrong-path'),
/Nock: No match for request/,
)
})
it('forward request if no mock', async () => {
const { origin } = await startHttpServer((request, response) => {
response.write('live')
response.end()
})
const { status } = await fetch(origin)
expect(status).to.equal(200)
})
it('should work with empty response', async () => {
nock('http://example.test').get('/').reply(204)
const { status } = await fetch('http://example.test')
expect(status).to.equal(204)
})
it('should work https', async () => {
nock('https://example.test').get('/').reply()
const { status } = await fetch('https://example.test')
expect(status).to.equal(200)
})
})