File tree 2 files changed +30
-1
lines changed
2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -662,9 +662,12 @@ RequestBase.prototype.send = function (data) {
662
662
// merge
663
663
if ( isObject_ && isObject ( this . _data ) ) {
664
664
for ( const key in data ) {
665
+ if ( typeof data [ key ] == "bigint" ) throw new Error ( "Cannot serialize BigInt value to json" ) ;
665
666
if ( hasOwn ( data , key ) ) this . _data [ key ] = data [ key ] ;
666
667
}
667
- } else if ( typeof data === 'string' ) {
668
+ }
669
+ else if ( typeof data === 'bigint' ) throw new Error ( "Cannot send value of type BigInt" ) ;
670
+ else if ( typeof data === 'string' ) {
668
671
// default to x-www-form-urlencoded
669
672
if ( ! type ) this . type ( 'form' ) ;
670
673
type = this . _header [ 'content-type' ] ;
Original file line number Diff line number Diff line change @@ -119,6 +119,32 @@ describe('req.send(Object) as "json"', function () {
119
119
} ) ;
120
120
} ) ;
121
121
122
+ it ( 'should error for BigInt object' , ( done ) => {
123
+ try {
124
+ request
125
+ . post ( `${ uri } /echo` )
126
+ . type ( 'json' )
127
+ . send ( { number : 1n } )
128
+ throw new Error ( 'Should have thrown error for object with BigInt' )
129
+ } catch ( error ) {
130
+ assert . strictEqual ( error . message , 'Cannot serialize BigInt value to json' ) ;
131
+ }
132
+ done ( ) ;
133
+ } ) ;
134
+
135
+ it ( 'should error for BigInt primitive' , ( done ) => {
136
+ try {
137
+ request
138
+ . post ( `${ uri } /echo` )
139
+ . type ( 'json' )
140
+ . send ( 1n )
141
+ throw new Error ( 'Should have thrown error for BigInt primitive' )
142
+ } catch ( error ) {
143
+ assert . strictEqual ( error . message , 'Cannot send value of type BigInt' ) ;
144
+ }
145
+ done ( ) ;
146
+ } ) ;
147
+
122
148
describe ( 'when called several times' , ( ) => {
123
149
it ( 'should merge the objects' , ( done ) => {
124
150
request
You can’t perform that action at this time.
0 commit comments