mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
改进ContentType.get忽略空格
This commit is contained in:
parent
0e402d2d2b
commit
ae3e6d4fad
@ -132,7 +132,7 @@ public enum ContentType {
|
||||
public static ContentType get(final String body) {
|
||||
ContentType contentType = null;
|
||||
if (StrUtil.isNotBlank(body)) {
|
||||
final char firstChar = body.charAt(0);
|
||||
final char firstChar = StrUtil.trimPrefix(body).charAt(0);
|
||||
switch (firstChar) {
|
||||
case '{':
|
||||
case '[':
|
||||
|
@ -14,9 +14,12 @@ package org.dromara.hutool.http;
|
||||
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.dromara.hutool.http.meta.ContentType;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* ContentType 单元测试
|
||||
*
|
||||
@ -26,6 +29,15 @@ public class ContentTypeTest {
|
||||
@Test
|
||||
public void testBuild() {
|
||||
final String result = ContentType.build(ContentType.JSON, CharsetUtil.UTF_8);
|
||||
Assertions.assertEquals("application/json;charset=UTF-8", result);
|
||||
assertEquals("application/json;charset=UTF-8", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetWithLeadingSpace() {
|
||||
final String json = " {\n" +
|
||||
" \"name\": \"hutool\"\n" +
|
||||
" }";
|
||||
final ContentType contentType = ContentType.get(json);
|
||||
assertEquals(ContentType.JSON, contentType);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user