mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
修复HttpUtil.normalizeParams
规则问题(issue#IBQIYQ@Gitee)
This commit is contained in:
parent
f11bd02037
commit
58c193cb48
@ -152,17 +152,17 @@ public class UrlQueryUtil {
|
||||
pos = i + 1;
|
||||
}
|
||||
} else if (c == '&') { // 参数对的分界点
|
||||
if (pos != i) {
|
||||
if (null == name) {
|
||||
// 对于像&a&这类无参数值的字符串,我们将name为a的值设为""
|
||||
if (null == name) {
|
||||
// 对于像&a&这类无参数值的字符串,我们将name为a的值设为""
|
||||
if (pos != i) {
|
||||
name = queryPart.substring(pos, i);
|
||||
builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=');
|
||||
} else {
|
||||
builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=')
|
||||
.append(RFC3986.QUERY_PARAM_VALUE.encode(queryPart.substring(pos, i), charset)).append('&');
|
||||
}
|
||||
name = null;
|
||||
} else {
|
||||
builder.append(RFC3986.QUERY_PARAM_NAME.encode(name, charset)).append('=')
|
||||
.append(RFC3986.QUERY_PARAM_VALUE.encode(queryPart.substring(pos, i), charset)).append('&');
|
||||
}
|
||||
name = null;
|
||||
pos = i + 1;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package org.dromara.hutool.http;
|
||||
|
||||
import org.dromara.hutool.core.net.url.UrlQueryUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class IssueIBQIYQTest {
|
||||
@Test
|
||||
void normalizeQueryTest() {
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", "");
|
||||
map.put("type", "4");
|
||||
String url = UrlQueryUtil.toQuery(map);
|
||||
Assertions.assertEquals("id=&type=4", url);
|
||||
url = UrlQueryUtil.normalizeQuery(url, null);
|
||||
Assertions.assertEquals("id=&type=4", url);
|
||||
}
|
||||
|
||||
@Test
|
||||
void normalizeQueryTest2() {
|
||||
final Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", "");
|
||||
map.put("type", "4");
|
||||
map.put("", "3");
|
||||
String url = UrlQueryUtil.toQuery(map);
|
||||
// 根据HTTP协议和URL规范(RFC 3986),name为空是合法的,例如:?value 或 ?=value
|
||||
Assertions.assertEquals("=3&id=&type=4", url);
|
||||
url = UrlQueryUtil.normalizeQuery(url, null);
|
||||
Assertions.assertEquals("=3&id=&type=4", url);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user