@@ -7,18 +7,9 @@ import { getSignedCookies, getSignedUrl } from "./index";
77const url = "https://d111111abcdef8.cloudfront.net/private-content/private.jpeg" ;
88const keyPairId = "APKAEIBAERJR2EXAMPLE" ;
99const dateLessThan = "2020-01-01" ;
10- const epochDateLessThan = Math . round (
11- ( typeof dateLessThan === "string" && / ^ \d + $ / . test ( dateLessThan )
12- ? Number ( dateLessThan )
13- : new Date ( dateLessThan ) . getTime ( ) ) / 1000
14- ) ;
10+ const epochDateLessThan = Math . round ( new Date ( dateLessThan ) . getTime ( ) / 1000 ) ;
1511const dateGreaterThan = "2019-12-01" ;
16- const epochDateGreaterThan = Math . round (
17- ( typeof dateGreaterThan === "string" && / ^ \d + $ / . test ( dateGreaterThan )
18- ? Number ( dateGreaterThan )
19- : new Date ( dateGreaterThan ) . getTime ( ) ) / 1000
20- ) ;
21-
12+ const epochDateGreaterThan = Math . round ( new Date ( dateGreaterThan ) . getTime ( ) / 1000 ) ;
2213const ipAddress = "10.0.0.0" ;
2314const privateKey = Buffer . from ( `
2415-----BEGIN RSA PRIVATE KEY-----
@@ -613,3 +604,49 @@ describe("getSignedCookies", () => {
613604 expect ( verifySignature ( denormalizeBase64 ( result [ "CloudFront-Signature" ] ) , policy ) ) . toBeTruthy ( ) ;
614605 } ) ;
615606} ) ;
607+
608+ describe ( "getSignedUrl- when signing a URL with a date range" , ( ) => {
609+ const dateString = "2024-05-17" ;
610+ const dateNumber = 1125674245900 ;
611+ it ( "allows string input compatible with Date constructor" , ( ) => {
612+ const epochDateLessThan = Math . round ( new Date ( dateString ) . getTime ( ) / 1000 ) ;
613+ const resultUrl = getSignedUrl ( {
614+ url,
615+ keyPairId,
616+ dateLessThan : dateString ,
617+ privateKey,
618+ passphrase,
619+ } ) ;
620+ const resultCookies = getSignedCookies ( {
621+ url,
622+ keyPairId,
623+ dateLessThan : dateString ,
624+ privateKey,
625+ passphrase,
626+ } ) ;
627+
628+ expect ( resultUrl ) . toContain ( `Expires=${ epochDateLessThan } ` ) ;
629+ expect ( resultCookies [ "CloudFront-Expires" ] ) . toBe ( epochDateLessThan ) ;
630+ } ) ;
631+
632+ it ( "allows number input in milliseconds compatible with Date constructor" , ( ) => {
633+ const epochDateLessThan = Math . round ( new Date ( dateNumber ) . getTime ( ) / 1000 ) ;
634+ const resultUrl = getSignedUrl ( {
635+ url,
636+ keyPairId,
637+ dateLessThan : dateNumber ,
638+ privateKey,
639+ passphrase,
640+ } ) ;
641+ const resultCookies = getSignedCookies ( {
642+ url,
643+ keyPairId,
644+ dateLessThan : dateNumber ,
645+ privateKey,
646+ passphrase,
647+ } ) ;
648+
649+ expect ( resultUrl ) . toContain ( `Expires=${ epochDateLessThan } ` ) ;
650+ expect ( resultCookies [ "CloudFront-Expires" ] ) . toBe ( epochDateLessThan ) ;
651+ } ) ;
652+ } ) ;
0 commit comments