Skip to content

Commit

Permalink
fix: fixed BigInt sent as json
Browse files Browse the repository at this point in the history
  • Loading branch information
NikoRaisanen committed Jul 26, 2023
1 parent 73c7efb commit 259a43f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/request-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,12 @@ RequestBase.prototype.send = function (data) {
// merge
if (isObject_ && isObject(this._data)) {
for (const key in data) {
if (typeof data[key] == "bigint") throw new Error("Cannot serialize BigInt value to json");
if (hasOwn(data, key)) this._data[key] = data[key];
}
} else if (typeof data === 'string') {
}
else if (typeof data === 'bigint') throw new Error("Cannot send value of type BigInt");
else if (typeof data === 'string') {
// default to x-www-form-urlencoded
if (!type) this.type('form');
type = this._header['content-type'];
Expand Down
26 changes: 26 additions & 0 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ describe('req.send(Object) as "json"', function () {
});
});

it('should error for BigInt object', (done) => {
try {
request
.post(`${uri}/echo`)
.type('json')
.send({number: 1n})
throw new Error('Should have thrown error for object with BigInt')
} catch (error) {
assert.strictEqual(error.message, 'Cannot serialize BigInt value to json');
}
done();
});

it('should error for BigInt primitive', (done) => {
try {
request
.post(`${uri}/echo`)
.type('json')
.send(1n)
throw new Error('Should have thrown error for BigInt primitive')
} catch (error) {
assert.strictEqual(error.message, 'Cannot send value of type BigInt');
}
done();
});

describe('when called several times', () => {
it('should merge the objects', (done) => {
request
Expand Down

0 comments on commit 259a43f

Please sign in to comment.