UNPKG

6.45 kBJavaScriptView Raw
1/*
2 * @Filename: test.js
3 * @Author: jin5354
4 * @Email: xiaoyanjinx@gmail.com
5 * @Last Modified time: 2017-06-24 21:28:35
6 */
7import 'regenerator-runtime/runtime'
8import axios from 'axios'
9import test from 'ava'
10import wrapper from '../src/index.js'
11
12const pause = function(time) {
13 return new Promise((resolve) => {
14 setTimeout(() => {
15 resolve()
16 }, time)
17 })
18}
19
20test('wrapped axios feature basis test', async t => {
21
22 let http = wrapper(axios)
23
24 let response1 = await http({
25 url: 'http://www.404forest.com:3000/users/jin5354',
26 method: 'get'
27 })
28
29 let response2 = await http({
30 url: 'http://www.404forest.com:3000/users/jin5354'
31 })
32
33 let response3 = await http.get('http://www.404forest.com:3000/users/jin5354')
34
35 let response4 = await http({
36 url: 'http://www.404forest.com:3000/users/yyhappynice',
37 method: 'post',
38 data: {
39 id: 10
40 }
41 })
42
43 let response5 = await http.get('http://www.404forest.com:3000/users/jin5354', {
44 params: {
45 ID: 'somethingelse'
46 }
47 })
48
49 let response6 = await http.post('http://www.404forest.com:3000/users/llissery', {
50 id: 11
51 })
52
53 let response7 = await http.get('http://www.404forest.com:3000/users/llissery', {
54 params: {
55 ID: 'somethingelse'
56 }
57 }, 'needless fault-tolerance param')
58
59 t.is(response1.data.id, 6868950)
60 t.is(response2.data.id, 6868950)
61 t.is(response3.data.id, 6868950)
62 t.is(response4.data.id, 10)
63 t.is(response5.data.id, 6868950)
64 t.is(response6.data.id, 11)
65 t.is(response7.data.id, 11)
66})
67
68test('wrapped axios instance feature basis test', async t => {
69
70 let instance = axios.create({
71 withCredentials: false
72 })
73 instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
74
75 let http = wrapper(instance)
76
77 let response1 = await http({
78 url: 'http://www.404forest.com:3000/users/jin5354',
79 method: 'get'
80 })
81
82 let response2 = await http({
83 url: 'http://www.404forest.com:3000/users/jin5354'
84 })
85
86 let response3 = await http.get('http://www.404forest.com:3000/users/jin5354')
87
88 t.is(response1.data.id, 6868950)
89 t.is(response2.data.id, 6868950)
90 t.is(response3.data.id, 6868950)
91})
92
93test('addFilter and removeFilter test', async t => {
94
95 let http = wrapper(axios)
96 let reg = /users/
97 t.is(http.__cacher.filters.length, 0)
98 http.__addFilter(reg)
99
100 t.is(http.__cacher.filters.length, 1)
101 t.truthy(http.__cacher.filters.indexOf(reg) !== -1)
102 t.is(http.__cacher.cacheMap.size, 0)
103
104 await http({
105 url: 'http://www.404forest.com:3000/users/jin5354',
106 method: 'get'
107 })
108
109 t.is(http.__cacher.cacheMap.size, 1)
110 http.__removeFilter(reg)
111 t.is(http.__cacher.filters.length, 0)
112 http.__removeFilter(/test/)
113 t.is(http.__cacher.filters.length, 0)
114
115 await http({
116 url: 'http://www.404forest.com:3000/users/yyhappynice',
117 method: 'get'
118 })
119
120 t.is(http.__cacher.cacheMap.size, 1)
121})
122
123test('maxCacheSize test', async t => {
124
125 let http = wrapper(axios, {
126 maxCacheSize: 3
127 })
128 let reg = /users/
129 http.__addFilter(reg)
130
131 t.is(http.__cacher.maxCacheSize, 3)
132 t.is(http.__cacher.cacheMap.size, 0)
133 await http({
134 url: 'http://www.404forest.com:3000/users/jin5354',
135 method: 'get'
136 })
137 await http({
138 url: 'http://www.404forest.com:3000/users/yyhappynice',
139 method: 'get'
140 })
141 await http({
142 url: 'http://www.404forest.com:3000/users/llissery',
143 method: 'get'
144 })
145 t.is(http.__cacher.cacheMap.size, 3)
146 await http({
147 url: 'http://www.404forest.com:3000/users/IOriens',
148 method: 'get'
149 })
150 t.is(http.__cacher.cacheMap.size, 3)
151 t.falsy(http.__cacher.hasCache({
152 url: 'http://www.404forest.com:3000/users/jin5354',
153 method: 'get'
154 }))
155
156})
157
158test('ttl test', async t => {
159
160 let http = wrapper(axios, {
161 ttl: 1000
162 })
163 let reg = /users/
164 http.__addFilter(reg)
165
166 t.is(http.__cacher.ttl, 1000)
167 t.is(http.__cacher.cacheMap.size, 0)
168 await http({
169 url: 'http://www.404forest.com:3000/users/jin5354',
170 method: 'get'
171 })
172 t.is(http.__cacher.cacheMap.size, 1)
173 t.truthy(http.__cacher.hasCache({
174 url: 'http://www.404forest.com:3000/users/jin5354',
175 method: 'get'
176 }))
177
178 await pause(1100)
179
180 t.falsy(http.__cacher.hasCache({
181 url: 'http://www.404forest.com:3000/users/jin5354',
182 method: 'get'
183 }))
184
185})
186
187test('hit cache test', async t => {
188
189 let http = wrapper(axios)
190 let reg = /users/
191 http.__addFilter(reg)
192
193 let response1 = await http({
194 url: 'http://www.404forest.com:3000/users/jin5354',
195 method: 'get'
196 })
197
198 let response2 = await http({
199 url: 'http://www.404forest.com:3000/users/jin5354',
200 method: 'get'
201 })
202
203 t.is(response1.data.id, 6868950)
204 t.is(response1.__fromAxiosCache, undefined)
205 t.is(response2.data.id, 6868950)
206 t.is(response2.__fromAxiosCache, true)
207})
208
209test('ttl with maxCacheSize test', async t => {
210
211 let http = wrapper(axios, {
212 maxCacheSize: 1,
213 ttl: 10000
214 })
215 let reg = /users/
216 http.__addFilter(reg)
217
218 await http({
219 url: 'http://www.404forest.com:3000/users/jin5354',
220 method: 'get'
221 })
222 t.is(http.__cacher.cacheMap.size, 1)
223 await http({
224 url: 'http://www.404forest.com:3000/users/yyhappynice',
225 method: 'get'
226 })
227 t.is(http.__cacher.cacheMap.size, 1)
228
229 await pause(11000)
230
231 t.falsy(http.__cacher.hasCache({
232 url: 'http://www.404forest.com:3000/users/jin5354',
233 method: 'get'
234 }))
235
236})
237
238test('with options ignore header', async t => {
239
240 let http = wrapper(axios, {
241 excludeHeaders: true
242 })
243 let reg = /users/
244 http.__addFilter(reg)
245
246 await http({
247 url: 'http://www.404forest.com:3000/users/jin5354',
248 method: 'get',
249 headers: {
250 'unique-prop': Math.random().toString()
251 }
252 })
253 t.is(http.__cacher.cacheMap.size, 1)
254 await http({
255 url: 'http://www.404forest.com:3000/users/jin5354',
256 method: 'get',
257 headers: {
258 'unique-prop': Math.random().toString()
259 }
260 })
261 t.is(http.__cacher.cacheMap.size, 1)
262})
263
264test('with out options ignore header', async t => {
265
266 let http = wrapper(axios, {})
267
268 let reg = /users/
269 http.__addFilter(reg)
270
271 await http({
272 url: 'http://www.404forest.com:3000/users/jin5354',
273 method: 'get',
274 headers: {
275 'unique-prop': Math.random().toString()
276 }
277 })
278 t.is(http.__cacher.cacheMap.size, 1)
279 await http({
280 url: 'http://www.404forest.com:3000/users/jin5354',
281 method: 'get',
282 headers: {
283 'unique-prop': Math.random().toString()
284 }
285 })
286 t.is(http.__cacher.cacheMap.size, 2)
287})