fix(playwrighttesting):escape config parameters (#31460)

### Packages impacted by this PR
@azure/microsoft-playwright-testing

### Issues associated with this PR


### Describe the problem that is addressed by this PR


### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
This commit is contained in:
Kashish Gupta 2024-10-18 18:18:01 +05:30 коммит произвёл GitHub
Родитель d2356b1450
Коммит ee6d08ecb0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
4 изменённых файлов: 31 добавлений и 10 удалений

Просмотреть файл

@ -231,7 +231,7 @@ class MPTReporter implements Reporter {
`\nTest run report successfully initialized: ${testRunResponse?.displayName}.`,
);
process.stdout.write(
`Initializing reporting for this test run. You can view the results at: https://playwright.microsoft.com/workspaces/${this.envVariables.accountId}/runs/${this.envVariables.runId}\n`,
`Initializing reporting for this test run. You can view the results at: https://playwright.microsoft.com/workspaces/${encodeURIComponent(this.envVariables.accountId!)}/runs/${encodeURIComponent(this.envVariables.runId!)}\n`,
);
const shardResponse = await this.serviceClient.postTestRunShardStart();
this.shard = shardResponse;
@ -241,9 +241,9 @@ class MPTReporter implements Reporter {
this.envVariables.accountId &&
this.envVariables.runId
) {
this.testRunUrl = `${Constants.DEFAULT_DASHBOARD_ENDPOINT}/workspaces/${encodeURI(
this.testRunUrl = `${Constants.DEFAULT_DASHBOARD_ENDPOINT}/workspaces/${encodeURIComponent(
this.envVariables.accountId,
)}/runs/${encodeURI(this.envVariables.runId)}`;
)}/runs/${encodeURIComponent(this.envVariables.runId)}`;
}
return true;
} catch (err: any) {

Просмотреть файл

@ -40,9 +40,12 @@ export class ServiceClient {
async patchTestRun(ciInfo: CIInfo): Promise<TestRun> {
const testRun = await this.reporterUtils.getTestRunObject(ciInfo);
// Escape the runId to avoid issues with special characters
const escapedRunId = encodeURIComponent(this.envVariables.runId!);
const response: PipelineResponse = await this.httpService.callAPI(
"PATCH",
`${this.getServiceEndpoint()}/${Constants.testRunsEndpoint.replace("{workspaceId}", this.envVariables.accountId!)}/${this.envVariables.runId}?api-version=${Constants.API_VERSION}`,
`${this.getServiceEndpoint()}/${Constants.testRunsEndpoint.replace("{workspaceId}", this.envVariables.accountId!)}/${escapedRunId}?api-version=${Constants.API_VERSION}`,
JSON.stringify(testRun),
this.envVariables.accessToken,
"application/merge-patch+json",
@ -72,9 +75,10 @@ export class ServiceClient {
async postTestRunShardStart(): Promise<Shard> {
const postTestRunShardObject = this.reporterUtils.getTestRunShardStartObject();
const escapedRunId = encodeURIComponent(this.envVariables.runId!);
const response: PipelineResponse = await this.httpService.callAPI(
"POST",
`${this.getServiceEndpoint()}/${Constants.testRunsShardEndpoint.replace("{workspaceId}", this.envVariables.accountId!).replace("{testRunId}", this.envVariables.runId)}/?api-version=${Constants.API_VERSION}`,
`${this.getServiceEndpoint()}/${Constants.testRunsShardEndpoint.replace("{workspaceId}", this.envVariables.accountId!).replace("{testRunId}", escapedRunId)}/?api-version=${Constants.API_VERSION}`,
JSON.stringify(postTestRunShardObject),
this.envVariables.accessToken,
"application/json",
@ -105,9 +109,10 @@ export class ServiceClient {
attachmentMetadata,
workers,
);
const escapedRunId = encodeURIComponent(this.envVariables.runId!);
const response: PipelineResponse = await this.httpService.callAPI(
"POST",
`${this.getServiceEndpoint()}/${Constants.testRunsShardEndpoint.replace("{workspaceId}", this.envVariables.accountId!).replace("{testRunId}", this.envVariables.runId)}/?api-version=${Constants.API_VERSION}`,
`${this.getServiceEndpoint()}/${Constants.testRunsShardEndpoint.replace("{workspaceId}", this.envVariables.accountId!).replace("{testRunId}", escapedRunId)}/?api-version=${Constants.API_VERSION}`,
JSON.stringify(postTestRunShardObject),
this.envVariables.accessToken,
"application/json",
@ -147,9 +152,10 @@ export class ServiceClient {
}
async createStorageUri(): Promise<StorageUri> {
const escapedRunId = encodeURIComponent(this.envVariables.runId!);
const response: PipelineResponse = await this.httpService.callAPI(
"POST",
`${this.getServiceEndpoint()}/${Constants.storageUriEndpoint.replace("{workspaceId}", this.envVariables.accountId!).replace("{testRunId}", this.envVariables.runId)}?api-version=${Constants.API_VERSION}`,
`${this.getServiceEndpoint()}/${Constants.storageUriEndpoint.replace("{workspaceId}", this.envVariables.accountId!).replace("{testRunId}", escapedRunId)}?api-version=${Constants.API_VERSION}`,
null,
this.envVariables.accessToken,
"application/json",

Просмотреть файл

@ -69,7 +69,7 @@ export const getAndSetRunId = (): string => {
};
export const getServiceWSEndpoint = (runId: string, runName: string, os: string): string => {
return `${getServiceBaseURL()}?runId=${runId}&runName=${runName}&os=${os}&api-version=${API_VERSION}`;
return `${getServiceBaseURL()}?runId=${encodeURIComponent(runId)}&runName=${encodeURIComponent(runName)}&os=${os}&api-version=${API_VERSION}`;
};
export const validateServiceUrl = (): void => {

Просмотреть файл

@ -70,13 +70,28 @@ describe("Service Utils", () => {
delete process.env[InternalEnvironmentVariables.MPT_SERVICE_RUN_ID];
});
it("should return service base url with query params", () => {
it("should return service base url with query params and should escape runID", () => {
process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_URL] =
"wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers";
const runId = "2021-10-11T07:00:00.000Z";
const escapeRunId = encodeURIComponent(runId);
const runName = "runName";
const os = "windows";
const expected = `wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers?runId=${runId}&runName=${runName}&os=${os}&api-version=${API_VERSION}`;
const expected = `wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers?runId=${escapeRunId}&runName=${runName}&os=${os}&api-version=${API_VERSION}`;
expect(getServiceWSEndpoint(runId, runName, os)).to.equal(expected);
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_URL];
});
it("should escape special character in runName and runId", () => {
process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_URL] =
"wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers";
const runId = "2021-10-11T07:00:00.000Z";
const escapeRunId = encodeURIComponent(runId);
const runName = "run#Name-12/09";
const escapeRunName = encodeURIComponent(runName);
const os = "windows";
const expected = `wss://eastus.api.playwright.microsoft.com/accounts/1234/browsers?runId=${escapeRunId}&runName=${escapeRunName}&os=${os}&api-version=${API_VERSION}`;
expect(getServiceWSEndpoint(runId, runName, os)).to.equal(expected);
delete process.env[ServiceEnvironmentVariable.PLAYWRIGHT_SERVICE_URL];