Skip to content

Commit

Permalink
feat: add the maxRedirects option (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Feb 27, 2019
1 parent 7aa6f74 commit 9c34810
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ gaxios.request({url: '/data'}).then(...);
// Defaults to `0`, which is the same as unset.
maxContentLength: 2000,

// The max number of HTTP redirects to follow.
// Defaults to 100.
maxRedirects: 100,

// The querystring parameters that will be encoded using `qs` and
// appended to the url
params: {
Expand Down
5 changes: 5 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export interface GaxiosOptions {
* The maximum size of the http response content in bytes allowed.
*/
maxContentLength?: number;
/**
* The maximum number of redirects to follow. Defaults to 20.
*/
maxRedirects?: number;
follow?: number;
params?: any;
paramsSerializer?: (params: {[index: string]: string|number}) => string;
timeout?: number;
Expand Down
6 changes: 5 additions & 1 deletion src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class Gaxios {
}

/**
* Validate the options, and massage them to match the fetch format.
* Validates the options, and merges them with defaults.
* @param opts The original options passed from the client.
*/
private validateOpts(options: GaxiosOptions): GaxiosOptions {
Expand All @@ -130,6 +130,10 @@ export class Gaxios {
opts.size = options.maxContentLength;
}

if (typeof options.maxRedirects === 'number') {
opts.follow = options.maxRedirects;
}

opts.headers = opts.headers || {};
if (opts.data) {
if (this.isReadableStream(opts.data)) {
Expand Down
20 changes: 19 additions & 1 deletion test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,25 @@ describe('🥁 configuration options', () => {
const body = {hello: '🌎'};
const scope = nock(url).get('/').reply(200, body);
const maxContentLength = 1;
assertRejects(request({url, maxContentLength}), /over limit/);
await assertRejects(request({url, maxContentLength}), /over limit/);
scope.done();
});

it('should support redirects by default', async () => {
const body = {hello: '🌎'};
const scopes = [
nock(url).get('/foo').reply(200, body),
nock(url).get('/').reply(302, null, {location: '/foo'})
];
const res = await request({url});
scopes.forEach(x => x.done());
assert.deepStrictEqual(res.data, body);
});

it('should support disabling redirects', async () => {
const scope = nock(url).get('/').reply(302, null, {location: '/foo'});
const maxRedirects = 0;
await assertRejects(request({url, maxRedirects}), /maximum redirect/);
scope.done();
});

Expand Down

0 comments on commit 9c34810

Please sign in to comment.