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¶m2=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¶m2=value2" ) ;
12
+ expect ( parsedUrl . hash ) . toBe ( "#section" ) ;
13
+ } ) ;
14
+
15
+ test ( "getQueryParams 方法应正确获取查询参数" , ( ) => {
16
+ const url = "https://www.example.com/page?param1=value1¶m2=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¶m2=value2" ) ;
28
+ } ) ;
29
+
30
+ test ( "removeQueryParams 方法应正确移除指定的查询参数" , ( ) => {
31
+ const url = "https://www.example.com/page?param1=value1¶m2=value2¶m3=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