Skip to content

Commit

Permalink
fix: do not override content-type if its given (#158)
Browse files Browse the repository at this point in the history
* fix: do not override content-type if its given

* gts fix
  • Loading branch information
alexander-fenster authored Aug 26, 2019
1 parent 5182075 commit f49e0e6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/gaxios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,13 @@ export class Gaxios {
opts.body = opts.data;
} else if (typeof opts.data === 'object') {
opts.body = JSON.stringify(opts.data);
opts.headers['Content-Type'] = 'application/json';
if (
!Object.keys(opts.headers).some((key: string) =>
key.match(/^content-type$/i)
)
) {
opts.headers['Content-Type'] = 'application/json';
}
} else {
opts.body = opts.data;
}
Expand Down
31 changes: 31 additions & 0 deletions test/test.getch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,37 @@ describe('🎏 data handling', () => {
assert.deepStrictEqual(res.data, {});
});

it('should set content-type for object request', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.matchHeader('content-type', 'application/json')
.post('/', JSON.stringify(body))
.reply(200, {});
const res = await request({
url,
method: 'POST',
data: body,
});
scope.done();
assert.deepStrictEqual(res.data, {});
});

it('should allow to override content-type for object request', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
.matchHeader('content-type', 'application/octet-stream')
.post('/', JSON.stringify(body))
.reply(200, {});
const res = await request({
url,
method: 'POST',
data: body,
headers: {'content-type': 'application/octet-stream'},
});
scope.done();
assert.deepStrictEqual(res.data, {});
});

it('should return stream if asked nicely', async () => {
const body = {hello: '🌎'};
const scope = nock(url)
Expand Down

0 comments on commit f49e0e6

Please sign in to comment.