@@ -730,4 +730,92 @@ describe('cache-control tests', () => {
730730 expect ( request10 . status ) . toBe ( 304 ) ;
731731 expect ( request10 . text ) . toBe ( '' ) ;
732732 } ) ;
733+
734+ test ( 'principal mempool cache control' , async ( ) => {
735+ const sender_address = 'SP3FXEKSA6D4BW3TFP2BWTSREV6FY863Y90YY7D8G' ;
736+ const url = `/extended/v1/address/${ sender_address } /mempool` ;
737+ await db . update (
738+ new TestBlockBuilder ( {
739+ block_height : 1 ,
740+ index_block_hash : '0x01' ,
741+ parent_index_block_hash : '0x00' ,
742+ } ) . build ( )
743+ ) ;
744+
745+ // ETag zero.
746+ const request1 = await supertest ( api . server ) . get ( url ) ;
747+ expect ( request1 . status ) . toBe ( 200 ) ;
748+ expect ( request1 . type ) . toBe ( 'application/json' ) ;
749+ const etag0 = request1 . headers [ 'etag' ] ;
750+
751+ // Add STX tx.
752+ await db . updateMempoolTxs ( {
753+ mempoolTxs : [ testMempoolTx ( { tx_id : '0x0001' , receipt_time : 1000 , sender_address } ) ] ,
754+ } ) ;
755+
756+ // Valid ETag.
757+ const request2 = await supertest ( api . server ) . get ( url ) ;
758+ expect ( request2 . status ) . toBe ( 200 ) ;
759+ expect ( request2 . type ) . toBe ( 'application/json' ) ;
760+ expect ( request2 . headers [ 'etag' ] ) . toBeTruthy ( ) ;
761+ const etag1 = request2 . headers [ 'etag' ] ;
762+ expect ( etag1 ) . not . toEqual ( etag0 ) ;
763+
764+ // Cache works with valid ETag.
765+ const request3 = await supertest ( api . server ) . get ( url ) . set ( 'If-None-Match' , etag1 ) ;
766+ expect ( request3 . status ) . toBe ( 304 ) ;
767+ expect ( request3 . text ) . toBe ( '' ) ;
768+
769+ // Add sponsor tx.
770+ await db . updateMempoolTxs ( {
771+ mempoolTxs : [
772+ testMempoolTx ( { tx_id : '0x0002' , receipt_time : 2000 , sponsor_address : sender_address } ) ,
773+ ] ,
774+ } ) ;
775+
776+ // Cache is now a miss.
777+ const request4 = await supertest ( api . server ) . get ( url ) . set ( 'If-None-Match' , etag1 ) ;
778+ expect ( request4 . status ) . toBe ( 200 ) ;
779+ expect ( request4 . type ) . toBe ( 'application/json' ) ;
780+ expect ( request4 . headers [ 'etag' ] ) . not . toEqual ( etag1 ) ;
781+ const etag2 = request4 . headers [ 'etag' ] ;
782+
783+ // Cache works with new ETag.
784+ const request5 = await supertest ( api . server ) . get ( url ) . set ( 'If-None-Match' , etag2 ) ;
785+ expect ( request5 . status ) . toBe ( 304 ) ;
786+ expect ( request5 . text ) . toBe ( '' ) ;
787+
788+ // Add token recipient tx.
789+ await db . updateMempoolTxs ( {
790+ mempoolTxs : [
791+ testMempoolTx ( {
792+ tx_id : '0x0003' ,
793+ receipt_time : 3000 ,
794+ token_transfer_recipient_address : sender_address ,
795+ } ) ,
796+ ] ,
797+ } ) ;
798+
799+ // Cache is now a miss.
800+ const request6 = await supertest ( api . server ) . get ( url ) . set ( 'If-None-Match' , etag2 ) ;
801+ expect ( request6 . status ) . toBe ( 200 ) ;
802+ expect ( request6 . type ) . toBe ( 'application/json' ) ;
803+ expect ( request6 . headers [ 'etag' ] ) . not . toEqual ( etag2 ) ;
804+ const etag3 = request6 . headers [ 'etag' ] ;
805+
806+ // Cache works with new ETag.
807+ const request7 = await supertest ( api . server ) . get ( url ) . set ( 'If-None-Match' , etag3 ) ;
808+ expect ( request7 . status ) . toBe ( 304 ) ;
809+ expect ( request7 . text ) . toBe ( '' ) ;
810+
811+ // Change mempool with no changes to this address.
812+ await db . updateMempoolTxs ( {
813+ mempoolTxs : [ testMempoolTx ( { tx_id : '0x0004' , receipt_time : 4000 } ) ] ,
814+ } ) ;
815+
816+ // Cache still works.
817+ const request8 = await supertest ( api . server ) . get ( url ) . set ( 'If-None-Match' , etag3 ) ;
818+ expect ( request8 . status ) . toBe ( 304 ) ;
819+ expect ( request8 . text ) . toBe ( '' ) ;
820+ } ) ;
733821} ) ;
0 commit comments