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
30 changes: 9 additions & 21 deletions sdk/core/core-client-rest/test/internal/browser/streams.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("[Browser] Streams", () => {
const reader = result.body!.getReader();
// Read the first chunk
const chunk = await reader.read();
assert.equal(chunk.done, false);
assert.isFalse(chunk.done);
expect(fetchMock).toHaveBeenCalledOnce();
});

Expand All @@ -63,7 +63,7 @@ describe("[Browser] Streams", () => {

const result = await client.pathUnchecked("/foo").get();

assert.deepEqual(result.body, responseText);
assert.strictEqual(result.body, responseText);
expect(fetchMock).toHaveBeenCalledOnce();
});

Expand All @@ -73,11 +73,7 @@ describe("[Browser] Streams", () => {
const fetchMock = vi.mocked(fetch);
fetchMock.mockRejectedValue(new Error("ExpectedException"));

try {
await client.pathUnchecked("/foo").get();
} catch (e: any) {
assert.match(e.message, /ExpectedException/);
}
await expect(client.pathUnchecked("/foo").get()).rejects.toThrow(/ExpectedException/);
});

it("should be able to handle errors on streamed response", async () => {
Expand All @@ -86,11 +82,9 @@ describe("[Browser] Streams", () => {
const fetchMock = vi.mocked(fetch);
fetchMock.mockRejectedValue(new Error("ExpectedException"));

try {
await client.pathUnchecked("/foo").get().asBrowserStream();
} catch (e: any) {
assert.match(e.message, /ExpectedException/);
}
await expect(client.pathUnchecked("/foo").get().asBrowserStream()).rejects.toThrow(
/ExpectedException/,
);
});

it("should throw when attempting to use node streams", async () => {
Expand All @@ -99,14 +93,8 @@ describe("[Browser] Streams", () => {

const client = getClient(mockBaseUrl);

try {
await client.pathUnchecked("/foo").get().asNodeStream();
assert.fail("Expected error was not thrown");
} catch (e: any) {
assert.equal(
e.message,
"`isNodeStream` is not supported in the browser environment. Use `asBrowserStream` to obtain the response body stream.",
);
}
await expect(client.pathUnchecked("/foo").get().asNodeStream()).rejects.toThrow(
"`isNodeStream` is not supported in the browser environment. Use `asBrowserStream` to obtain the response body stream.",
);
});
});
34 changes: 14 additions & 20 deletions sdk/core/core-client-rest/test/internal/clientHelpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { describe, it, assert } from "vitest";
import { describe, it, assert, expect } from "vitest";
import { createDefaultPipeline } from "../../src/clientHelpers.js";
import { bearerTokenAuthenticationPolicyName } from "@azure/core-rest-pipeline";
import { keyCredentialAuthenticationPolicyName } from "../../src/keyCredentialAuthenticationPolicy.js";
Expand All @@ -14,7 +14,7 @@ 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");

assert.isUndefined(
policies.find((p) => p.name === bearerTokenAuthenticationPolicyName),
Expand All @@ -31,21 +31,19 @@ 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}`,
);
});

it("should throw if key credentials but no Api Header Name", () => {
try {
createDefaultPipeline(mockBaseUrl, { key: "mockKey" });
assert.fail("Expected to throw an error");
} catch (error: any) {
assert.equal((error as Error).message, "Missing API Key Header Name");
}
expect(() => createDefaultPipeline(mockBaseUrl, { key: "mockKey" })).toThrow(
"Missing API Key Header Name",
);
});

it("should create a default pipeline with key credentials", () => {
Expand All @@ -56,17 +54,15 @@ 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");
});

it("should create a default pipeline with TokenCredential", () => {
Expand All @@ -76,12 +72,10 @@ 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.isUndefined(
policies.find((p) => p.name === keyCredentialAuthenticationPolicyName),
Expand Down
20 changes: 10 additions & 10 deletions sdk/core/core-client-rest/test/internal/createRestError.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// Licensed under the MIT License.

import { createRestError } from "../../src/restError.js";
import type { PipelineRequest } from "@azure/core-rest-pipeline";
import { createPipelineRequest } from "@azure/core-rest-pipeline";
import { describe, it, assert } from "vitest";

describe("createRestError", () => {
it("should create a rest error from a PathUnchecked response with standard error", () => {
const response = {
status: "400",
headers: {},
request: {} as PipelineRequest,
request: createPipelineRequest({ url: "https://example.com" }),
body: {
error: {
code: "code",
Expand All @@ -28,7 +28,7 @@ describe("createRestError", () => {
const response = {
status: "400",
headers: {},
request: {} as PipelineRequest,
request: createPipelineRequest({ url: "https://example.com" }),
body: {
error: {
code: "code",
Expand All @@ -46,7 +46,7 @@ describe("createRestError", () => {
const response = {
status: "400",
headers: {},
request: {} as PipelineRequest,
request: createPipelineRequest({ url: "https://example.com" }),
body: {
code: "code",
message: "message",
Expand All @@ -58,11 +58,11 @@ describe("createRestError", () => {
assert.equal(error.message, "message");
});

it("should create a rest error from an error message and a PathUnchecked response with standard error", () => {
it("should create a rest error from an error message and a PathUnchecked response with top-level error properties", () => {
const response = {
status: "400",
headers: {},
request: {} as PipelineRequest,
request: createPipelineRequest({ url: "https://example.com" }),
body: {
code: "code",
message: "message",
Expand All @@ -78,25 +78,25 @@ describe("createRestError", () => {
const response = {
status: "400",
headers: {},
request: {} as PipelineRequest,
request: createPipelineRequest({ url: "https://example.com" }),
body: undefined,
};
const error = createRestError("error message", response);
assert.equal(error.statusCode, 400);
assert.equal(error.code, undefined);
assert.isUndefined(error.code);
assert.equal(error.message, "error message");
});

it("should create a rest error from an error response with an undefined body and no default message", () => {
const response = {
status: "400",
headers: {},
request: {} as PipelineRequest,
request: createPipelineRequest({ url: "https://example.com" }),
body: undefined,
};
const error = createRestError(response);
assert.equal(error.statusCode, 400);
assert.equal(error.code, undefined);
assert.isUndefined(error.code);
assert.equal(error.message, "Unexpected status code: 400");
});
});
Loading