Skip to content

Commit

Permalink
Add independent maxBodyLength option (axios#2781)
Browse files Browse the repository at this point in the history
* Add independent option to set the maximum size of the request body

* Remove maxBodyLength check

* Update README

* Assert for error code and message
  • Loading branch information
gualopezb authored Mar 6, 2020
1 parent 5214445 commit 6642ca9
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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
// 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
Expand Down
13 changes: 7 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export type Method =
| 'link' | 'LINK'
| 'unlink' | 'UNLINK'

export type ResponseType =
| 'arraybuffer'
| 'blob'
| 'document'
| 'json'
| 'text'
export type ResponseType =
| 'arraybuffer'
| 'blob'
| 'document'
| 'json'
| 'text'
| 'stream'

export interface AxiosRequestConfig {
Expand All @@ -61,6 +61,7 @@ export interface AxiosRequestConfig {
onUploadProgress?: (progressEvent: any) => void;
onDownloadProgress?: (progressEvent: any) => void;
maxContentLength?: number;
maxBodyLength?: number;
validateStatus?: (status: number) => boolean;
maxRedirects?: number;
socketPath?: string | null;
Expand Down
4 changes: 2 additions & 2 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ module.exports = function httpAdapter(config) {
transport = isHttpsProxy ? httpsFollow : httpFollow;
}

if (config.maxContentLength && config.maxContentLength > -1) {
options.maxBodyLength = config.maxContentLength;
if (config.maxBodyLength > -1) {
options.maxBodyLength = config.maxBodyLength;
}

// Create the request
Expand Down
2 changes: 1 addition & 1 deletion lib/core/mergeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function mergeConfig(config1, config2) {
'baseURL', 'url', 'transformRequest', 'transformResponse', 'paramsSerializer',
'timeout', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress',
'maxContentLength', 'validateStatus', 'maxRedirects', 'httpAgent',
'maxContentLength', 'maxBodyLength', 'validateStatus', 'maxRedirects', 'httpAgent',
'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding'
];

Expand Down
1 change: 1 addition & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var defaults = {
xsrfHeaderName: 'X-XSRF-TOKEN',

maxContentLength: -1,
maxBodyLength: -1,

validateStatus: function validateStatus(status) {
return status >= 200 && status < 300;
Expand Down
1 change: 1 addition & 0 deletions test/typescript/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const config: AxiosRequestConfig = {
onUploadProgress: (progressEvent: any) => {},
onDownloadProgress: (progressEvent: any) => {},
maxContentLength: 2000,
maxBodyLength: 2000,
validateStatus: (status: number) => status >= 200 && status < 300,
maxRedirects: 5,
proxy: {
Expand Down
31 changes: 31 additions & 0 deletions test/unit/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
server = net.createServer(function (socket) {
socket.on('data', function () {
Expand Down

0 comments on commit 6642ca9

Please sign in to comment.