SPR-6291 - UrlPathHelper is too aggressive decoding URLs
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.Matcher;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class UriUtilsTest {
|
||||
|
||||
@Test
|
||||
public void encode() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded URI", "foobar", UriUtils.encode("foobar", "UTF-8"));
|
||||
assertEquals("Invalid encoded URI", "foo%20bar", UriUtils.encode("foo bar", "UTF-8"));
|
||||
assertEquals("Invalid encoded URI", "foo%2Bbar", UriUtils.encode("foo+bar", "UTF-8"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decode() throws UnsupportedEncodingException {
|
||||
assertEquals("Invalid encoded URI", "foobar", UriUtils.decode("foobar", "UTF-8"));
|
||||
assertEquals("Invalid encoded URI", "foo bar", UriUtils.decode("foo%20bar", "UTF-8"));
|
||||
assertEquals("Invalid encoded URI", "foo+bar", UriUtils.decode("foo%2bbar", "UTF-8"));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void decodeInvalidSequence() throws UnsupportedEncodingException {
|
||||
UriUtils.decode("foo%2", "UTF-8");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,44 +16,68 @@
|
||||
|
||||
package org.springframework.web.util;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.Before;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class UrlPathHelperTests extends TestCase {
|
||||
public class UrlPathHelperTests {
|
||||
|
||||
public void testGetPathWithinApplication() {
|
||||
UrlPathHelper helper = new UrlPathHelper();
|
||||
private UrlPathHelper helper;
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
helper = new UrlPathHelper();
|
||||
request = new MockHttpServletRequest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPathWithinApplication() {
|
||||
request.setContextPath("/petclinic");
|
||||
request.setRequestURI("/petclinic/welcome.html");
|
||||
|
||||
assertEquals("Incorrect path returned", "/welcome.html", helper.getPathWithinApplication(request));
|
||||
}
|
||||
|
||||
public void testGetPathWithinApplicationForRootWithNoLeadingSlash() {
|
||||
UrlPathHelper helper = new UrlPathHelper();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@Test
|
||||
public void getPathWithinApplicationForRootWithNoLeadingSlash() {
|
||||
request.setContextPath("/petclinic");
|
||||
request.setRequestURI("/petclinic");
|
||||
|
||||
assertEquals("Incorrect root path returned", "/", helper.getPathWithinApplication(request));
|
||||
}
|
||||
|
||||
public void testGetPathWithinApplicationForSlashContextPath() {
|
||||
UrlPathHelper helper = new UrlPathHelper();
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
@Test
|
||||
public void getPathWithinApplicationForSlashContextPath() {
|
||||
request.setContextPath("/");
|
||||
request.setRequestURI("/welcome.html");
|
||||
|
||||
assertEquals("Incorrect path returned", "/welcome.html", helper.getPathWithinApplication(request));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRequestUri() {
|
||||
request.setRequestURI("/welcome.html");
|
||||
assertEquals("Incorrect path returned", "/welcome.html", helper.getRequestUri(request));
|
||||
|
||||
request.setRequestURI("/foo%20bar");
|
||||
assertEquals("Incorrect path returned", "/foo bar", helper.getRequestUri(request));
|
||||
|
||||
request.setRequestURI("/foo+bar");
|
||||
assertEquals("Incorrect path returned", "/foo+bar", helper.getRequestUri(request));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user