@@ -58,10 +58,10 @@ public void givenRequestParam_whenUTF8Scheme_thenEncode() throws Exception {
5858 requestParams .put ("key2" , "value@!$2" );
5959 requestParams .put ("key3" , "value%3" );
6060
61- String path = "path 1" ;
61+ String path = "path+ 1" ;
6262 String fragment = "dummy Fragment" ;
6363
64- String encodedURL = requestParams .keySet ().stream ().map (key -> key + "=" + encodeValue (requestParams .get (key ))).collect (joining ("&" , "http://www.baeldung.com/" + encodeValue (path ) + "?" , "#" + encodeValue (fragment )));
64+ String encodedURL = requestParams .keySet ().stream ().map (key -> key + "=" + encodeValue (requestParams .get (key ))).collect (joining ("&" , "http://www.baeldung.com/" + getPath (path ) + "?" , "#" + encodeValue (fragment )));
6565
6666 Assert .assertThat (testUrl , CoreMatchers .is (encodedURL ));
6767 }
@@ -77,8 +77,25 @@ public void givenRequestParam_whenUTF8Scheme_thenDecodeRequestParams() throws Ex
7777 String fragment = uri .getRawFragment ();
7878
7979 String decodedQuery = Arrays .stream (query .split ("&" )).map (param -> param .split ("=" )[0 ] + "=" + decode (param .split ("=" )[1 ])).collect (joining ("&" ));
80- String decodedPath = Arrays .stream (path .split ("/" )).map (param -> decode (param )).collect (joining ("/" ));
80+ String decodedPath = Arrays .stream (path .split ("/" )).map (param -> getPath (param )).collect (joining ("/" ));
8181
82- Assert .assertEquals ("http://www.baeldung.com/path 1?key1=value 1&key2=value@!$2&key3=value%3#dummy Fragment" , scheme + "://" + host + decodedPath + "?" + decodedQuery + "#" + decode (fragment ));
82+ Assert .assertEquals ("http://www.baeldung.com/path+1?key1=value 1&key2=value@!$2&key3=value%3#dummy Fragment" , scheme + "://" + host + decodedPath + "?" + decodedQuery + "#" + decode (fragment ));
83+ }
84+
85+ private String getPath (String path ) {
86+ try {
87+ path = new URI (null , null , path , null ).getPath ();
88+ } catch (URISyntaxException e ) {
89+ e .printStackTrace ();
90+ }
91+ return path ;
92+ }
93+
94+ @ Test
95+ public void givenPath_thenEncodeDecodePath () throws URISyntaxException {
96+ URI uri = new URI (null , null , "/Path 1/Path+2" , null );
97+
98+ Assert .assertEquals ("/Path 1/Path+2" , uri .getPath ());
99+ Assert .assertEquals ("/Path%201/Path+2" , uri .getRawPath ());
83100 }
84101}
0 commit comments