-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
139 lines (111 loc) · 3.06 KB
/
test.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
'use strict'
const t = require('tap')
const { test } = t
const Fastify = require('fastify')
const urllib = require('urllib')
const http = require('http')
const https = require('https')
const fastifyCurl = require('./index')
test('fastify.httpclient and fastify.curl should exist', t => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifyCurl)
fastify.ready(err => {
t.error(err)
t.ok(fastify.httpclient)
t.ok(fastify.curl)
fastify.close()
})
})
test('fastify.httpclient should be an instance of urllib.HttpClient', t => {
t.plan(3)
const fastify = Fastify()
fastify.register(fastifyCurl)
fastify.ready(err => {
t.error(err)
t.ok(fastify.httpclient instanceof urllib.HttpClient)
t.same(fastify.httpclient.defaultArgs, {})
fastify.close()
})
})
test('fastifyCurl can be registered with options', t => {
t.plan(2)
const opts = { timeout: 1000 }
const fastify = Fastify()
fastify.register(fastifyCurl, opts)
fastify.ready(err => {
t.error(err)
t.same(fastify.httpclient.defaultArgs, opts)
fastify.close()
})
})
test('fastify.curl should be the same as urllib.HttpClient.curl method', t => {
t.plan(4)
const fastify = Fastify()
fastify.register(fastifyCurl)
fastify.ready(err => {
t.error(err)
fastify.curl('https://nodejs.org/en/', (err, data, res) => {
t.error(err)
t.ok(data instanceof Buffer)
t.equal(res.statusCode, 200)
fastify.close()
})
})
})
test('a custom client can be assigned', t => {
t.plan(2)
const fastify = Fastify()
const client = urllib.create()
fastify.register(fastifyCurl, { client })
fastify.ready(err => {
t.error(err)
t.is(fastify.httpclient, client)
fastify.close()
})
})
test('a custom http agent can be assigned', t => {
t.plan(3)
const fastify = Fastify()
const agent = new http.Agent()
fastify.register(fastifyCurl, { agent })
fastify.ready(err => {
t.error(err)
t.ok(fastify.httpclient.hasCustomAgent)
t.is(fastify.httpclient.agent, agent)
fastify.close()
})
})
test('a custom https agent can be assigned', t => {
t.plan(3)
const fastify = Fastify()
const httpsAgent = new https.Agent()
fastify.register(fastifyCurl, { httpsAgent })
fastify.ready(err => {
t.error(err)
t.ok(fastify.httpclient.hasCustomHttpsAgent)
t.is(fastify.httpclient.httpsAgent, httpsAgent)
fastify.close()
})
})
test('fastify.curl after registered with options should be the same as direct urllib.curl with options', t => {
t.plan(3)
const timing = {
queuing: /\d+/,
dnslookup: /\d+/,
connected: /\d+/,
requestSent: /\d+/,
waiting: /\d+/,
contentDownload: /\d+/
}
const fastify = Fastify()
fastify.register(fastifyCurl, { timing: true })
fastify.ready(async err => {
t.error(err)
const { res: { timing: timing1 } } = await fastify.curl('https://nodejs.org/en/')
t.match(timing1, timing)
const { res: { timing: timing2 } } = await urllib.curl('https://nodejs.org/en/', { timing: true })
t.match(timing2, timing)
fastify.close()
})
})