File size: 4,239 Bytes
0ad74ed |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
import {
resolve_root,
get_jwt,
determine_protocol,
parse_and_set_cookies
} from "../helpers/init_helpers";
import { initialise_server } from "./server";
import { beforeAll, afterEach, afterAll, it, expect, describe } from "vitest";
import { Client } from "../client";
import { INVALID_CREDENTIALS_MSG, MISSING_CREDENTIALS_MSG } from "../constants";
const server = initialise_server();
beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
describe("resolve_root", () => {
it('should return the base URL if the root path starts with "http://"', () => {
const base_url = "https://huggingface.co";
const root_path = "https://hmb-hello-world.hf.space";
const prioritize_base = true;
const result = resolve_root(base_url, root_path, prioritize_base);
expect(result).toBe(base_url);
});
it('should return the base URL if the root path starts with "https://"', () => {
const base_url = "https://huggingface.co";
const root_path = "https://hmb-hello-world.hf.space";
const prioritize_base = true;
const result = resolve_root(base_url, root_path, prioritize_base);
expect(result).toBe(base_url);
});
});
describe("get_jwt", () => {
it("should return a valid JWT token when the API call is successful", async () => {
const space = "hmb/hello_world";
const token = "hf_123";
const expected_jwt = "jwt_123";
const result = await get_jwt(space, token);
expect(result).toBe(expected_jwt);
});
it("should return false when the API call fails", async () => {
const space = "hmb/bye_world";
const token = "hf_123";
const result = await get_jwt(space, token);
expect(result).toBe(false);
});
});
describe("determine_protocol", () => {
it('should return the correct protocols and host when the endpoint starts with "http"', () => {
const endpoint = "http://huggingface.co";
const result = determine_protocol(endpoint);
expect(result).toEqual({
ws_protocol: "ws",
http_protocol: "http:",
host: "huggingface.co"
});
});
it('should return the correct protocols and host when the endpoint starts with "https"', () => {
const endpoint = "https://huggingface.co";
const result = determine_protocol(endpoint);
expect(result).toEqual({
ws_protocol: "wss",
http_protocol: "https:",
host: "huggingface.co"
});
});
it('should return the correct protocols and host when the endpoint starts with "file"', () => {
const endpoint = "file:///path/to/app.html";
const result = determine_protocol(endpoint);
expect(result).toEqual({
ws_protocol: "ws",
http_protocol: "http:",
host: "lite.local"
});
});
});
describe("parse_and_set_cookies", () => {
it("should return an empty array when the cookie header is empty", () => {
const cookie_header = "";
const result = parse_and_set_cookies(cookie_header);
expect(result).toEqual([]);
});
it("should parse the cookie header and return an array of cookies", () => {
const cookie_header = "access-token-123=abc;access-token-unsecured-456=def";
const result = parse_and_set_cookies(cookie_header);
expect(result).toEqual(["access-token-123=abc"]);
});
});
describe("resolve_cookies", () => {
it("should set the cookies when correct auth credentials are provided", async () => {
const client = await Client.connect("hmb/auth_space", {
auth: ["admin", "pass1234"]
});
const api = client.view_api();
expect((await api).named_endpoints["/predict"]).toBeDefined();
});
it("should connect to a private and authenticated space", async () => {
const client = await Client.connect("hmb/private_auth_space", {
hf_token: "hf_123",
auth: ["admin", "pass1234"]
});
const api = client.view_api();
expect((await api).named_endpoints["/predict"]).toBeDefined();
});
it("should not set the cookies when auth credentials are invalid", async () => {
await expect(
Client.connect("hmb/invalid_auth_space", {
auth: ["admin", "wrong_password"]
})
).rejects.toThrowError(INVALID_CREDENTIALS_MSG);
});
it("should not set the cookies when auth option is not provided in an auth space", async () => {
await expect(Client.connect("hmb/unauth_space")).rejects.toThrowError(
MISSING_CREDENTIALS_MSG
);
});
});
|