From ae3e6d4fad6429e2151c08e3eda07bb37bf09b93 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 31 Jul 2024 00:23:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BContentType.get=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/hutool/http/meta/ContentType.java | 2 +- .../org/dromara/hutool/http/ContentTypeTest.java | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/meta/ContentType.java b/hutool-http/src/main/java/org/dromara/hutool/http/meta/ContentType.java index fcc7a56c9..9a822597b 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/meta/ContentType.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/meta/ContentType.java @@ -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 '[': diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/ContentTypeTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/ContentTypeTest.java index 48a294fb5..2e1678446 100644 --- a/hutool-http/src/test/java/org/dromara/hutool/http/ContentTypeTest.java +++ b/hutool-http/src/test/java/org/dromara/hutool/http/ContentTypeTest.java @@ -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); } }