Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle application/x-www-form-urlencoded/Buffer #374

Merged
merged 3 commits into from
Mar 1, 2021

Conversation

bcoe
Copy link
Contributor

@bcoe bcoe commented Feb 26, 2021

Rather than switching node-gtoken to serializing its body with urlencoding, add support for application/x-www-form-urlencoded to gaxios.

This PR also corrects our handling of Buffer (otherwise allowing users to set a content-type header would not have corrected their issue).

Refs: googleapis/node-gtoken#362
Refs: googleapis/node-gtoken#363
Refs: googleapis/google-api-nodejs-client#2003

@bcoe bcoe requested a review from a team as a code owner February 26, 2021 19:02
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Feb 26, 2021
src/gaxios.ts Outdated
return typeof Buffer !== 'undefined';
}

function hasHeader(options: GaxiosOptions, headerCheck: string) {
Copy link
Contributor Author

@bcoe bcoe Feb 26, 2021

Choose a reason for hiding this comment

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

Support content-type, and Content-Type, and content-Type, when deciding whether to reset header.

src/gaxios.ts Outdated
!opts.headers['Content-Type'] ||
!opts.headers['Content-Type'].includes('json')
opts.headers['Content-Type'] &&
opts.headers['Content-Type'] === 'application/x-www-form-urlencoded'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If someone has explicitly asked for application/x-www-form-urlencoded serialize the body as a query strong.

const body = {hello: '🌎'};
const scope = nock(url)
.matchHeader('Content-Type', 'application/json')
.post('/', JSON.stringify(body))
.matchHeader('Content-Type', 'application/x-www-form-urlencoded')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test was for the old behavior where application/x-www-form-urlencoded got magically rewritten as application/json.

const pkgJson = JSON.parse(pkg.toString('utf8'));
const scope = nock(url)
.matchHeader('content-type', 'application/dicom')
.post('/', pkgJson)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

once in transport, the Buffer is properly handled by node-fetch, and becomes a string body.

Copy link
Contributor

Choose a reason for hiding this comment

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

sick

const pkg = fs.readFileSync('./package.json');
const pkgJson = JSON.parse(pkg.toString('utf8'));
const scope = nock(url)
.matchHeader('content-type', 'application/json')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default to application/json if we don't have a hint.

@codecov
Copy link

codecov bot commented Feb 26, 2021

Codecov Report

Merging #374 (f123899) into master (16384ce) will decrease coverage by 0.02%.
The diff coverage is 97.05%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #374      +/-   ##
==========================================
- Coverage   96.31%   96.28%   -0.03%     
==========================================
  Files           6        6              
  Lines         732      754      +22     
  Branches      106      114       +8     
==========================================
+ Hits          705      726      +21     
  Misses         27       27              
- Partials        0        1       +1     
Impacted Files Coverage Δ
src/gaxios.ts 98.46% <97.05%> (-0.22%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 16384ce...f123899. Read the comment docs.

src/gaxios.ts Show resolved Hide resolved
src/gaxios.ts Outdated Show resolved Hide resolved
src/gaxios.ts Outdated Show resolved Hide resolved
src/gaxios.ts Show resolved Hide resolved
test/test.getch.ts Outdated Show resolved Hide resolved
const pkgJson = JSON.parse(pkg.toString('utf8'));
const scope = nock(url)
.matchHeader('content-type', 'application/dicom')
.post('/', pkgJson)
Copy link
Contributor

Choose a reason for hiding this comment

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

sick

test/test.getch.ts Outdated Show resolved Hide resolved
// Do not attempt to JSON.stringify() a Buffer:
opts.body = opts.data;
if (!hasHeader(opts, 'Content-Type')) {
opts.headers['Content-Type'] = 'application/json';
Copy link
Contributor

@JustinBeckwith JustinBeckwith Feb 26, 2021

Choose a reason for hiding this comment

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

this also should be getHeader to ensure case safety. I am starting to wonder if we should lowercase all header names in validateOptions, and then always use lowercase throughout this code. Eh? (don't feel like you need to go that far right now)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't need to be, because we already know it doesn't have the header Content-Type, or content-type. So, we're just picking one when we choose it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, for clarification hasHeader doesn't care about case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants