Skip to content

Commit d6cd0c3

Browse files
author
梁志伟
committed
🔖新增URL测试用例
1 parent 9cf9e59 commit d6cd0c3

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

Diff for: src/UrlUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default class UrlUtils {
3636

3737
/**
3838
* 获取 URL 中的查询参数
39-
* @param url 包含查询参数的 URL 字符串
39+
* @param url 包含查询参数的 URL 字符串å
4040
* @returns 包含所有查询参数的对象
4141
*
4242
* @example

Diff for: tests/UrlUtils.spec.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import UrlUtils from "../src/UrlUtils";
2+
3+
describe("UrlUtils 类测试", () => {
4+
test("parseUrl 方法应正确解析 URL", () => {
5+
const url = "https://www.example.com:8080/path/to/page?param1=value1&param2=value2#section";
6+
const parsedUrl = UrlUtils.parseUrl(url);
7+
8+
expect(parsedUrl.protocol).toBe("https:");
9+
expect(parsedUrl.host).toBe("www.example.com:8080");
10+
expect(parsedUrl.pathname).toBe("/path/to/page");
11+
expect(parsedUrl.search).toBe("?param1=value1&param2=value2");
12+
expect(parsedUrl.hash).toBe("#section");
13+
});
14+
15+
test("getQueryParams 方法应正确获取查询参数", () => {
16+
const url = "https://www.example.com/page?param1=value1&param2=value2";
17+
const params = UrlUtils.getQueryParams(url);
18+
19+
expect(params).toEqual({ param1: "value1", param2: "value2" });
20+
});
21+
22+
test("addQueryParams 方法应正确添加查询参数", () => {
23+
const url = "https://www.example.com/page";
24+
const newParams = { param1: "value1", param2: "value2" };
25+
const newUrl = UrlUtils.addQueryParams(url, newParams);
26+
27+
expect(newUrl).toBe("https://www.example.com/page?param1=value1&param2=value2");
28+
});
29+
30+
test("removeQueryParams 方法应正确移除指定的查询参数", () => {
31+
const url = "https://www.example.com/page?param1=value1&param2=value2&param3=value3";
32+
const newUrl = UrlUtils.removeQueryParams(url, ["param1", "param3"]);
33+
34+
expect(newUrl).toBe("https://www.example.com/page?param2=value2");
35+
});
36+
37+
test("getDomain 方法应正确获取域名", () => {
38+
const url = "https://sub.example.com/page";
39+
const domain = UrlUtils.getDomain(url);
40+
41+
expect(domain).toBe("example.com");
42+
});
43+
44+
test("isAbsoluteUrl 方法应正确判断绝对 URL", () => {
45+
expect(UrlUtils.isAbsoluteUrl("https://www.example.com")).toBe(true);
46+
expect(UrlUtils.isAbsoluteUrl("/path/to/page")).toBe(false);
47+
});
48+
49+
test("resolveRelativeUrl 方法应正确解析相对 URL", () => {
50+
const base = "https://www.example.com/path/";
51+
const relative = "../page";
52+
const absoluteUrl = UrlUtils.resolveRelativeUrl(base, relative);
53+
54+
expect(absoluteUrl).toBe("https://www.example.com/page");
55+
});
56+
});

0 commit comments

Comments
 (0)