@@ -74,6 +74,46 @@ describe('HMAC Utility Functions', () => {
7474 } )
7575 ) . to . equal ( expectedSubject ) ;
7676 } ) ;
77+
78+ it ( 'should return Buffer when text is Buffer (request)' , ( ) => {
79+ const expectedSubject = 'GET|1672531200000|3.0|/api/test?query=123|body-content' ;
80+ const result = calculateHMACSubject ( {
81+ urlPath : '/api/test?query=123' ,
82+ text : Buffer . from ( 'body-content' , 'utf8' ) ,
83+ timestamp : MOCK_TIMESTAMP ,
84+ method : 'get' ,
85+ authVersion : 3 ,
86+ } ) ;
87+ expect ( Buffer . isBuffer ( result ) ) . to . be . true ;
88+ expect ( result . toString ( 'utf8' ) ) . to . equal ( expectedSubject ) ;
89+ } ) ;
90+
91+ it ( 'should return Buffer when text is Buffer (response with statusCode)' , ( ) => {
92+ const expectedSubject = 'GET|1672531200000|/api/test|200|response-body' ;
93+ const result = calculateHMACSubject ( {
94+ urlPath : '/api/test' ,
95+ text : Buffer . from ( 'response-body' , 'utf8' ) ,
96+ timestamp : MOCK_TIMESTAMP ,
97+ statusCode : 200 ,
98+ method : 'get' ,
99+ authVersion : 3 ,
100+ } ) ;
101+ expect ( Buffer . isBuffer ( result ) ) . to . be . true ;
102+ expect ( result . toString ( 'utf8' ) ) . to . equal ( expectedSubject ) ;
103+ } ) ;
104+
105+ it ( 'should handle Buffer text with authVersion 2' , ( ) => {
106+ const expectedSubject = '1672531200000|/api/test|request-body' ;
107+ const result = calculateHMACSubject ( {
108+ urlPath : '/api/test' ,
109+ text : Buffer . from ( 'request-body' , 'utf8' ) ,
110+ timestamp : MOCK_TIMESTAMP ,
111+ method : 'post' ,
112+ authVersion : 2 ,
113+ } ) ;
114+ expect ( Buffer . isBuffer ( result ) ) . to . be . true ;
115+ expect ( result . toString ( 'utf8' ) ) . to . equal ( expectedSubject ) ;
116+ } ) ;
77117 } ) ;
78118
79119 describe ( 'calculateRequestHMAC' , ( ) => {
@@ -113,7 +153,7 @@ describe('HMAC Utility Functions', () => {
113153 } ) ;
114154
115155 describe ( 'verifyResponse' , ( ) => {
116- it ( 'should verify the HMAC and timestamp validity' , ( ) => {
156+ it ( 'should verify the HMAC and timestamp validity with string text ' , ( ) => {
117157 const result = verifyResponse ( {
118158 url : '/api/test' ,
119159 statusCode : 200 ,
@@ -130,6 +170,7 @@ describe('HMAC Utility Functions', () => {
130170 expectedHmac : 'a16c08b1fa8bff1e2e58d1831855e1745361f78bd6eb6e18b5b7ee17ae0a3bb7' ,
131171 isInResponseValidityWindow : true ,
132172 } ) ;
173+ expect ( typeof result . signatureSubject ) . to . equal ( 'string' ) ;
133174 } ) ;
134175
135176 it ( 'should return invalid if HMAC does not match' , ( ) => {
@@ -161,5 +202,23 @@ describe('HMAC Utility Functions', () => {
161202
162203 expect ( result . isInResponseValidityWindow ) . to . be . false ;
163204 } ) ;
205+
206+ it ( 'should return Buffer signatureSubject when text is Buffer' , ( ) => {
207+ const textBuffer = Buffer . from ( 'response-body' , 'utf8' ) ;
208+ const result = verifyResponse ( {
209+ url : '/api/test' ,
210+ statusCode : 200 ,
211+ text : textBuffer ,
212+ timestamp : MOCK_TIMESTAMP ,
213+ token : 'test-token' ,
214+ hmac : 'a16c08b1fa8bff1e2e58d1831855e1745361f78bd6eb6e18b5b7ee17ae0a3bb7' ,
215+ method : 'post' ,
216+ authVersion : 3 ,
217+ } ) ;
218+
219+ expect ( result . isValid ) . to . be . true ;
220+ expect ( Buffer . isBuffer ( result . signatureSubject ) ) . to . be . true ;
221+ expect ( result . signatureSubject . toString ( 'utf8' ) ) . to . equal ( 'POST|1672531200000|/api/test|200|response-body' ) ;
222+ } ) ;
164223 } ) ;
165224} ) ;
0 commit comments