mirror of
https://github.com/tenrok/axios.git
synced 2026-06-23 20:40:40 +03:00
Add independent maxBodyLength option (#2781)
* Add independent option to set the maximum size of the request body * Remove maxBodyLength check * Update README * Assert for error code and message
This commit is contained in:
@@ -361,6 +361,9 @@ These are the available config options for making requests. Only the `url` is re
|
|||||||
// `maxContentLength` defines the max size of the http response content in bytes allowed
|
// `maxContentLength` defines the max size of the http response content in bytes allowed
|
||||||
maxContentLength: 2000,
|
maxContentLength: 2000,
|
||||||
|
|
||||||
|
// `maxBodyLength` (Node only option) defines the max size of the http request content in bytes allowed
|
||||||
|
maxBodyLength: 2000,
|
||||||
|
|
||||||
// `validateStatus` defines whether to resolve or reject the promise for a given
|
// `validateStatus` defines whether to resolve or reject the promise for a given
|
||||||
// HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
|
// HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
|
||||||
// or `undefined`), the promise will be resolved; otherwise, the promise will be
|
// or `undefined`), the promise will be resolved; otherwise, the promise will be
|
||||||
|
|||||||
Vendored
+7
-6
@@ -32,12 +32,12 @@ export type Method =
|
|||||||
| 'link' | 'LINK'
|
| 'link' | 'LINK'
|
||||||
| 'unlink' | 'UNLINK'
|
| 'unlink' | 'UNLINK'
|
||||||
|
|
||||||
export type ResponseType =
|
export type ResponseType =
|
||||||
| 'arraybuffer'
|
| 'arraybuffer'
|
||||||
| 'blob'
|
| 'blob'
|
||||||
| 'document'
|
| 'document'
|
||||||
| 'json'
|
| 'json'
|
||||||
| 'text'
|
| 'text'
|
||||||
| 'stream'
|
| 'stream'
|
||||||
|
|
||||||
export interface AxiosRequestConfig {
|
export interface AxiosRequestConfig {
|
||||||
@@ -61,6 +61,7 @@ export interface AxiosRequestConfig {
|
|||||||
onUploadProgress?: (progressEvent: any) => void;
|
onUploadProgress?: (progressEvent: any) => void;
|
||||||
onDownloadProgress?: (progressEvent: any) => void;
|
onDownloadProgress?: (progressEvent: any) => void;
|
||||||
maxContentLength?: number;
|
maxContentLength?: number;
|
||||||
|
maxBodyLength?: number;
|
||||||
validateStatus?: (status: number) => boolean;
|
validateStatus?: (status: number) => boolean;
|
||||||
maxRedirects?: number;
|
maxRedirects?: number;
|
||||||
socketPath?: string | null;
|
socketPath?: string | null;
|
||||||
|
|||||||
@@ -171,8 +171,8 @@ module.exports = function httpAdapter(config) {
|
|||||||
transport = isHttpsProxy ? httpsFollow : httpFollow;
|
transport = isHttpsProxy ? httpsFollow : httpFollow;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.maxContentLength && config.maxContentLength > -1) {
|
if (config.maxBodyLength > -1) {
|
||||||
options.maxBodyLength = config.maxContentLength;
|
options.maxBodyLength = config.maxBodyLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the request
|
// Create the request
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ module.exports = function mergeConfig(config1, config2) {
|
|||||||
'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer',
|
'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer',
|
||||||
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
|
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
|
||||||
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress',
|
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress',
|
||||||
'maxContentLength', 'validateStatus', 'maxRedirects', 'httpAgent',
|
'maxContentLength', 'maxBodyLength', 'validateStatus', 'maxRedirects', 'httpAgent',
|
||||||
'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding'
|
'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ var defaults = {
|
|||||||
xsrfHeaderName: 'X-XSRF-TOKEN',
|
xsrfHeaderName: 'X-XSRF-TOKEN',
|
||||||
|
|
||||||
maxContentLength: -1,
|
maxContentLength: -1,
|
||||||
|
maxBodyLength: -1,
|
||||||
|
|
||||||
validateStatus: function validateStatus(status) {
|
validateStatus: function validateStatus(status) {
|
||||||
return status >= 200 && status < 300;
|
return status >= 200 && status < 300;
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ const config: AxiosRequestConfig = {
|
|||||||
onUploadProgress: (progressEvent: any) => {},
|
onUploadProgress: (progressEvent: any) => {},
|
||||||
onDownloadProgress: (progressEvent: any) => {},
|
onDownloadProgress: (progressEvent: any) => {},
|
||||||
maxContentLength: 2000,
|
maxContentLength: 2000,
|
||||||
|
maxBodyLength: 2000,
|
||||||
validateStatus: (status: number) => status >= 200 && status < 300,
|
validateStatus: (status: number) => status >= 200 && status < 300,
|
||||||
maxRedirects: 5,
|
maxRedirects: 5,
|
||||||
proxy: {
|
proxy: {
|
||||||
|
|||||||
@@ -296,6 +296,37 @@ describe('supports http with nodejs', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should support max body length', function (done) {
|
||||||
|
var data = Array(100000).join('ж');
|
||||||
|
|
||||||
|
server = http.createServer(function (req, res) {
|
||||||
|
res.setHeader('Content-Type', 'text/html; charset=UTF-8');
|
||||||
|
res.end();
|
||||||
|
}).listen(4444, function () {
|
||||||
|
var success = false, failure = false, error;
|
||||||
|
|
||||||
|
axios.post('http://localhost:4444/', {
|
||||||
|
data: data
|
||||||
|
}, {
|
||||||
|
maxBodyLength: 2000
|
||||||
|
}).then(function (res) {
|
||||||
|
success = true;
|
||||||
|
}).catch(function (err) {
|
||||||
|
error = err;
|
||||||
|
failure = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
setTimeout(function () {
|
||||||
|
assert.equal(success, false, 'request should not succeed');
|
||||||
|
assert.equal(failure, true, 'request should fail');
|
||||||
|
assert.equal(error.code, 'ERR_FR_MAX_BODY_LENGTH_EXCEEDED');
|
||||||
|
assert.equal(error.message, 'Request body larger than maxBodyLength limit');
|
||||||
|
done();
|
||||||
|
}, 100);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it.skip('should support sockets', function (done) {
|
it.skip('should support sockets', function (done) {
|
||||||
server = net.createServer(function (socket) {
|
server = net.createServer(function (socket) {
|
||||||
socket.on('data', function () {
|
socket.on('data', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user