Commit 41d563500ab96aa0cc932e88f4a369fc73457f22
1 parent
ea3ad5c8
♻️ refactor(common): HttpUtils.replaceUrlQuery 改用 hutool UrlQuery#remove 替换反射实现,…
…并补充单测,基于 yudao-cloud #248 的二开
Showing
2 changed files
with
54 additions
and
8 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/http/HttpUtils.java
| 1 | package cn.iocoder.yudao.framework.common.util.http; | 1 | package cn.iocoder.yudao.framework.common.util.http; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.codec.Base64; | 3 | import cn.hutool.core.codec.Base64; |
| 4 | -import cn.hutool.core.map.TableMap; | ||
| 5 | import cn.hutool.core.net.url.UrlBuilder; | 4 | import cn.hutool.core.net.url.UrlBuilder; |
| 6 | -import cn.hutool.core.util.ReflectUtil; | ||
| 7 | import cn.hutool.core.util.StrUtil; | 5 | import cn.hutool.core.util.StrUtil; |
| 8 | import cn.hutool.http.HttpRequest; | 6 | import cn.hutool.http.HttpRequest; |
| 9 | import cn.hutool.http.HttpResponse; | 7 | import cn.hutool.http.HttpResponse; |
| @@ -62,14 +60,10 @@ public class HttpUtils { | @@ -62,14 +60,10 @@ public class HttpUtils { | ||
| 62 | return URLDecoder.decode(encoded, StandardCharsets.UTF_8); | 60 | return URLDecoder.decode(encoded, StandardCharsets.UTF_8); |
| 63 | } | 61 | } |
| 64 | 62 | ||
| 65 | - @SuppressWarnings("unchecked") | ||
| 66 | public static String replaceUrlQuery(String url, String key, String value) { | 63 | public static String replaceUrlQuery(String url, String key, String value) { |
| 67 | UrlBuilder builder = UrlBuilder.of(url, Charset.defaultCharset()); | 64 | UrlBuilder builder = UrlBuilder.of(url, Charset.defaultCharset()); |
| 68 | - // 先移除 | ||
| 69 | - TableMap<CharSequence, CharSequence> query = (TableMap<CharSequence, CharSequence>) | ||
| 70 | - ReflectUtil.getFieldValue(builder.getQuery(), "query"); | ||
| 71 | - query.remove(key); | ||
| 72 | - // 后添加 | 65 | + // 先移除;再添加 |
| 66 | + builder.getQuery().remove(key); | ||
| 73 | builder.addQuery(key, value); | 67 | builder.addQuery(key, value); |
| 74 | return builder.build(); | 68 | return builder.build(); |
| 75 | } | 69 | } |
yudao-framework/yudao-common/src/test/java/cn/iocoder/yudao/framework/common/util/http/HttpUtilsTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.common.util.http; | ||
| 2 | + | ||
| 3 | +import org.junit.jupiter.api.Test; | ||
| 4 | + | ||
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * {@link HttpUtils} 的单元测试 | ||
| 9 | + */ | ||
| 10 | +public class HttpUtilsTest { | ||
| 11 | + | ||
| 12 | + @Test | ||
| 13 | + public void testReplaceUrlQuery_replace() { | ||
| 14 | + // 准备参数 | ||
| 15 | + String url = "https://www.iocoder.cn/path?a=1&b=2"; | ||
| 16 | + // 调用 | ||
| 17 | + String result = HttpUtils.replaceUrlQuery(url, "a", "3"); | ||
| 18 | + // 断言:被替换的 key 会移到末尾,原顺序的其它参数保留 | ||
| 19 | + assertEquals("https://www.iocoder.cn/path?b=2&a=3", result); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + @Test | ||
| 23 | + public void testReplaceUrlQuery_add() { | ||
| 24 | + // 准备参数 | ||
| 25 | + String url = "https://www.iocoder.cn/path?a=1"; | ||
| 26 | + // 调用 | ||
| 27 | + String result = HttpUtils.replaceUrlQuery(url, "b", "2"); | ||
| 28 | + // 断言 | ||
| 29 | + assertEquals("https://www.iocoder.cn/path?a=1&b=2", result); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + @Test | ||
| 33 | + public void testReplaceUrlQuery_noQuery() { | ||
| 34 | + // 准备参数:原 URL 没有 query | ||
| 35 | + String url = "https://www.iocoder.cn/path"; | ||
| 36 | + // 调用 | ||
| 37 | + String result = HttpUtils.replaceUrlQuery(url, "a", "1"); | ||
| 38 | + // 断言 | ||
| 39 | + assertEquals("https://www.iocoder.cn/path?a=1", result); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + @Test | ||
| 43 | + public void testReplaceUrlQuery_emptyValue() { | ||
| 44 | + // 准备参数:value 为空字符串 | ||
| 45 | + String url = "https://www.iocoder.cn/path?a=1"; | ||
| 46 | + // 调用 | ||
| 47 | + String result = HttpUtils.replaceUrlQuery(url, "a", ""); | ||
| 48 | + // 断言:保留 key,value 为空 | ||
| 49 | + assertEquals("https://www.iocoder.cn/path?a=", result); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | +} |