|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { type IGetSender } from "@Packages/message/server"; |
| 3 | +import { type ExtMessageSender } from "@Packages/message/types"; |
| 4 | +import { isConnectMatched } from "./gm_api"; |
| 5 | + |
| 6 | +// 小工具:建立假的 IGetSender |
| 7 | +const makeSender = (url?: string): IGetSender => ({ |
| 8 | + getSender: () => (url ? { url } : {}), |
| 9 | + getType: () => 0, |
| 10 | + isType: (_type: any) => false, |
| 11 | + getExtMessageSender: () => null as unknown as ExtMessageSender, |
| 12 | + getConnect: () => undefined, |
| 13 | +}); |
| 14 | + |
| 15 | +describe("isConnectMatched", () => { |
| 16 | + it("回传 false 当 metadataConnect 为 undefined 或空阵列", () => { |
| 17 | + const req = new URL("https://api.example.com/v1"); |
| 18 | + expect(isConnectMatched(undefined, req, makeSender("https://app.example.com"))).toBe(false); |
| 19 | + expect(isConnectMatched([], req, makeSender("https://app.example.com"))).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it('遇到 "*" 应回传 true', () => { |
| 23 | + const req = new URL("https://anything.example.com/path"); |
| 24 | + expect(isConnectMatched(["*"], req, makeSender())).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it("尾缀网域比对成功时回传 true(example.com 比对 api.example.com)", () => { |
| 28 | + const req = new URL("https://api.example.com/users"); |
| 29 | + expect(isConnectMatched(["example.com"], req, makeSender())).toBe(true); |
| 30 | + expect(isConnectMatched(["foo.com", "bar.net", "example.com"], req, makeSender())).toBe(true); |
| 31 | + expect(isConnectMatched(["foo.com", "bar.net", "api.example.com"], req, makeSender())).toBe(true); |
| 32 | + expect(isConnectMatched(["foo.com", "bar.net", "apiexample.com"], req, makeSender())).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it("尾缀网域比对成功时回传 true(myapple.com vs apple.com)", () => { |
| 36 | + const req = new URL("https://myapple.com/users"); |
| 37 | + expect(isConnectMatched(["myapple.com"], req, makeSender())).toBe(true); |
| 38 | + expect(isConnectMatched(["apple.com"], req, makeSender())).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it('metadata 包含 "self" 且 sender.url 与 reqURL 主机相同时回传 true', () => { |
| 42 | + const req = new URL("https://app.example.com/dashboard"); |
| 43 | + const sender = makeSender("https://app.example.com/some-page"); |
| 44 | + expect(isConnectMatched(["self"], req, sender)).toBe(true); |
| 45 | + }); |
| 46 | + |
| 47 | + it('metadata 包含 "self" 但 sender.url 与 reqURL 主机不同时回传 false(若无其他规则命中)', () => { |
| 48 | + const req = new URL("https://api.example.com/resource"); |
| 49 | + const sender = makeSender("https://news.example.com/article"); |
| 50 | + expect(isConnectMatched(["self"], req, sender)).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it('当 sender.getSender() 回传没有 url 或无效 URL 时,"self" 不应报错且回传 false(若无其他规则命中)', () => { |
| 54 | + const req = new URL("https://example.com/path"); |
| 55 | + |
| 56 | + // 无 url |
| 57 | + const senderNoUrl = makeSender(); |
| 58 | + expect(isConnectMatched(["self"], req, senderNoUrl)).toBe(false); |
| 59 | + |
| 60 | + // 无效 URL(try/catch 会吞掉错误) |
| 61 | + const senderBadUrl = makeSender("not a valid url"); |
| 62 | + expect(isConnectMatched(["self"], req, senderBadUrl)).toBe(false); |
| 63 | + }); |
| 64 | + |
| 65 | + it('当 "self" 不符合但尾缀规则符合时仍应回传 true(走到后续条件)', () => { |
| 66 | + const req = new URL("https://api.example.com/data"); |
| 67 | + const sender = makeSender("https://other.site.com/"); |
| 68 | + expect(isConnectMatched(["self", "example.com"], req, sender)).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it("完全不匹配时回传 false", () => { |
| 72 | + const req = new URL("https://api.foo.com"); |
| 73 | + const sender = makeSender("https://bar.com"); |
| 74 | + expect(isConnectMatched(["baz.com", "qux.net"], req, sender)).toBe(false); |
| 75 | + }); |
| 76 | + |
| 77 | + it("域名不区分大小写", () => { |
| 78 | + const req = new URL("https://API.Example.COM/Path"); |
| 79 | + expect(isConnectMatched(["example.com"], req, makeSender())).toBe(true); |
| 80 | + expect(isConnectMatched(["EXAMPLE.COM"], req, makeSender())).toBe(true); |
| 81 | + expect(isConnectMatched(["Api.Example.com"], req, makeSender())).toBe(true); |
| 82 | + }); |
| 83 | +}); |
0 commit comments