@@ -58,9 +58,11 @@ test('Calls the purge API endpoint and returns `undefined` if the operation was
5858 expect ( mockAPI . fulfilled ) . toBeTruthy ( )
5959} )
6060
61- test ( 'Throws if the API response does not have a successful status code' , async ( ) => {
61+ test ( 'Throws an error if the API response does not have a successful status code, using the response body as part of the error message ' , async ( ) => {
6262 if ( ! hasFetchAPI ) {
6363 console . warn ( 'Skipping test requires the fetch API' )
64+
65+ return
6466 }
6567
6668 const mockSiteID = '123456789'
@@ -77,7 +79,7 @@ test('Throws if the API response does not have a successful status code', async
7779 } ,
7880 headers : { Authorization : `Bearer ${ mockToken } ` } ,
7981 method : 'post' ,
80- response : new Response ( null , { status : 500 } ) ,
82+ response : new Response ( 'site not found' , { status : 404 } ) ,
8183 url : `https://api.netlify.com/api/v1/purge` ,
8284 } )
8385 // eslint-disable-next-line unicorn/consistent-function-scoping
@@ -90,14 +92,54 @@ test('Throws if the API response does not have a successful status code', async
9092 try {
9193 await invokeLambda ( myFunction )
9294
93- throw new Error ( 'Invocation should have failed' )
95+ expect . fail ( 'Invocation should have failed' )
9496 } catch ( error ) {
9597 expect ( ( error as NodeJS . ErrnoException ) . message ) . toBe (
96- 'Cache purge API call returned an unexpected status code: 500 ' ,
98+ 'Cache purge API call was unsuccessful.\nStatus: 404\nBody: site not found ' ,
9799 )
98100 }
99101} )
100102
103+ test ( 'Throws if the API response does not have a successful status code, does not include the response body if it is not text' , async ( ) => {
104+ if ( ! hasFetchAPI ) {
105+ console . warn ( 'Skipping test requires the fetch API' )
106+
107+ return
108+ }
109+
110+ const mockSiteID = '123456789'
111+ const mockToken = '1q2w3e4r5t6y7u8i9o0p'
112+
113+ process . env . NETLIFY_PURGE_API_TOKEN = mockToken
114+ process . env . SITE_ID = mockSiteID
115+
116+ const mockAPI = new MockFetch ( ) . post ( {
117+ body : ( payload : string ) => {
118+ const data = JSON . parse ( payload )
119+
120+ expect ( data . site_id ) . toBe ( mockSiteID )
121+ } ,
122+ headers : { Authorization : `Bearer ${ mockToken } ` } ,
123+ method : 'post' ,
124+ response : new Response ( null , { status : 500 } ) ,
125+ url : `https://api.netlify.com/api/v1/purge` ,
126+ } )
127+ // eslint-disable-next-line unicorn/consistent-function-scoping
128+ const myFunction = async ( ) => {
129+ await purgeCache ( )
130+ }
131+
132+ globalThis . fetch = mockAPI . fetcher
133+
134+ try {
135+ await invokeLambda ( myFunction )
136+
137+ throw new Error ( 'Invocation should have failed' )
138+ } catch ( error ) {
139+ expect ( ( error as NodeJS . ErrnoException ) . message ) . toBe ( 'Cache purge API call was unsuccessful.\nStatus: 500' )
140+ }
141+ } )
142+
101143test ( 'Ignores purgeCache if in local dev with no token or site' , async ( ) => {
102144 if ( ! hasFetchAPI ) {
103145 console . warn ( 'Skipping test requires the fetch API' )
0 commit comments