提交 18833443 编写于 作者: J Jeremy Fowler 提交者: Rubén Norte

support node buffers less than 8192 bytes (#773)

上级 bbfbeff4
......@@ -30,13 +30,15 @@ module.exports = function httpAdapter(config) {
}
if (data && !utils.isStream(data)) {
if (utils.isArrayBuffer(data)) {
if (utils.isBuffer(data)) {
// Nothing to do...
} else if (utils.isArrayBuffer(data)) {
data = new Buffer(new Uint8Array(data));
} else if (utils.isString(data)) {
data = new Buffer(data, 'utf-8');
} else {
return reject(createError(
'Data after transformation must be a string, an ArrayBuffer, or a Stream',
'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
config
));
}
......
......@@ -32,6 +32,7 @@ var defaults = {
normalizeHeaderName(headers, 'Content-Type');
if (utils.isFormData(data) ||
utils.isArrayBuffer(data) ||
utils.isBuffer(data) ||
utils.isStream(data) ||
utils.isFile(data) ||
utils.isBlob(data)
......
......@@ -18,6 +18,16 @@ function isArray(val) {
return toString.call(val) === '[object Array]';
}
/**
* Determine if a value is a Node Buffer
*
* @param {Object} val The value to test
* @returns {boolean} True if value is a Node Buffer, otherwise false
*/
function isBuffer(val) {
return ((typeof Buffer !== 'undefined') && (Buffer.isBuffer) && (Buffer.isBuffer(val)));
}
/**
* Determine if a value is an ArrayBuffer
*
......@@ -281,6 +291,7 @@ function extend(a, b, thisArg) {
module.exports = {
isArray: isArray,
isArrayBuffer: isArrayBuffer,
isBuffer: isBuffer,
isFormData: isFormData,
isArrayBufferView: isArrayBufferView,
isString: isString,
......
......@@ -248,6 +248,30 @@ module.exports = {
});
},
testBuffer: function(test) {
var buf = new Buffer(1024); // Unsafe buffer < Buffer.poolSize (8192 bytes)
buf.fill('x');
server = http.createServer(function (req, res) {
test.equal(req.headers['content-length'], buf.length.toString());
req.pipe(res);
}).listen(4444, function () {
axios.post('http://localhost:4444/',
buf, {
responseType: 'stream'
}).then(function (res) {
var stream = res.data;
var string = '';
stream.on('data', function (chunk) {
string += chunk.toString('utf8');
});
stream.on('end', function () {
test.equal(string, buf.toString());
test.done();
});
});
});
},
testHTTPProxy: function(test) {
server = http.createServer(function(req, res) {
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册