改进ContentType.get忽略空格

This commit is contained in:
Looly 2024-07-31 00:23:59 +08:00
parent 0e402d2d2b
commit ae3e6d4fad
2 changed files with 14 additions and 2 deletions

View File

@ -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 '[':

View File

@ -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);
}
}