Skip to content

Commit 9fd4928

Browse files
author
Cameron Lakenen
committed
Add support for XHR2 send with Blob/ArrayBuffer/FormData (fixes #43)
1 parent c0869e5 commit 9fd4928

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lib/request.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ Request.prototype.end = function (s) {
134134
}
135135
this.xhr.send(body);
136136
}
137+
else if (isXHR2Compatible(this.body[0])) {
138+
this.xhr.send(this.body[0]);
139+
}
137140
else {
138141
var body = '';
139142
for (var i = 0; i < this.body.length; i++) {
@@ -190,3 +193,9 @@ var indexOf = function (xs, x) {
190193
}
191194
return -1;
192195
};
196+
197+
var isXHR2Compatible = function (obj) {
198+
return obj instanceof window.Blob
199+
|| obj instanceof window.ArrayBuffer
200+
|| obj instanceof window.FormData;
201+
};

test/request_url.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ global.window.XMLHttpRequest = function() {
1212
this.send = noop;
1313
};
1414

15+
global.window.FormData = function () {};
16+
global.window.Blob = function () {};
17+
global.window.ArrayBuffer = function () {};
18+
1519
var test = require('tape').test;
1620
var http = require('../index.js');
1721

@@ -72,3 +76,26 @@ test('Test withCredentials param', function(t) {
7276

7377
t.end();
7478
});
79+
80+
test('Test POST XHR2 types', function(t) {
81+
t.plan(3);
82+
var url = '/api/foo';
83+
84+
var request = http.request({ url: url, method: 'POST' }, noop);
85+
request.xhr.send = function (data) {
86+
t.ok(data instanceof global.window.ArrayBuffer, 'data should be instanceof ArrayBuffer');
87+
};
88+
request.end(new global.window.ArrayBuffer());
89+
90+
request = http.request({ url: url, method: 'POST' }, noop);
91+
request.xhr.send = function (data) {
92+
t.ok(data instanceof global.window.Blob, 'data should be instanceof Blob');
93+
};
94+
request.end(new global.window.Blob());
95+
96+
request = http.request({ url: url, method: 'POST' }, noop);
97+
request.xhr.send = function (data) {
98+
t.ok(data instanceof global.window.FormData, 'data should be instanceof FormData');
99+
};
100+
request.end(new global.window.FormData());
101+
});

0 commit comments

Comments
 (0)