@@ -38,8 +38,44 @@ test.describe("API Gateway Tests to Verify Get Letter PDF Endpoint", () => {
3838 ) ;
3939
4040 expect ( response . status ( ) ) . toBe ( 200 ) ;
41- const responseBody = await response . text ( ) ;
42- expect ( responseBody ) . toContain ( "PDF" ) ;
41+ // const responseBody = await response.text();
42+ // expect(responseBody).toContain("PDF");
43+
44+ async function fetchAndValidatePdf ( url : string ) {
45+ const res = await request . get ( url , { headers } ) ;
46+ return {
47+ status : res . status ( ) ,
48+ headers : res . headers ( ) ,
49+ buffer : await res . body ( ) . catch ( ( ) => null ) ,
50+ text : await res . text ( ) . catch ( ( ) => null ) ,
51+ } ;
52+ }
53+
54+ const pdfUrl = await response . url ( ) ;
55+ const parsed = new URL ( pdfUrl ) ;
56+ const expiresParam = parsed . searchParams . get ( "X-Amz-Expires" ) ;
57+
58+ expect ( expiresParam ) . toBeTruthy ( ) ;
59+ const expiresSeconds = Number ( expiresParam ) ;
60+ expect ( Number . isFinite ( expiresSeconds ) ) . toBeTruthy ( ) ;
61+
62+ // Validate the URL works immediately
63+ const url = await fetchAndValidatePdf ( pdfUrl ) ;
64+ expect ( url . status ) . toBe ( 200 ) ;
65+ expect ( url . headers [ "content-type" ] || "" ) . toContain ( "application/pdf" ) ;
66+
67+ const waitMs = Math . max ( expiresSeconds * 1000 + 2000 , 0 ) ;
68+ if ( waitMs > 0 ) {
69+ // Cap wait to avoid extremely long waits
70+ const MAX_WAIT_MS = 120_000 ; // 2 minutes cap
71+ const effectiveWait = Math . min ( waitMs , MAX_WAIT_MS ) ;
72+ await new Promise ( ( resolve ) => {
73+ setTimeout ( resolve , effectiveWait ) ;
74+ } ) ;
75+
76+ const after = await fetchAndValidatePdf ( pdfUrl ) ;
77+ expect ( [ 400 , 403 ] ) . toContain ( after . status ) ;
78+ }
4379 } ) ;
4480
4581 test ( `Get /letters/{id}/data returns 404 if no resource is found for id` , async ( {
0 commit comments