@@ -9,6 +9,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
99import { ApimClient , HttpError } from '../../../src/clients/apim-client.js' ;
1010import { ResourceType } from '../../../src/models/resource-types.js' ;
1111import { ApimServiceContext } from '../../../src/models/types.js' ;
12+ import { buildArmBaseUrl } from '../../../src/lib/cloud-config.js' ;
1213
1314const testContext : ApimServiceContext = {
1415 subscriptionId : 'sub-1' ,
@@ -31,6 +32,45 @@ function makeResponse(
3132 } ) ;
3233}
3334
35+ describe ( 'ApimClient.validatePreFlight' , ( ) => {
36+ afterEach ( ( ) => {
37+ vi . restoreAllMocks ( ) ;
38+ vi . unstubAllGlobals ( ) ;
39+ } ) ;
40+
41+ it . each ( [
42+ [ 'public' , 'https://management.azure.com' ] ,
43+ [ 'usgov' , 'https://management.usgovcloudapi.net' ] ,
44+ [ 'china' , 'https://management.chinacloudapi.cn' ] ,
45+ [ 'germany' , 'https://management.microsoftazure.de' ] ,
46+ ] ) ( 'should use the %s endpoint for both pre-flight checks' , async ( cloud , endpoint ) => {
47+ const client = new ApimClient ( ) ;
48+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
49+ vi . spyOn ( client as any , 'getToken' ) . mockResolvedValue ( 'fake-token' ) ;
50+ const fetchSpy = vi . fn ( ) . mockImplementation ( ( ) => Promise . resolve ( makeResponse ( 200 , { } ) ) ) ;
51+ vi . stubGlobal ( 'fetch' , fetchSpy ) ;
52+ const context : ApimServiceContext = {
53+ ...testContext ,
54+ resourceGroup : 'rg with spaces' ,
55+ baseUrl : buildArmBaseUrl ( cloud , testContext . subscriptionId , 'rg with spaces' , testContext . serviceName ) ,
56+ } ;
57+
58+ await client . validatePreFlight ( context ) ;
59+
60+ expect ( fetchSpy ) . toHaveBeenCalledTimes ( 2 ) ;
61+ expect ( fetchSpy ) . toHaveBeenNthCalledWith (
62+ 1 ,
63+ `${ endpoint } /subscriptions/sub-1/resourceGroups/rg%20with%20spaces?api-version=2021-04-01` ,
64+ expect . objectContaining ( { method : 'GET' } ) ,
65+ ) ;
66+ expect ( fetchSpy ) . toHaveBeenNthCalledWith (
67+ 2 ,
68+ `${ context . baseUrl } ?api-version=${ context . apiVersion } ` ,
69+ expect . objectContaining ( { method : 'GET' } ) ,
70+ ) ;
71+ } ) ;
72+ } ) ;
73+
3474describe ( 'ApimClient.listResources' , ( ) => {
3575 let client : ApimClient ;
3676 let fetchSpy : ReturnType < typeof vi . fn > ;
0 commit comments