-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
152 lines (132 loc) · 3.67 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
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict'
const t = require('tap')
const { test } = t
const Fastify = require('fastify')
const axiosPlugin = require('./index')
test('fastify.axios intance and instance.methods exists', t => {
t.plan(12)
const fastify = Fastify()
fastify.register(axiosPlugin, {})
fastify.ready(err => {
t.error(err)
t.ok(fastify.axios)
t.ok(fastify.axios.post)
t.ok(fastify.axios.get)
t.ok(fastify.axios.patch)
t.ok(fastify.axios.options)
t.ok(fastify.axios.put)
t.ok(fastify.axios.getUri)
t.ok(fastify.axios.delete)
t.ok(fastify.axios.head)
t.ok(fastify.axios.request)
t.equal(fastify.axios.defaults.defaultArgs, undefined)
fastify.close()
})
})
test('fastify.axios should be an axios instance', t => {
t.plan(2)
const fastify = Fastify()
fastify.register(axiosPlugin)
fastify.ready(err => {
t.error(err)
t.ok(fastify.axios instanceof Function)
fastify.close()
})
})
test('verify default args', t => {
t.plan(4)
const fastify = Fastify()
fastify.register(axiosPlugin, {
baseURL: 'https://nodejs.org',
interceptors: {
request: function (config) {
console.log(config)
},
errorRequest: function (error) {
console.log(error)
},
response: function (config) {
console.log(config)
},
errorResponse: function (error) {
console.log(error)
}
}
})
fastify.ready(async err => {
t.error(err)
t.equal(fastify.axios.defaults.baseURL, 'https://nodejs.org')
t.equal(typeof fastify.axios.interceptors.request, 'object')
t.equal(typeof fastify.axios.interceptors.response, 'object')
fastify.close()
})
})
test('fastify.axios works well', t => {
t.plan(3)
const fastify = Fastify()
fastify.register(axiosPlugin)
fastify.ready(async err => {
t.error(err)
const { data, status } = await fastify.axios.get('https://nodejs.org/en/')
t.ok(data)
t.equal(status, 200)
fastify.close()
})
})
test('fastify.axios register multiple clients', t => {
t.plan(26)
const fastify = Fastify()
fastify.register(axiosPlugin, {
clients: {
nodejs: {
baseURL: 'https://nodejs.org'
},
google: {
baseURL: 'https://google.com',
interceptors: {
request: function (config) {
console.log(config)
},
errorRequest: function (error) {
console.log(error)
},
response: function (config) {
console.log(config)
},
errorResponse: function (error) {
console.log(error)
}
}
}
}
})
fastify.ready(async err => {
t.error(err)
t.ok(fastify.axios.nodejs)
t.ok(fastify.axios.nodejs instanceof Function)
t.ok(fastify.axios.nodejs.post)
t.ok(fastify.axios.nodejs.get)
t.ok(fastify.axios.nodejs.patch)
t.ok(fastify.axios.nodejs.options)
t.ok(fastify.axios.nodejs.put)
t.ok(fastify.axios.nodejs.getUri)
t.ok(fastify.axios.nodejs.delete)
t.ok(fastify.axios.nodejs.head)
t.ok(fastify.axios.nodejs.request)
t.equal(fastify.axios.nodejs.defaults.baseURL, 'https://nodejs.org')
t.ok(fastify.axios.google)
t.ok(fastify.axios instanceof Object)
t.ok(fastify.axios.google instanceof Function)
t.ok(fastify.axios.google.post)
t.ok(fastify.axios.google.get)
t.ok(fastify.axios.google.patch)
t.ok(fastify.axios.google.options)
t.ok(fastify.axios.google.put)
t.ok(fastify.axios.google.getUri)
t.ok(fastify.axios.google.delete)
t.ok(fastify.axios.google.head)
t.ok(fastify.axios.google.request)
t.equal(fastify.axios.google.defaults.baseURL, 'https://google.com')
fastify.close()
})
})