UNPKG

3.94 kBJavaScriptView Raw
1var test = require('tape')
2var ajax = require('./ajax.js')
3var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest
4ajax.env.XMLHttpRequest = XMLHttpRequest
5
6test('call cb multiple time', t => {
7 var flow = 0
8 ajax.get('https://httpstat.us/200', () => {
9 flow++
10 throw new Error('sample err')
11 })
12 setTimeout(() => {
13 t.equal(flow, 1)
14 t.end()
15 }, 3000)
16})
17
18test('waterfall', t => {
19 ajax.waterfall(
20 [
21 function (param, cb) {
22 t.equal(param.a, 1)
23 param.a = 2
24 setTimeout(cb)
25 },
26 function (param, cb) {
27 t.equal(param.a, 2)
28 param.a = 3
29 setTimeout(cb)
30 },
31 ],
32 { a: 1 },
33 param => {
34 t.equal(param.a, 3)
35 t.end()
36 }
37 )
38})
39
40test('get 200', t => {
41 ajax.get('https://httpstat.us/200', (err, body, code) => {
42 t.equal(err, undefined)
43 t.equal(body, '200 OK')
44 t.equal(code, 200)
45 t.end()
46 })
47})
48
49test('get 404', t => {
50 ajax.env.XMLHttpRequest = XMLHttpRequest
51 ajax.get('https://httpstat.us/404', (err, body, code) => {
52 t.equal(err, undefined)
53 t.equal(body, '404 Not Found')
54 t.equal(code, 404)
55 t.end()
56 })
57})
58
59test('get 500', t => {
60 ajax.env.XMLHttpRequest = XMLHttpRequest
61 ajax.get('https://httpstat.us/500', undefined, (err, body, code) => {
62 t.equal(err, undefined)
63 t.equal(body, '500 Internal Server Error')
64 t.equal(code, 500)
65 t.end()
66 })
67})
68
69test('post JSON 200', t => {
70 ajax.env.XMLHttpRequest = XMLHttpRequest
71 ajax.
72 contentTypeJson().
73 setParser('json').
74 post('https://httpbin.org/anything', { a: 5, b: 6 }, (err, body, code) => {
75 t.equal(err, undefined)
76 t.equal(body.json.a, 5)
77 t.equal(body.json.b, 6)
78 t.equal(code, 200)
79 t.end()
80 })
81})
82
83test('before hook error', t => {
84 ajax.
85 beforeHook((param, cb) => {
86 param.request = param.request.addQuery('a', 'xinchao')
87 cb()
88 }).
89 beforeHook((param, cb) => {
90 param.error = 'just an sample error'
91 cb(false)
92 }).
93 setParser('json').
94 get('https://postman-echo.com/get', (err, body, code) => {
95 t.equal(code, 0)
96 t.equal(err, 'just an sample error')
97 t.equal(body, undefined)
98 t.end()
99 })
100})
101
102test('before hook', t => {
103 ajax.
104 beforeHook((param, cb) => {
105 param.request = param.request.addQuery('a', 'xinchao')
106 cb()
107 }).
108 beforeHook((param, cb) => {
109 cb(false)
110 }).
111 beforeHook((param, cb) => {
112 param.request = param.request.addQuery('b', 6)
113 cb()
114 }).
115 setParser('json').
116 get('https://postman-echo.com/get', (err, body, code) => {
117 t.equal(code, 200)
118 t.equal(err, undefined)
119 t.equal(body.args.a, 'xinchao')
120 t.equal(body.args.b, undefined)
121 t.end()
122 })
123})
124
125test('after hook retry fail', t => {
126 var count = 0
127 let req = ajax.
128 afterHook((param, cb) => {
129 if (param.code !== 500) return cb()
130 count++
131 var retry = param.request.meta.retry || 0
132 if (retry === 3) return cb()
133 var req = param.request.setMeta('retry', retry + 1) // increase counter
134 req[req.method]('', (err, body, code) => {
135 // continue retry
136 param.code = code
137 param.body = body
138 param.err = err
139 cb()
140 })
141 }).
142 get('https://httpstat.us/500', (err, body, code) => {
143 t.equal(count, 4)
144 t.equal(code, 500)
145 t.equal(body, '500 Internal Server Error')
146 t.equal(err, undefined)
147 t.end()
148 })
149})
150
151test('after hook retry success', t => {
152 var count = 0
153 let req = ajax.
154 afterHook((param, cb) => {
155 if (param.code !== 500) return cb()
156 count++
157 var retry = param.request.meta.retry || 0
158 if (retry === 2) {
159 param.request = param.request.setBaseUrl('https://httpstat.us/200')
160 }
161 if (retry === 3) return cb() // give up
162 var req = param.request.setMeta('retry', retry + 1) // increase counter
163 // continue retry
164 req[req.method]('', (err, body, code) => {
165 param.code = code
166 param.body = body
167 param.err = err
168 cb()
169 })
170 }).
171 get('https://httpstat.us/500', (err, body, code) => {
172 t.equal(count, 3)
173 t.equal(code, 200)
174 t.equal(body, '200 OK')
175 t.equal(err, undefined)
176 t.end()
177 })
178})