@@ -214,7 +214,9 @@ describe('RpcService', () => {
214
214
method : 'eth_chainId' ,
215
215
params : [ ] ,
216
216
} ;
217
- await ignoreRejection ( service . request ( jsonRpcRequest ) ) ;
217
+ await expect ( service . request ( jsonRpcRequest ) ) . rejects . toThrow (
218
+ 'Nock: No match for request' ,
219
+ ) ;
218
220
expect ( failoverService . request ) . not . toHaveBeenCalled ( ) ;
219
221
} ) ;
220
222
@@ -236,13 +238,14 @@ describe('RpcService', () => {
236
238
} ) ;
237
239
service . onBreak ( onBreakListener ) ;
238
240
239
- const promise = service . request ( {
240
- id : 1 ,
241
- jsonrpc : '2.0' ,
242
- method : 'eth_chainId' ,
243
- params : [ ] ,
244
- } ) ;
245
- await ignoreRejection ( promise ) ;
241
+ await expect (
242
+ service . request ( {
243
+ id : 1 ,
244
+ jsonrpc : '2.0' ,
245
+ method : 'eth_chainId' ,
246
+ params : [ ] ,
247
+ } ) ,
248
+ ) . rejects . toThrow ( 'Nock: No match for request' ) ;
246
249
expect ( onBreakListener ) . not . toHaveBeenCalled ( ) ;
247
250
} ) ;
248
251
} ) ;
@@ -331,7 +334,7 @@ describe('RpcService', () => {
331
334
) ;
332
335
333
336
describe ( 'if the endpoint has a 405 response' , ( ) => {
334
- it ( 'throws a non-existent method error without retrying the request ' , async ( ) => {
337
+ it ( 'returns a method not supported error response for 405 status ' , async ( ) => {
335
338
const endpointUrl = 'https://rpc.example.chain' ;
336
339
nock ( endpointUrl )
337
340
. post ( '/' , {
@@ -347,15 +350,21 @@ describe('RpcService', () => {
347
350
endpointUrl,
348
351
} ) ;
349
352
350
- const promise = service . request ( {
353
+ const response = await service . request ( {
351
354
id : 1 ,
352
355
jsonrpc : '2.0' ,
353
356
method : 'eth_unknownMethod' ,
354
357
params : [ ] ,
355
358
} ) ;
356
- await expect ( promise ) . rejects . toThrow (
357
- 'The method does not exist / is not available.' ,
358
- ) ;
359
+
360
+ expect ( response ) . toStrictEqual ( {
361
+ id : 1 ,
362
+ jsonrpc : '2.0' ,
363
+ error : {
364
+ code : - 32601 ,
365
+ message : 'The method does not exist / is not available.' ,
366
+ } ,
367
+ } ) ;
359
368
} ) ;
360
369
361
370
it ( 'does not forward the request to a failover service if given one' , async ( ) => {
@@ -382,7 +391,7 @@ describe('RpcService', () => {
382
391
method : 'eth_unknownMethod' ,
383
392
params : [ ] ,
384
393
} ;
385
- await ignoreRejection ( service . request ( jsonRpcRequest ) ) ;
394
+ await service . request ( jsonRpcRequest ) ;
386
395
expect ( failoverService . request ) . not . toHaveBeenCalled ( ) ;
387
396
} ) ;
388
397
@@ -404,19 +413,18 @@ describe('RpcService', () => {
404
413
} ) ;
405
414
service . onBreak ( onBreakListener ) ;
406
415
407
- const promise = service . request ( {
416
+ await service . request ( {
408
417
id : 1 ,
409
418
jsonrpc : '2.0' ,
410
419
method : 'eth_unknownMethod' ,
411
420
params : [ ] ,
412
421
} ) ;
413
- await ignoreRejection ( promise ) ;
414
422
expect ( onBreakListener ) . not . toHaveBeenCalled ( ) ;
415
423
} ) ;
416
424
} ) ;
417
425
418
426
describe ( 'if the endpoint has a 429 response' , ( ) => {
419
- it ( 'throws a rate-limiting error without retrying the request ' , async ( ) => {
427
+ it ( 'returns a rate limit error response for 429 status ' , async ( ) => {
420
428
const endpointUrl = 'https://rpc.example.chain' ;
421
429
nock ( endpointUrl )
422
430
. post ( '/' , {
@@ -432,13 +440,62 @@ describe('RpcService', () => {
432
440
endpointUrl,
433
441
} ) ;
434
442
435
- const promise = service . request ( {
443
+ const response = await service . request ( {
444
+ id : 1 ,
445
+ jsonrpc : '2.0' ,
446
+ method : 'eth_chainId' ,
447
+ params : [ ] ,
448
+ } ) ;
449
+
450
+ expect ( response ) . toStrictEqual ( {
451
+ id : 1 ,
452
+ jsonrpc : '2.0' ,
453
+ error : {
454
+ code : - 32005 ,
455
+ message : 'Request is being rate limited.' ,
456
+ data : {
457
+ retryAfter : 1000 ,
458
+ } ,
459
+ } ,
460
+ } ) ;
461
+ } ) ;
462
+
463
+ it ( 'includes retry delay from Retry-After header when available' , async ( ) => {
464
+ const endpointUrl = 'https://rpc.example.chain' ;
465
+ nock ( endpointUrl )
466
+ . post ( '/' , {
467
+ id : 1 ,
468
+ jsonrpc : '2.0' ,
469
+ method : 'eth_chainId' ,
470
+ params : [ ] ,
471
+ } )
472
+ . reply ( 429 , '' , {
473
+ 'Retry-After' : '5' ,
474
+ } ) ;
475
+ const service = new RpcService ( {
476
+ fetch,
477
+ btoa,
478
+ endpointUrl,
479
+ } ) ;
480
+
481
+ const response = await service . request ( {
436
482
id : 1 ,
437
483
jsonrpc : '2.0' ,
438
484
method : 'eth_chainId' ,
439
485
params : [ ] ,
440
486
} ) ;
441
- await expect ( promise ) . rejects . toThrow ( 'Request is being rate limited.' ) ;
487
+
488
+ expect ( response ) . toStrictEqual ( {
489
+ id : 1 ,
490
+ jsonrpc : '2.0' ,
491
+ error : {
492
+ code : - 32005 ,
493
+ message : 'Request is being rate limited.' ,
494
+ data : {
495
+ retryAfter : 5000 ,
496
+ } ,
497
+ } ,
498
+ } ) ;
442
499
} ) ;
443
500
444
501
it ( 'does not forward the request to a failover service if given one' , async ( ) => {
@@ -465,7 +522,7 @@ describe('RpcService', () => {
465
522
method : 'eth_chainId' ,
466
523
params : [ ] ,
467
524
} ;
468
- await ignoreRejection ( service . request ( jsonRpcRequest ) ) ;
525
+ await service . request ( jsonRpcRequest ) ;
469
526
expect ( failoverService . request ) . not . toHaveBeenCalled ( ) ;
470
527
} ) ;
471
528
@@ -487,13 +544,12 @@ describe('RpcService', () => {
487
544
} ) ;
488
545
service . onBreak ( onBreakListener ) ;
489
546
490
- const promise = service . request ( {
547
+ await service . request ( {
491
548
id : 1 ,
492
549
jsonrpc : '2.0' ,
493
550
method : 'eth_chainId' ,
494
551
params : [ ] ,
495
552
} ) ;
496
- await ignoreRejection ( promise ) ;
497
553
expect ( onBreakListener ) . not . toHaveBeenCalled ( ) ;
498
554
} ) ;
499
555
} ) ;
0 commit comments