2026-06-26 20b96473f2520590a0dca6b775b81e3ea06a77a0
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
package cn.iocoder.yudao.framework.common.util.http;
 
import org.junit.jupiter.api.Test;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
 
/**
 * {@link HttpUtils} 的单元测试
 */
public class HttpUtilsTest {
 
    @Test
    public void testReplaceUrlQuery_replace() {
        // 准备参数
        String url = "https://www.iocoder.cn/path?a=1&b=2";
        // 调用
        String result = HttpUtils.replaceUrlQuery(url, "a", "3");
        // 断言:被替换的 key 会移到末尾,原顺序的其它参数保留
        assertEquals("https://www.iocoder.cn/path?b=2&a=3", result);
    }
 
    @Test
    public void testReplaceUrlQuery_add() {
        // 准备参数
        String url = "https://www.iocoder.cn/path?a=1";
        // 调用
        String result = HttpUtils.replaceUrlQuery(url, "b", "2");
        // 断言
        assertEquals("https://www.iocoder.cn/path?a=1&b=2", result);
    }
 
    @Test
    public void testReplaceUrlQuery_noQuery() {
        // 准备参数:原 URL 没有 query
        String url = "https://www.iocoder.cn/path";
        // 调用
        String result = HttpUtils.replaceUrlQuery(url, "a", "1");
        // 断言
        assertEquals("https://www.iocoder.cn/path?a=1", result);
    }
 
    @Test
    public void testReplaceUrlQuery_emptyValue() {
        // 准备参数:value 为空字符串
        String url = "https://www.iocoder.cn/path?a=1";
        // 调用
        String result = HttpUtils.replaceUrlQuery(url, "a", "");
        // 断言:保留 key,value 为空
        assertEquals("https://www.iocoder.cn/path?a=", result);
    }
 
}