Skip to content

Commit

Permalink
fix: use deep merge for headers (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Jun 14, 2019
1 parent 916d21d commit 404fd19
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@compodoc/compodoc": "^1.1.9",
"@types/chai": "^4.1.7",
"@types/execa": "^0.9.0",
"@types/extend": "^3.0.1",
"@types/mocha": "^5.2.7",
"@types/mv": "^2.1.0",
"@types/ncp": "^2.0.1",
Expand Down Expand Up @@ -71,6 +72,7 @@
"webpack-cli": "^3.3.4"
},
"dependencies": {
"extend": "^3.0.2",
"gaxios": "^2.0.1",
"google-auth-library": "^4.2.0",
"qs": "^6.7.0",
Expand Down
6 changes: 4 additions & 2 deletions src/apirequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as qs from 'qs';
import * as stream from 'stream';
import * as urlTemplate from 'url-template';
import * as uuid from 'uuid';
import * as extend from 'extend';

import {APIRequestParams, BodyResponseCallback} from './api';
import {isBrowser} from './isbrowser';
Expand Down Expand Up @@ -225,7 +226,7 @@ async function createAPIRequestAsync<T>(parameters: APIRequestParams) {
options.data = resource || undefined;
}

options.headers = headers;
options.headers = extend(true, options.headers || {}, headers);
options.params = params;
if (!isBrowser()) {
options.headers!['Accept-Encoding'] = 'gzip';
Expand Down Expand Up @@ -262,7 +263,8 @@ async function createAPIRequestAsync<T>(parameters: APIRequestParams) {
// Combine the GaxiosOptions options passed with this specific
// API call witht the global options configured at the API Context
// level, or at the global level.
const mergedOptions = Object.assign(
const mergedOptions = extend(
true,
{},
parameters.context.google ? parameters.context.google._options : {},
parameters.context._options,
Expand Down
30 changes: 30 additions & 0 deletions test/test.apirequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,35 @@ describe('createAPIRequest', () => {
});
scope.done();
});

it('should merge headers from global and local config', async () => {
const scope = nock(url)
.get('/')
.reply(200);
const res = await createAPIRequest<FakeParams>({
options: {
url,
headers: {
'Local-Header': 'local',
},
},
params: {},
requiredParams: [],
pathParams: [],
context: {
_options: {},
google: {
_options: {
headers: {
'Global-Header': 'global',
},
},
},
},
});
scope.done();
assert.strictEqual(res.config.headers!['Global-Header'], 'global');
assert.strictEqual(res.config.headers!['Local-Header'], 'local');
});
});
});

0 comments on commit 404fd19

Please sign in to comment.