@@ -4,6 +4,7 @@ const process = require('process')
4
4
const os = require ( 'os' )
5
5
const cpy = require ( 'cpy' )
6
6
const { dir : getTmpDir } = require ( 'tmp-promise' )
7
+ const { downloadFile } = require ( '../src/templates/handlerUtils' )
7
8
8
9
const plugin = require ( '../src' )
9
10
@@ -186,8 +187,10 @@ describe('onBuild()', () => {
186
187
187
188
expect ( existsSync ( `.netlify/internal-functions/___netlify-handler/___netlify-handler.js` ) ) . toBeTruthy ( )
188
189
expect ( existsSync ( `.netlify/internal-functions/___netlify-handler/bridge.js` ) ) . toBeTruthy ( )
190
+ expect ( existsSync ( `.netlify/internal-functions/___netlify-handler/handlerUtils.js` ) ) . toBeTruthy ( )
189
191
expect ( existsSync ( `.netlify/internal-functions/___netlify-odb-handler/___netlify-odb-handler.js` ) ) . toBeTruthy ( )
190
192
expect ( existsSync ( `.netlify/internal-functions/___netlify-odb-handler/bridge.js` ) ) . toBeTruthy ( )
193
+ expect ( existsSync ( `.netlify/internal-functions/___netlify-odb-handler/handlerUtils.js` ) ) . toBeTruthy ( )
191
194
} )
192
195
193
196
test ( 'writes correct redirects to netlifyConfig' , async ( ) => {
@@ -448,3 +451,37 @@ describe('utility functions', () => {
448
451
}
449
452
} )
450
453
} )
454
+
455
+ describe ( 'function helpers' , ( ) => {
456
+ it ( 'downloadFile can download a file' , async ( ) => {
457
+ const url =
458
+ 'https://raw.githubusercontent.com/netlify/netlify-plugin-nextjs/c2668af24a78eb69b33222913f44c1900a3bce23/manifest.yml'
459
+ const tmpFile = join ( os . tmpdir ( ) , 'next-test' , 'downloadfile.txt' )
460
+ await ensureDir ( path . dirname ( tmpFile ) )
461
+ await downloadFile ( url , tmpFile )
462
+ expect ( existsSync ( tmpFile ) ) . toBeTruthy ( )
463
+ expect ( readFileSync ( tmpFile , 'utf8' ) ) . toMatchInlineSnapshot ( `
464
+ "name: netlify-plugin-nextjs-experimental
465
+ "
466
+ ` )
467
+ await unlink ( tmpFile )
468
+ } )
469
+
470
+ it ( 'downloadFile throws on bad domain' , async ( ) => {
471
+ const url = 'https://nonexistentdomain.example'
472
+ const tmpFile = join ( os . tmpdir ( ) , 'next-test' , 'downloadfile.txt' )
473
+ await ensureDir ( path . dirname ( tmpFile ) )
474
+ await expect ( downloadFile ( url , tmpFile ) ) . rejects . toThrowErrorMatchingInlineSnapshot (
475
+ `"getaddrinfo ENOTFOUND nonexistentdomain.example"` ,
476
+ )
477
+ } )
478
+
479
+ it ( 'downloadFile throws on 404' , async ( ) => {
480
+ const url = 'https://example.com/nonexistentfile'
481
+ const tmpFile = join ( os . tmpdir ( ) , 'next-test' , 'downloadfile.txt' )
482
+ await ensureDir ( path . dirname ( tmpFile ) )
483
+ await expect ( downloadFile ( url , tmpFile ) ) . rejects . toThrowError (
484
+ 'Failed to download https://example.com/nonexistentfile: 404 Not Found' ,
485
+ )
486
+ } )
487
+ } )
0 commit comments