Skip to content
Open
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
18 changes: 18 additions & 0 deletions handwritten/storage/src/nodejs-common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,24 @@ export class Util {
delete reqOpts.json.autoPaginate;
delete reqOpts.json.autoPaginateVal;
reqOpts.json = replaceProjectIdToken(reqOpts.json, projectId);

const headers = reqOpts.headers || {};
if (
typeof (headers as any).set === 'function' &&
typeof (headers as any).has === 'function'
) {
if (!(headers as any).has('content-type')) {
(headers as any).set('Content-Type', 'application/json');
}
reqOpts.headers = headers;
} else {
const hasContentType = Object.keys(headers).some(
key => key.toLowerCase() === 'content-type'
);
reqOpts.headers = hasContentType
? headers
: { ...headers, 'Content-Type': 'application/json' };
}
}

reqOpts.uri = replaceProjectIdToken(reqOpts.uri, projectId);
Expand Down
58 changes: 58 additions & 0 deletions handwritten/storage/test/nodejs-common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,64 @@ describe('common/util', () => {
assert.deepStrictEqual(decoratedRequest.json, decoratedJson);
});

it('should set Content-Type header on plain headers object when json is set', () => {
const projectId = 'project-id';
const reqOpts = {
uri: 'http://',
json: {},
headers: {},
};
replaceProjectIdTokenOverride = (x: any) => x;

const decoratedRequest = util.decorateRequest(reqOpts, projectId);
assert.strictEqual(
(decoratedRequest.headers as any)['Content-Type'],
'application/json'
);
});

it('should set Content-Type header on Headers instance when json is set', () => {
if (typeof Headers === 'undefined') {
return;
}
const projectId = 'project-id';
const headersInstance = new Headers();
const reqOpts = {
uri: 'http://',
json: {},
headers: headersInstance,
};
replaceProjectIdTokenOverride = (x: any) => x;

const decoratedRequest = util.decorateRequest(reqOpts as any, projectId);
assert.strictEqual(
(decoratedRequest.headers as any).get('Content-Type'),
'application/json'
);
});
Comment thread
thiyaguk09 marked this conversation as resolved.

it('should not overwrite existing Content-Type header if already present', () => {
const projectId = 'project-id';
const reqOpts = {
uri: 'http://',
json: {},
headers: {
'content-type': 'application/x-protobuf',
},
};
replaceProjectIdTokenOverride = (x: any) => x;

const decoratedRequest = util.decorateRequest(reqOpts, projectId);
assert.strictEqual(
(decoratedRequest.headers as any)['content-type'],
'application/x-protobuf'
);
assert.strictEqual(
(decoratedRequest.headers as any)['Content-Type'],
undefined
);
});

it('should decorate the request', () => {
const projectId = 'project-id';
const reqOpts = {
Expand Down
Loading