Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions core/packages/gaxios/src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,12 +478,12 @@ export class Gaxios implements FetchCompliance {
}
}

if (typeof options.maxContentLength === 'number') {
opts.size = options.maxContentLength;
if (typeof opts.maxContentLength === 'number') {
opts.size = opts.maxContentLength;
}

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

const shouldDirectlyPassData =
Expand Down
10 changes: 9 additions & 1 deletion core/packages/gaxios/test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ describe('🥁 configuration options', () => {
scope.done();
});

it('should enforce maxContentLength on responses without a Content-Length header', async () => {
it('should enforce maxContentLength on responses without a Content-Length header and from instance defaults', async () => {
const body = {hello: 'world'};
for (const responseType of [
'json',
Expand All @@ -358,6 +358,14 @@ describe('🥁 configuration options', () => {
);
scope.done();
}

const defaultScope = nock(url).get('/').reply(200, body);
const client = new Gaxios({maxContentLength: 5});
await assert.rejects(
client.request({url}),
(err: Error) => err instanceof GaxiosError && /limit/.test(err.message),
);
defaultScope.done();
Comment on lines +363 to +368

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The pull request fixes both maxContentLength and maxRedirects instance defaults, but the added test only covers maxContentLength. It would be great to also add a test case verifying that maxRedirects instance defaults are correctly honored and enforced.

    const defaultScope = nock(url).get('/').reply(200, body);
    const client = new Gaxios({maxContentLength: 5});
    await assert.rejects(
      client.request({url}),
      (err: Error) => err instanceof GaxiosError && /limit/.test(err.message),
    );
    defaultScope.done();

    const redirectScope = nock(url)
      .get('/')
      .reply(302, undefined, {Location: '/redirect'})
      .get('/redirect')
      .reply(200, 'done');
    const redirectClient = new Gaxios({maxRedirects: 0});
    await assert.rejects(
      redirectClient.request({url}),
      (err: Error) => err instanceof GaxiosError,
    );
    redirectScope.done();

});
Comment on lines +362 to 369

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The pull request description and title mention honoring both maxContentLength and maxRedirects instance defaults. However, the added unit test only covers maxContentLength. Adding a test case for maxRedirects ensures both options are fully verified and protected against future regressions.

Suggested change
const defaultScope = nock(url).get('/').reply(200, body);
const client = new Gaxios({maxContentLength: 5});
await assert.rejects(
client.request({url}),
(err: Error) => err instanceof GaxiosError && /limit/.test(err.message),
);
defaultScope.done();
});
const defaultScope = nock(url).get('/').reply(200, body);
const client = new Gaxios({maxContentLength: 5});
await assert.rejects(
client.request({url}),
(err: Error) => err instanceof GaxiosError && /limit/.test(err.message),
);
defaultScope.done();
});
it('should enforce maxRedirects from instance defaults', async () => {
const redirectScope = nock(url)
.get('/')
.reply(302, undefined, {Location: '/redirect'});
const client = new Gaxios({maxRedirects: 0});
await assert.rejects(
client.request({url}),
(err: Error) => err instanceof GaxiosError,
);
redirectScope.done();
});


it('should support redirects by default', async () => {
Expand Down
Loading