Skip to content
Browse files

Allow custom xhr factory similar to jQuery.

  • Loading branch information...
1 parent c8ba9c6 commit dd3cd58655d1d229e82e3c2b5178ad8fed579072 @satazor satazor committed
Showing with 46 additions and 1 deletion.
  1. +4 −1 src/reqwest.js
  2. +42 −0 tests/tests.js
View
5 src/reqwest.js
@@ -189,7 +189,10 @@
if (o['type'] == 'jsonp') return handleJsonp(o, fn, err, url)
- http = xhr(o)
+ // get the xhr from the factory if passed
+ // if the factory returns null, fall-back to ours
+ http = (o.xhr && o.xhr(o)) || xhr(o);
+
http.open(method, url, o['async'] === false ? false : true)
setHeaders(http, o)
setCredentials(http, o)
View
42 tests/tests.js
@@ -512,6 +512,48 @@
sink('Connection Object', function (test, ok) {
+ test('use xhr factory provided in the options', function (complete) {
+ var reqwest
+ , xhr
+
+ if (typeof XMLHttpRequest !== 'undefined') {
+ xhr = new XMLHttpRequest()
+ } else if (typeof ActiveXObject !== 'undefined') {
+ xhr = new ActiveXObject('Microsoft.XMLHTTP')
+ } else {
+ ok(false, 'browser not supported')
+ }
+
+ reqwest = ajax({
+ url: '/tests/fixtures/fixtures.html',
+ xhr: function () {
+ return xhr
+ }
+ })
+
+ ok(reqwest.request === xhr, 'uses factory')
+ complete()
+ })
+
+ test('fallbacks to own xhr factory if falsy is returned', function (complete) {
+ var reqwest
+
+ FakeXHR.setup()
+ try {
+ reqwest = ajax({
+ url: '/tests/fixtures/fixtures.html',
+ xhr: function () {
+ return null
+ }
+ })
+
+ ok(reqwest.request instanceof FakeXHR, 'fallbacks correctly')
+ complete()
+ } finally {
+ FakeXHR.restore()
+ }
+ })
+
test('setRequestHeaders', function (complete) {
ajax({
url: '/tests/fixtures/fixtures.html'

0 comments on commit dd3cd58

Please sign in to comment.
Something went wrong with that request. Please try again.