651ea94b10879e79f5d0fcdbd188674c5c2396ab
[yaffs-website] / node_modules / xhr / test / index.js
1 var window = require("global/window")
2 var test = require("tape")
3 var forEach = require("for-each")
4
5 var xhr = require("../index.js")
6
7 test("constructs and calls callback without throwing", function(assert) {
8     xhr({}, function(err, resp, body) {
9         assert.ok(true, "got here")
10         assert.end()
11     })
12 })
13
14 test("[func] Can GET a url (cross-domain)", function(assert) {
15     xhr({
16         uri: "http://www.mocky.io/v2/55a02cb72651260b1a94f024",
17         useXDR: true
18     }, function(err, resp, body) {
19         assert.ifError(err, "no err")
20         assert.equal(resp.statusCode, 200)
21         assert.equal(typeof resp.rawRequest, "object")
22         assert.notEqual(resp.body.length, 0)
23         assert.equal(resp.body, '{"a":1}')
24         assert.notEqual(body.length, 0)
25         assert.end()
26     })
27 })
28
29 test("[func] Returns http error responses like npm's request (cross-domain)", function(assert) {
30     if (!window.XDomainRequest) {
31         xhr({
32             uri: "http://www.mocky.io/v2/55a02d63265126221a94f025",
33             useXDR: true
34         }, function(err, resp, body) {
35             assert.ifError(err, "no err")
36             assert.equal(resp.statusCode, 404)
37             assert.equal(typeof resp.rawRequest, "object")
38             assert.end()
39         })
40     } else {
41         assert.end();
42     }
43 })
44
45 test("[func] Returns a falsy body for 204 responses", function(assert) {
46     xhr({
47         uri: "/mock/no-content"
48     }, function(err, resp, body) {
49         assert.notOk(body, "body should be falsey")
50         assert.equal(resp.statusCode, 204)
51         assert.end()
52     })
53 })
54
55 test("[func] Calls the callback once even if error is thrown issue #127", function(assert) {
56     var count = 0;
57     setTimeout(function(){
58         assert.equal(count, 1, "expected one call")
59         assert.end()
60     },100)
61     try{
62         xhr({
63             uri: "instanterror://foo"
64         }, function(err, resp, body) {
65             count++;
66             throw Error("dummy error")
67         })
68     } catch(e){}
69 })
70
71 test("[func] Times out to an error ", function(assert) {
72     xhr({
73         timeout: 1,
74         uri: "/mock/timeout"
75     }, function(err, resp, body) {
76         assert.ok(err instanceof Error, "should return error")
77         assert.equal(err.message, "XMLHttpRequest timeout")
78         assert.equal(err.code, "ETIMEDOUT")
79         assert.equal(resp.statusCode, 0)
80         assert.end()
81     })
82 })
83
84 test("withCredentials option", function(assert) {
85     if (!window.XDomainRequest) {
86         var req = xhr({}, function() {})
87         assert.ok(!req.withCredentials,
88             "withCredentials not true"
89         )
90         req = xhr({
91             withCredentials: true
92         }, function() {})
93         assert.ok(
94             req.withCredentials,
95             "withCredentials set to true"
96         )
97     }
98     assert.end()
99 })
100
101 test("withCredentials ignored when using synchronous requests", function(assert) {
102     if (!window.XDomainRequest) {
103         var req = xhr({
104             withCredentials: true,
105             sync: true
106         }, function() {})
107         assert.ok(!req.withCredentials,
108             "sync overrides withCredentials"
109         )
110     }
111     assert.end()
112 })
113
114 test("XDR usage (run on IE8 or 9)", function(assert) {
115     var req = xhr({
116         useXDR: true,
117         uri: window.location.href,
118     }, function() {})
119
120     assert.ok(!window.XDomainRequest || window.XDomainRequest === req.constructor,
121         "Uses XDR when told to"
122     )
123
124
125     if (!!window.XDomainRequest) {
126         assert.throws(function() {
127             xhr({
128                 useXDR: true,
129                 uri: window.location.href,
130                 headers: {
131                     "foo": "bar"
132                 }
133             }, function() {})
134         }, true, "Throws when trying to send headers with XDR")
135     }
136     assert.end()
137 })
138
139 test("handles errorFunc call with no arguments provided", function(assert) {
140     var req = xhr({}, function(err) {
141         assert.ok(err instanceof Error, "callback should get an error")
142         assert.equal(err.message, "Unknown XMLHttpRequest Error", "error message incorrect")
143     })
144     assert.doesNotThrow(function() {
145         req.onerror()
146     }, "should not throw when error handler called without arguments")
147     assert.end()
148
149 })
150
151 test("constructs and calls callback without throwing", function(assert) {
152     assert.throws(function() {
153         xhr({})
154     }, "callback is not optional")
155     assert.end()
156 })
157
158 if (!window.XDomainRequest) {
159     var methods = ["get", "put", "post", "patch"]
160 } else {
161     var methods = ["get", "post"]
162 }
163
164 test("[func] xhr[method] get, put, post, patch", function(assert) {
165     var i = 0
166
167     forEach(methods, function(method) {
168         xhr[method]({
169             uri: "/mock/200ok"
170         }, function(err, resp, body) {
171             i++
172             assert.ifError(err, "no err")
173             assert.equal(resp.statusCode, 200)
174             assert.equal(resp.method, method.toUpperCase())
175             if (i === methods.length) assert.end()
176         })
177     })
178 })
179
180 test("xhr[method] get, put, post, patch with url shorthands", function(assert) {
181     var i = 0
182     forEach(methods, function(method) {
183         var req = xhr[method]("/some-test", function() {})
184         i++
185         assert.equal(req.method, method.toUpperCase())
186
187         if (i === methods.length) assert.end()
188     })
189 })
190
191
192 test("xhr[method] get, put, post, patch with url shorthands and options", function(assert) {
193     var i = 0
194     forEach(methods, function(method) {
195         var req = xhr[method]("/some-test", {
196             headers: {
197                 foo: 'bar'
198             }
199         }, function(err, resp, body) {
200             i++
201             assert.equal(resp.rawRequest.headers.foo, 'bar')
202             assert.equal(resp.method, method.toUpperCase())
203
204             if (i === methods.length) assert.end()
205         })
206     })
207 })
208 if (!window.XDomainRequest) {
209     test("[func] xhr.head", function(assert) {
210         xhr.head({
211             uri: "/mock/200ok",
212         }, function(err, resp, body) {
213             assert.ifError(err, "no err")
214             assert.equal(resp.statusCode, 200)
215             assert.equal(resp.method, "HEAD")
216             assert.notOk(resp.body)
217             assert.end()
218         })
219     })
220
221     test("xhr.head url shorthand", function(assert) {
222         xhr.head("/mock/200ok", function(err, resp, body) {
223             assert.equal(resp.method, "HEAD")
224             assert.end()
225         })
226     })
227
228     test("[func] xhr.del", function(assert) {
229         xhr.del({
230             uri: "/mock/200ok"
231         }, function(err, resp, body) {
232             assert.ifError(err, "no err")
233             assert.equal(resp.statusCode, 200)
234             assert.equal(resp.method, "DELETE")
235             assert.end()
236         })
237     })
238
239     test("xhr.del url shorthand", function(assert) {
240         xhr.del("/mock/200ok", function(err, resp, body) {
241             assert.equal(resp.method, "DELETE")
242             assert.end()
243         })
244     })
245 }
246 test("url signature without object", function(assert) {
247     xhr("/some-test", function(err, resp, body) {
248         assert.equal(resp.url, '/some-test')
249         assert.end()
250     })
251 })
252
253 test("url signature with object", function(assert) {
254     xhr("/some-test", {
255         headers: {
256             "foo": "bar"
257         }
258     }, function(err, resp, body) {
259         assert.equal(resp.url, '/some-test')
260         assert.equal(resp.rawRequest.headers.foo, 'bar')
261         assert.end()
262     })
263 })
264
265 test("XHR can be overridden", function(assert) {
266     var xhrs = 0
267     var noop = function() {}
268     var fakeXHR = function() {
269         xhrs++
270         this.open = this.send = noop
271     }
272     var xdrs = 0
273     var fakeXDR = function() {
274         xdrs++
275         this.open = this.send = noop
276     }
277     xhr.XMLHttpRequest = fakeXHR
278     xhr({}, function() {})
279     assert.equal(xhrs, 1, "created the custom XHR")
280
281     xhr.XDomainRequest = fakeXDR
282     xhr({
283         useXDR: true
284     }, function() {});
285     assert.equal(xdrs, 1, "created the custom XDR")
286     assert.end()
287 })