Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
40 changes: 27 additions & 13 deletions sdk/core/core-client-rest/test/internal/clientHelpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { describe, it, assert } from "vitest";
import { createDefaultPipeline } from "../../src/clientHelpers.js";
import { createDefaultPipeline, getCachedDefaultHttpsClient } from "../../src/clientHelpers.js";
import { bearerTokenAuthenticationPolicyName } from "@azure/core-rest-pipeline";
import { keyCredentialAuthenticationPolicyName } from "../../src/keyCredentialAuthenticationPolicy.js";
import type { TokenCredential } from "@azure/core-auth";
Expand Down Expand Up @@ -31,12 +31,14 @@ describe("clientHelpers", () => {
const pipeline = createDefaultPipeline(mockBaseUrl);
const policies = pipeline.getOrderedPolicies();

assert.isDefined(policies, "default pipeline should contain policies");
assert.isNotEmpty(policies, "default pipeline should contain policies");

const apiVersionPolicy = policies.find((p) => p.name === apiVersionPolicyName);
assert.isDefined(
policies.find((p) => p.name === apiVersionPolicyName),
apiVersionPolicy,
`Pipeline policy not found in the default pipeline: ${apiVersionPolicyName}`,
);
assert.strictEqual(apiVersionPolicy!.name, apiVersionPolicyName);
});

it("should throw if key credentials but no Api Header Name", () => {
Expand All @@ -56,17 +58,16 @@ describe("clientHelpers", () => {
);
const policies = pipeline.getOrderedPolicies();

assert.isDefined(policies, "default pipeline should contain policies");
assert.isNotEmpty(policies, "default pipeline should contain policies");

assert.isUndefined(
policies.find((p) => p.name === bearerTokenAuthenticationPolicyName),
"pipeline shouldn't have bearerTokenAuthenticationPolicyName",
);

assert.isDefined(
policies.find((p) => p.name === keyCredentialAuthenticationPolicyName),
"pipeline shouldn have keyCredentialAuthenticationPolicyName",
);
const keyCredPolicy = policies.find((p) => p.name === keyCredentialAuthenticationPolicyName);
assert.isDefined(keyCredPolicy, "pipeline should have keyCredentialAuthenticationPolicyName");
assert.strictEqual(keyCredPolicy!.name, keyCredentialAuthenticationPolicyName);
});

it("should create a default pipeline with TokenCredential", () => {
Expand All @@ -76,16 +77,29 @@ describe("clientHelpers", () => {
const pipeline = createDefaultPipeline(mockBaseUrl, mockCredential);
const policies = pipeline.getOrderedPolicies();

assert.isDefined(policies, "default pipeline should contain policies");
assert.isNotEmpty(policies, "default pipeline should contain policies");

assert.isDefined(
policies.find((p) => p.name === bearerTokenAuthenticationPolicyName),
"pipeline should have bearerTokenAuthenticationPolicyName",
);
const bearerPolicy = policies.find((p) => p.name === bearerTokenAuthenticationPolicyName);
assert.isDefined(bearerPolicy, "pipeline should have bearerTokenAuthenticationPolicyName");
assert.strictEqual(bearerPolicy!.name, bearerTokenAuthenticationPolicyName);

assert.isUndefined(
policies.find((p) => p.name === keyCredentialAuthenticationPolicyName),
"pipeline shouldn have keyCredentialAuthenticationPolicyName",
);
});

describe("getCachedDefaultHttpsClient", () => {
it("should return an HttpClient", () => {
const client = getCachedDefaultHttpsClient();
assert.isDefined(client);
assert.isFunction(client.sendRequest);
});

it("should return the same instance on subsequent calls", () => {
const client1 = getCachedDefaultHttpsClient();
const client2 = getCachedDefaultHttpsClient();
assert.strictEqual(client1, client2, "should return cached instance");
});
});
});
114 changes: 114 additions & 0 deletions sdk/core/core-client-rest/test/internal/getClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,120 @@ describe("getClient", () => {
.get();
});

describe("HTTP methods", () => {
it("should support post method", async () => {
let policyExecuted = false;
const client = getClient("https://example.org", { httpClient });
const validationPolicy: PipelinePolicy = {
name: "validationPolicy",
sendRequest: (req, next) => {
policyExecuted = true;
assert.equal(req.method, "POST");
return next(req);
},
};
client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" });
await client.pathUnchecked("/foo").post();
assert.isTrue(policyExecuted, "Validation policy should have executed");
});

it("should support put method", async () => {
let policyExecuted = false;
const client = getClient("https://example.org", { httpClient });
const validationPolicy: PipelinePolicy = {
name: "validationPolicy",
sendRequest: (req, next) => {
policyExecuted = true;
assert.equal(req.method, "PUT");
return next(req);
},
};
client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" });
await client.pathUnchecked("/foo").put();
assert.isTrue(policyExecuted, "Validation policy should have executed");
});

it("should support patch method", async () => {
let policyExecuted = false;
const client = getClient("https://example.org", { httpClient });
const validationPolicy: PipelinePolicy = {
name: "validationPolicy",
sendRequest: (req, next) => {
policyExecuted = true;
assert.equal(req.method, "PATCH");
return next(req);
},
};
client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" });
await client.pathUnchecked("/foo").patch();
assert.isTrue(policyExecuted, "Validation policy should have executed");
});

it("should support delete method", async () => {
let policyExecuted = false;
const client = getClient("https://example.org", { httpClient });
const validationPolicy: PipelinePolicy = {
name: "validationPolicy",
sendRequest: (req, next) => {
policyExecuted = true;
assert.equal(req.method, "DELETE");
return next(req);
},
};
client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" });
await client.pathUnchecked("/foo").delete();
assert.isTrue(policyExecuted, "Validation policy should have executed");
});

it("should support head method", async () => {
let policyExecuted = false;
const client = getClient("https://example.org", { httpClient });
const validationPolicy: PipelinePolicy = {
name: "validationPolicy",
sendRequest: (req, next) => {
policyExecuted = true;
assert.equal(req.method, "HEAD");
return next(req);
},
};
client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" });
await client.pathUnchecked("/foo").head();
assert.isTrue(policyExecuted, "Validation policy should have executed");
});

it("should support options method", async () => {
let policyExecuted = false;
const client = getClient("https://example.org", { httpClient });
const validationPolicy: PipelinePolicy = {
name: "validationPolicy",
sendRequest: (req, next) => {
policyExecuted = true;
assert.equal(req.method, "OPTIONS");
return next(req);
},
};
client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" });
await client.pathUnchecked("/foo").options();
assert.isTrue(policyExecuted, "Validation policy should have executed");
});

it("should support trace method", async () => {
let policyExecuted = false;
const client = getClient("https://example.org", { httpClient });
const validationPolicy: PipelinePolicy = {
name: "validationPolicy",
sendRequest: (req, next) => {
policyExecuted = true;
assert.equal(req.method, "TRACE");
return next(req);
},
};
client.pipeline.addPolicy(validationPolicy, { afterPhase: "Serialize" });
await client.pathUnchecked("/foo").trace();
assert.isTrue(policyExecuted, "Validation policy should have executed");
});
});

describe("when pipeline is passed via options", () => {
it("should use the provided pipeline when passed via second parameter (options only)", async () => {
let customPolicyInvoked = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { describe, it, assert } from "vitest";
import {
keyCredentialAuthenticationPolicy,
keyCredentialAuthenticationPolicyName,
} from "../../src/keyCredentialAuthenticationPolicy.js";
import { createHttpHeaders, createPipelineRequest } from "@azure/core-rest-pipeline";

describe("keyCredentialAuthenticationPolicy", () => {
it("should set the api key header on the request", async () => {
const credential = { key: "test-api-key" };
const headerName = "x-api-key";
const policy = keyCredentialAuthenticationPolicy(credential, headerName);

assert.equal(policy.name, keyCredentialAuthenticationPolicyName);

const request = createPipelineRequest({
url: "https://example.org",
headers: createHttpHeaders(),
});

const response = await policy.sendRequest(request, async (req) => {
assert.equal(req.headers.get(headerName), "test-api-key");
return { headers: createHttpHeaders(), status: 200, request: req };
});

assert.equal(response.status, 200);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { describe, it, assert } from "vitest";
import { operationOptionsToRequestParameters } from "../../src/operationOptionHelpers.js";

describe("operationOptionsToRequestParameters", () => {
it("should convert empty operation options to request parameters", () => {
const result = operationOptionsToRequestParameters({});
Comment thread
deyaaeldeen marked this conversation as resolved.
Outdated
assert.isDefined(result);
assert.isUndefined(result.abortSignal);
assert.isUndefined(result.onResponse);
assert.isObject(result);
});

it("should pass through abort signal", () => {
const abortController = new AbortController();
const result = operationOptionsToRequestParameters({
abortSignal: abortController.signal,
});
assert.equal(result.abortSignal, abortController.signal);
});
});