From 2faebb9beac8ea5f82c3fd63a51e06f87a9fb6a2 Mon Sep 17 00:00:00 2001 From: liam Date: Tue, 18 Apr 2023 15:58:24 +0800 Subject: [PATCH] =?UTF-8?q?test(UrlDecoderTest):=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加一些单元测试 --- .../hutool/core/net/UrlDecoderTest.java | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/net/UrlDecoderTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/net/UrlDecoderTest.java index 1dffef709..17c9c376d 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/net/UrlDecoderTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/net/UrlDecoderTest.java @@ -9,10 +9,10 @@ import org.junit.jupiter.api.Test; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; -public class UrlDecoderTest { +class UrlDecoderTest { @Test - void decodeForPathTest(){ + void decodeForPathTest() { Assertions.assertEquals("+", URLDecoder.decodeForPath("+", CharsetUtil.UTF_8)); } @@ -41,4 +41,49 @@ public class UrlDecoderTest { URLDecoder.decode(mixDecoded, StandardCharsets.UTF_16) ); } + + @Test + void decodeCharSetIsNullToStrTest() { + final String hello = "你好"; + String decode = URLDecoder.decode(hello, null, true); + Assertions.assertEquals(hello, decode); + } + + @Test + void decodeStrIsEmptyToStrTest() { + final String strEmpty = ""; + String decode = URLDecoder.decode(strEmpty, StandardCharsets.UTF_8, true); + Assertions.assertEquals(strEmpty, decode); + } + + @Test + void decodeStrWithUTF8ToStrTest() { + final String exceptedDecode = "你好"; + final String encode = "%E4%BD%A0%E5%A5%BD"; + String s1 = URLDecoder.decode(encode); + Assertions.assertEquals(exceptedDecode, s1); + + String s2 = URLDecoder.decode(encode, StandardCharsets.UTF_8); + Assertions.assertEquals(exceptedDecode, s2); + + String s3 = URLDecoder.decode(encode, true); + Assertions.assertEquals(exceptedDecode, s3); + + String s4 = URLDecoder.decode(encode + "+", false); + Assertions.assertEquals(exceptedDecode + "+", s4); + + String s5 = URLDecoder.decode(encode, StandardCharsets.UTF_8, false); + Assertions.assertEquals(exceptedDecode, s5); + } + + @Test + void decodeStrWithUTF8ToByteTest(){ + final String exceptedDecode = "你好"; + final String encode = "%E4%BD%A0%E5%A5%BD"; + byte[] decode = URLDecoder.decode(encode.getBytes(StandardCharsets.UTF_8)); + Assertions.assertEquals(exceptedDecode, new String(decode,StandardCharsets.UTF_8)); + + byte[] decode1 = URLDecoder.decode((encode + "+").getBytes(StandardCharsets.UTF_8)); + Assertions.assertEquals(exceptedDecode+" ",new String(decode1,StandardCharsets.UTF_8)); + } }