@@ -27,15 +27,13 @@ const io = { mv: mock.fn(async () => {}) };
2727const globCreate = mock . fn ( async ( ) => ( { glob : async ( ) => [ ] as string [ ] } ) ) ;
2828const glob = { create : globCreate } ;
2929
30- const httpGet = mock . fn ( async ( ) => httpResponse ) ;
31- const httpResponse = {
32- message : { statusCode : 200 } ,
33- readBody : mock . fn ( async ( ) => '' ) ,
30+ const fetchResponse = {
31+ ok : true ,
32+ status : 200 ,
33+ text : mock . fn ( async ( ) => '' ) ,
3434} ;
35- class HttpClient {
36- get = httpGet ;
37- }
38- const http = { HttpClient } ;
35+ const fetchMock = mock . fn ( async ( ) => fetchResponse ) ;
36+ globalThis . fetch = fetchMock as unknown as typeof fetch ;
3937
4038const readdir = mock . fn ( async ( ) => [ ] as string [ ] ) ;
4139const generateFileHash = mock . fn ( async ( ) => randomBytes ( 32 ) ) ;
@@ -45,7 +43,6 @@ mock.module('@actions/exec', { namedExports: exec });
4543mock . module ( '@actions/tool-cache' , { namedExports : tc } ) ;
4644mock . module ( '@actions/io' , { namedExports : io } ) ;
4745mock . module ( '@actions/glob' , { namedExports : glob } ) ;
48- mock . module ( '@actions/http-client' , { namedExports : http } ) ;
4946mock . module ( 'node:fs/promises' , { namedExports : { readdir } } ) ;
5047mock . module ( '../src/crypto.ts' , { namedExports : { generateFileHash } } ) ;
5148
@@ -58,7 +55,7 @@ function resetAll() {
5855 core . startGroup , core . endGroup , core . isDebug , core . platform . getDetails ,
5956 exec . exec ,
6057 tc . downloadTool , tc . cacheFile , tc . cacheDir ,
61- io . mv , globCreate , httpGet , httpResponse . readBody ,
58+ io . mv , globCreate , fetchMock , fetchResponse . text ,
6259 readdir , generateFileHash ,
6360 ] ;
6461 for ( const fn of fns ) fn . mock . resetCalls ( ) ;
@@ -72,9 +69,10 @@ function resetAll() {
7269 tc . cacheDir . mock . mockImplementation ( async ( ) => `C:/tools/${ randomUUID ( ) } ` ) ;
7370 io . mv . mock . mockImplementation ( async ( ) => { } ) ;
7471 globCreate . mock . mockImplementation ( async ( ) => ( { glob : async ( ) => [ ] } ) ) ;
75- httpResponse . message = { statusCode : 200 } ;
76- httpResponse . readBody . mock . mockImplementation ( async ( ) => '' ) ;
77- httpGet . mock . mockImplementation ( async ( ) => httpResponse ) ;
72+ fetchResponse . ok = true ;
73+ fetchResponse . status = 200 ;
74+ fetchResponse . text . mock . mockImplementation ( async ( ) => '' ) ;
75+ fetchMock . mock . mockImplementation ( async ( ) => fetchResponse ) ;
7876 readdir . mock . mockImplementation ( async ( ) => [ ] ) ;
7977 generateFileHash . mock . mockImplementation ( async ( ) => randomBytes ( 32 ) ) ;
8078}
@@ -312,8 +310,9 @@ describe('utils', () => {
312310 } ) ;
313311 describe ( '.downloadUpdateInstaller()' , ( ) => {
314312 beforeEach ( ( ) => {
315- httpResponse . message = { statusCode : 200 } ;
316- httpResponse . readBody . mock . mockImplementation ( async ( ) => '<a href="https://download.microsoft.com/update.exe">' ) ;
313+ fetchResponse . ok = true ;
314+ fetchResponse . status = 200 ;
315+ fetchResponse . text . mock . mockImplementation ( async ( ) => '<a href="https://download.microsoft.com/update.exe">' ) ;
317316 } ) ;
318317 it ( 'returns a path to an exe' , async ( ) => {
319318 const res = await utils . downloadUpdateInstaller ( {
@@ -349,17 +348,30 @@ describe('utils', () => {
349348 updateUrl : 'https://example.com/sqlupdate.exe' ,
350349 } ) ;
351350 assert . match ( res , / ^ C : \/ t o o l s \/ [ a - f 0 - 9 - ] * \/ s q l u p d a t e \. e x e $ / ) ;
352- assert . equal ( httpGet . mock . callCount ( ) , 0 ) ;
351+ assert . equal ( fetchMock . mock . callCount ( ) , 0 ) ;
353352 } ) ;
354353 it ( 'returns empty string if URL is not resolved' , async ( ) => {
355- httpResponse . readBody . mock . mockImplementation ( async ( ) => '<a href="https://example.com/update.exe">' ) ;
354+ fetchResponse . text . mock . mockImplementation ( async ( ) => '<a href="https://example.com/update.exe">' ) ;
356355 const res = await utils . downloadUpdateInstaller ( {
357356 exeUrl : 'https://example.com/installer.exe' ,
358357 version : '2022' ,
359358 updateUrl : 'https://example.com/sqlupdate.html' ,
360359 } ) ;
361360 assert . equal ( res , '' ) ;
362- assert . equal ( httpGet . mock . callCount ( ) , 1 ) ;
361+ assert . equal ( fetchMock . mock . callCount ( ) , 1 ) ;
362+ } ) ;
363+ it ( 'returns empty string if the update page request is rejected' , async ( ) => {
364+ fetchResponse . ok = false ;
365+ fetchResponse . status = 403 ;
366+ const res = await utils . downloadUpdateInstaller ( {
367+ exeUrl : 'https://example.com/installer.exe' ,
368+ version : '2022' ,
369+ updateUrl : 'https://example.com/sqlupdate.html' ,
370+ } ) ;
371+ assert . equal ( res , '' ) ;
372+ const calls = core . info . mock . calls . filter ( ( c ) => String ( c . arguments [ 0 ] ) . startsWith ( 'Response code' ) ) ;
373+ assert . equal ( calls . length , 1 ) ;
374+ assert . equal ( String ( calls [ 0 ] . arguments [ 0 ] ) , 'Response code: 403' ) ;
363375 } ) ;
364376 } ) ;
365377 describe ( '.gatherSummaryFiles()' , ( ) => {
0 commit comments