1 var window = require("global/window")
2 var test = require("tape")
3 var forEach = require("for-each")
5 var xhr = require("../index.js")
7 test("constructs and calls callback without throwing", function(assert) {
8 xhr({}, function(err, resp, body) {
9 assert.ok(true, "got here")
14 test("[func] Can GET a url (cross-domain)", function(assert) {
16 uri: "http://www.mocky.io/v2/55a02cb72651260b1a94f024",
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)
29 test("[func] Returns http error responses like npm's request (cross-domain)", function(assert) {
30 if (!window.XDomainRequest) {
32 uri: "http://www.mocky.io/v2/55a02d63265126221a94f025",
34 }, function(err, resp, body) {
35 assert.ifError(err, "no err")
36 assert.equal(resp.statusCode, 404)
37 assert.equal(typeof resp.rawRequest, "object")
45 test("[func] Returns a falsy body for 204 responses", function(assert) {
47 uri: "/mock/no-content"
48 }, function(err, resp, body) {
49 assert.notOk(body, "body should be falsey")
50 assert.equal(resp.statusCode, 204)
55 test("[func] Calls the callback once even if error is thrown issue #127", function(assert) {
57 setTimeout(function(){
58 assert.equal(count, 1, "expected one call")
63 uri: "instanterror://foo"
64 }, function(err, resp, body) {
66 throw Error("dummy error")
71 test("[func] Times out to an error ", function(assert) {
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)
84 test("withCredentials option", function(assert) {
85 if (!window.XDomainRequest) {
86 var req = xhr({}, function() {})
87 assert.ok(!req.withCredentials,
88 "withCredentials not true"
95 "withCredentials set to true"
101 test("withCredentials ignored when using synchronous requests", function(assert) {
102 if (!window.XDomainRequest) {
104 withCredentials: true,
107 assert.ok(!req.withCredentials,
108 "sync overrides withCredentials"
114 test("XDR usage (run on IE8 or 9)", function(assert) {
117 uri: window.location.href,
120 assert.ok(!window.XDomainRequest || window.XDomainRequest === req.constructor,
121 "Uses XDR when told to"
125 if (!!window.XDomainRequest) {
126 assert.throws(function() {
129 uri: window.location.href,
134 }, true, "Throws when trying to send headers with XDR")
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")
144 assert.doesNotThrow(function() {
146 }, "should not throw when error handler called without arguments")
151 test("constructs and calls callback without throwing", function(assert) {
152 assert.throws(function() {
154 }, "callback is not optional")
158 if (!window.XDomainRequest) {
159 var methods = ["get", "put", "post", "patch"]
161 var methods = ["get", "post"]
164 test("[func] xhr[method] get, put, post, patch", function(assert) {
167 forEach(methods, function(method) {
170 }, function(err, resp, body) {
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()
180 test("xhr[method] get, put, post, patch with url shorthands", function(assert) {
182 forEach(methods, function(method) {
183 var req = xhr[method]("/some-test", function() {})
185 assert.equal(req.method, method.toUpperCase())
187 if (i === methods.length) assert.end()
192 test("xhr[method] get, put, post, patch with url shorthands and options", function(assert) {
194 forEach(methods, function(method) {
195 var req = xhr[method]("/some-test", {
199 }, function(err, resp, body) {
201 assert.equal(resp.rawRequest.headers.foo, 'bar')
202 assert.equal(resp.method, method.toUpperCase())
204 if (i === methods.length) assert.end()
208 if (!window.XDomainRequest) {
209 test("[func] xhr.head", function(assert) {
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)
221 test("xhr.head url shorthand", function(assert) {
222 xhr.head("/mock/200ok", function(err, resp, body) {
223 assert.equal(resp.method, "HEAD")
228 test("[func] xhr.del", function(assert) {
231 }, function(err, resp, body) {
232 assert.ifError(err, "no err")
233 assert.equal(resp.statusCode, 200)
234 assert.equal(resp.method, "DELETE")
239 test("xhr.del url shorthand", function(assert) {
240 xhr.del("/mock/200ok", function(err, resp, body) {
241 assert.equal(resp.method, "DELETE")
246 test("url signature without object", function(assert) {
247 xhr("/some-test", function(err, resp, body) {
248 assert.equal(resp.url, '/some-test')
253 test("url signature with object", function(assert) {
258 }, function(err, resp, body) {
259 assert.equal(resp.url, '/some-test')
260 assert.equal(resp.rawRequest.headers.foo, 'bar')
265 test("XHR can be overridden", function(assert) {
267 var noop = function() {}
268 var fakeXHR = function() {
270 this.open = this.send = noop
273 var fakeXDR = function() {
275 this.open = this.send = noop
277 xhr.XMLHttpRequest = fakeXHR
278 xhr({}, function() {})
279 assert.equal(xhrs, 1, "created the custom XHR")
281 xhr.XDomainRequest = fakeXDR
285 assert.equal(xdrs, 1, "created the custom XDR")