@@ -123,3 +123,41 @@ test('Ignores purgeCache if in local dev with no token or site', async () => {
123
123
124
124
expect ( response ) . toBeUndefined ( )
125
125
} )
126
+
127
+ test ( 'Accepts a custom user-agent' , async ( ) => {
128
+ if ( ! hasFetchAPI ) {
129
+ console . warn ( 'Skipping test requires the fetch API' )
130
+
131
+ return
132
+ }
133
+
134
+ const userAgent = 'Netlify'
135
+ const mockSiteID = '123456789'
136
+ const mockToken = '1q2w3e4r5t6y7u8i9o0p'
137
+
138
+ process . env . NETLIFY_PURGE_API_TOKEN = mockToken
139
+ process . env . SITE_ID = mockSiteID
140
+
141
+ const mockAPI = new MockFetch ( ) . post ( {
142
+ body : ( payload : string ) => {
143
+ const data = JSON . parse ( payload )
144
+
145
+ expect ( data . site_id ) . toBe ( mockSiteID )
146
+ } ,
147
+ headers : { Authorization : `Bearer ${ mockToken } ` , 'user-agent' : userAgent } ,
148
+ method : 'post' ,
149
+ response : new Response ( null , { status : 202 } ) ,
150
+ url : `https://api.netlify.com/api/v1/purge` ,
151
+ } )
152
+
153
+ const myFunction = async ( ) => {
154
+ await purgeCache ( { userAgent } )
155
+ }
156
+
157
+ globalThis . fetch = mockAPI . fetcher
158
+
159
+ const response = await invokeLambda ( myFunction )
160
+
161
+ expect ( response ) . toBeUndefined ( )
162
+ expect ( mockAPI . fulfilled ) . toBeTruthy ( )
163
+ } )
0 commit comments