Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/clients/apim-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,8 @@ export class ApimClient implements IApimClient {

async validatePreFlight(context: ApimServiceContext): Promise<void> {
// Check resource group exists
const rgUrl = `https://management.azure.com/subscriptions/${encodeURIComponent(context.subscriptionId)}/resourceGroups/${encodeURIComponent(context.resourceGroup)}?api-version=${ApimClient.RESOURCE_GROUP_API_VERSION}`;
const armEndpoint = new URL(context.baseUrl).origin;
const rgUrl = `${armEndpoint}/subscriptions/${encodeURIComponent(context.subscriptionId)}/resourceGroups/${encodeURIComponent(context.resourceGroup)}?api-version=${ApimClient.RESOURCE_GROUP_API_VERSION}`;
let rgResponse: Response;
try {
rgResponse = await this.request(rgUrl, { method: 'GET' });
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/clients/apim-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { ApimClient, HttpError } from '../../../src/clients/apim-client.js';
import { ResourceType } from '../../../src/models/resource-types.js';
import { ApimServiceContext } from '../../../src/models/types.js';
import { buildArmBaseUrl } from '../../../src/lib/cloud-config.js';

const testContext: ApimServiceContext = {
subscriptionId: 'sub-1',
Expand All @@ -31,6 +32,45 @@ function makeResponse(
});
}

describe('ApimClient.validatePreFlight', () => {
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});

it.each([
['public', 'https://management.azure.com'],
['usgov', 'https://management.usgovcloudapi.net'],
['china', 'https://management.chinacloudapi.cn'],
['germany', 'https://management.microsoftazure.de'],
])('should use the %s endpoint for both pre-flight checks', async (cloud, endpoint) => {
const client = new ApimClient();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn(client as any, 'getToken').mockResolvedValue('fake-token');
const fetchSpy = vi.fn().mockImplementation(() => Promise.resolve(makeResponse(200, {})));
vi.stubGlobal('fetch', fetchSpy);
const context: ApimServiceContext = {
...testContext,
resourceGroup: 'rg with spaces',
baseUrl: buildArmBaseUrl(cloud, testContext.subscriptionId, 'rg with spaces', testContext.serviceName),
};

await client.validatePreFlight(context);

expect(fetchSpy).toHaveBeenCalledTimes(2);
expect(fetchSpy).toHaveBeenNthCalledWith(
1,
`${endpoint}/subscriptions/sub-1/resourceGroups/rg%20with%20spaces?api-version=2021-04-01`,
expect.objectContaining({ method: 'GET' }),
);
expect(fetchSpy).toHaveBeenNthCalledWith(
2,
`${context.baseUrl}?api-version=${context.apiVersion}`,
expect.objectContaining({ method: 'GET' }),
);
});
});

describe('ApimClient.listResources', () => {
let client: ApimClient;
let fetchSpy: ReturnType<typeof vi.fn>;
Expand Down