From f3c29fe2e6d2b1b1da4acc09d00bf2c67f10ff3e Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Tue, 20 Nov 2018 10:51:19 +0100 Subject: [PATCH] Add test for UriUtils.encode(String, Charset) Issue: SPR-17451 --- .../web/util/UriUtilsTests.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java b/spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java index 7be96561219..dfed10b24a6 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.springframework.web.util; -import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -35,43 +34,43 @@ public class UriUtilsTests { @Test - public void encodeScheme() throws UnsupportedEncodingException { + public void encodeScheme() { assertEquals("Invalid encoded result", "foobar+-.", UriUtils.encodeScheme("foobar+-.", CHARSET)); assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeScheme("foo bar", CHARSET)); } @Test - public void encodeUserInfo() throws UnsupportedEncodingException { + public void encodeUserInfo() { assertEquals("Invalid encoded result", "foobar:", UriUtils.encodeUserInfo("foobar:", CHARSET)); assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeUserInfo("foo bar", CHARSET)); } @Test - public void encodeHost() throws UnsupportedEncodingException { + public void encodeHost() { assertEquals("Invalid encoded result", "foobar", UriUtils.encodeHost("foobar", CHARSET)); assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeHost("foo bar", CHARSET)); } @Test - public void encodePort() throws UnsupportedEncodingException { + public void encodePort() { assertEquals("Invalid encoded result", "80", UriUtils.encodePort("80", CHARSET)); } @Test - public void encodePath() throws UnsupportedEncodingException { + public void encodePath() { assertEquals("Invalid encoded result", "/foo/bar", UriUtils.encodePath("/foo/bar", CHARSET)); assertEquals("Invalid encoded result", "/foo%20bar", UriUtils.encodePath("/foo bar", CHARSET)); assertEquals("Invalid encoded result", "/Z%C3%BCrich", UriUtils.encodePath("/Z\u00fcrich", CHARSET)); } @Test - public void encodePathSegment() throws UnsupportedEncodingException { + public void encodePathSegment() { assertEquals("Invalid encoded result", "foobar", UriUtils.encodePathSegment("foobar", CHARSET)); assertEquals("Invalid encoded result", "%2Ffoo%2Fbar", UriUtils.encodePathSegment("/foo/bar", CHARSET)); } @Test - public void encodeQuery() throws UnsupportedEncodingException { + public void encodeQuery() { assertEquals("Invalid encoded result", "foobar", UriUtils.encodeQuery("foobar", CHARSET)); assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeQuery("foo bar", CHARSET)); assertEquals("Invalid encoded result", "foobar/+", UriUtils.encodeQuery("foobar/+", CHARSET)); @@ -79,21 +78,28 @@ public class UriUtilsTests { } @Test - public void encodeQueryParam() throws UnsupportedEncodingException { + public void encodeQueryParam() { assertEquals("Invalid encoded result", "foobar", UriUtils.encodeQueryParam("foobar", CHARSET)); assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeQueryParam("foo bar", CHARSET)); assertEquals("Invalid encoded result", "foo%26bar", UriUtils.encodeQueryParam("foo&bar", CHARSET)); } @Test - public void encodeFragment() throws UnsupportedEncodingException { + public void encodeFragment() { assertEquals("Invalid encoded result", "foobar", UriUtils.encodeFragment("foobar", CHARSET)); assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeFragment("foo bar", CHARSET)); assertEquals("Invalid encoded result", "foobar/", UriUtils.encodeFragment("foobar/", CHARSET)); } @Test - public void decode() throws UnsupportedEncodingException { + public void encode() { + assertEquals("Invalid encoded result", "foo", UriUtils.encode("foo", CHARSET)); + assertEquals("Invalid encoded result", "http%3A%2F%2Fexample.com%2Ffoo%20bar", + UriUtils.encode("http://example.com/foo bar", CHARSET)); + } + + @Test + public void decode() { assertEquals("Invalid encoded URI", "", UriUtils.decode("", CHARSET)); assertEquals("Invalid encoded URI", "foobar", UriUtils.decode("foobar", CHARSET)); assertEquals("Invalid encoded URI", "foo bar", UriUtils.decode("foo%20bar", CHARSET)); @@ -104,7 +110,7 @@ public class UriUtilsTests { } @Test(expected = IllegalArgumentException.class) - public void decodeInvalidSequence() throws UnsupportedEncodingException { + public void decodeInvalidSequence() { UriUtils.decode("foo%2", CHARSET); }