Skip to content

Commit 3314ec5

Browse files
author
James Halliday
committed
Merge branch 'xhr2-send-support' of https://github.com/lakenen/http-browserify
2 parents ddeabab + 9fd4928 commit 3314ec5

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
@@ -142,6 +142,9 @@ Request.prototype.end = function (s) {
142142
}
143143
this.xhr.send(body);
144144
}
145+
else if (isXHR2Compatible(this.body[0])) {
146+
this.xhr.send(this.body[0]);
147+
}
145148
else {
146149
var body = '';
147150
for (var i = 0; i < this.body.length; i++) {
@@ -198,3 +201,9 @@ var indexOf = function (xs, x) {
198201
}
199202
return -1;
200203
};
204+
205+
var isXHR2Compatible = function (obj) {
206+
return obj instanceof window.Blob
207+
|| obj instanceof window.ArrayBuffer
208+
|| obj instanceof window.FormData;
209+
};

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

@@ -86,3 +90,26 @@ test('Test withCredentials param', function(t) {
8690

8791
t.end();
8892
});
93+
94+
test('Test POST XHR2 types', function(t) {
95+
t.plan(3);
96+
var url = '/api/foo';
97+
98+
var request = http.request({ url: url, method: 'POST' }, noop);
99+
request.xhr.send = function (data) {
100+
t.ok(data instanceof global.window.ArrayBuffer, 'data should be instanceof ArrayBuffer');
101+
};
102+
request.end(new global.window.ArrayBuffer());
103+
104+
request = http.request({ url: url, method: 'POST' }, noop);
105+
request.xhr.send = function (data) {
106+
t.ok(data instanceof global.window.Blob, 'data should be instanceof Blob');
107+
};
108+
request.end(new global.window.Blob());
109+
110+
request = http.request({ url: url, method: 'POST' }, noop);
111+
request.xhr.send = function (data) {
112+
t.ok(data instanceof global.window.FormData, 'data should be instanceof FormData');
113+
};
114+
request.end(new global.window.FormData());
115+
});

0 commit comments

Comments
 (0)