diff options
Diffstat (limited to 'spring-web/src/test/java/org/springframework/web/util')
4 files changed, 154 insertions, 62 deletions
diff --git a/spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java b/spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java index 1642351e..09f3700a 100644 --- a/spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -15,74 +15,135 @@ */ package org.springframework.web.util; -import static org.junit.Assert.assertEquals; - import java.net.URI; import java.util.HashMap; import java.util.Map; -import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * Unit tests for {@link DefaultUriTemplateHandler}. + * * @author Rossen Stoyanchev */ public class DefaultUriTemplateHandlerTests { - private DefaultUriTemplateHandler handler; - - - @Before - public void setUp() throws Exception { - this.handler = new DefaultUriTemplateHandler(); - } + private final DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); @Test - public void baseUrl() throws Exception { + public void baseUrlWithoutPath() throws Exception { this.handler.setBaseUrl("http://localhost:8080"); URI actual = this.handler.expand("/myapiresource"); - URI expected = new URI("http://localhost:8080/myapiresource"); - assertEquals(expected, actual); + assertEquals("http://localhost:8080/myapiresource", actual.toString()); } @Test - public void baseUrlWithPartialPath() throws Exception { + public void baseUrlWithPath() throws Exception { this.handler.setBaseUrl("http://localhost:8080/context"); URI actual = this.handler.expand("/myapiresource"); - URI expected = new URI("http://localhost:8080/context/myapiresource"); - assertEquals(expected, actual); + assertEquals("http://localhost:8080/context/myapiresource", actual.toString()); + } + + @Test // SPR-14147 + public void defaultUriVariables() throws Exception { + Map<String, String> defaultVars = new HashMap<>(2); + defaultVars.put("host", "api.example.com"); + defaultVars.put("port", "443"); + this.handler.setDefaultUriVariables(defaultVars); + + Map<String, Object> vars = new HashMap<>(1); + vars.put("id", 123L); + + String template = "https://{host}:{port}/v42/customers/{id}"; + URI actual = this.handler.expand(template, vars); + + assertEquals("https://api.example.com:443/v42/customers/123", actual.toString()); } @Test - public void expandWithFullPath() throws Exception { - Map<String, String> vars = new HashMap<String, String>(2); + public void parsePathIsOff() throws Exception { + this.handler.setParsePath(false); + Map<String, String> vars = new HashMap<>(2); vars.put("hotel", "1"); vars.put("publicpath", "pics/logo.png"); String template = "http://example.com/hotels/{hotel}/pic/{publicpath}"; - URI actual = this.handler.expand(template, vars); - URI expected = new URI("http://example.com/hotels/1/pic/pics/logo.png"); - assertEquals(expected, actual); + assertEquals("http://example.com/hotels/1/pic/pics/logo.png", actual.toString()); } @Test - public void expandWithFullPathAndParsePathEnabled() throws Exception { - Map<String, String> vars = new HashMap<String, String>(2); + public void parsePathIsOn() throws Exception { + this.handler.setParsePath(true); + Map<String, String> vars = new HashMap<>(2); vars.put("hotel", "1"); vars.put("publicpath", "pics/logo.png"); vars.put("scale", "150x150"); String template = "http://example.com/hotels/{hotel}/pic/{publicpath}/size/{scale}"; + URI actual = this.handler.expand(template, vars); - this.handler.setParsePath(true); + assertEquals("http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150", actual.toString()); + } + + @Test + public void strictEncodingIsOffWithMap() throws Exception { + this.handler.setStrictEncoding(false); + Map<String, String> vars = new HashMap<>(2); + vars.put("userId", "john;doe"); + String template = "http://www.example.com/user/{userId}/dashboard"; + URI actual = this.handler.expand(template, vars); + + assertEquals("http://www.example.com/user/john;doe/dashboard", actual.toString()); + } + + @Test + public void strictEncodingOffWithArray() throws Exception { + this.handler.setStrictEncoding(false); + String template = "http://www.example.com/user/{userId}/dashboard"; + URI actual = this.handler.expand(template, "john;doe"); + + assertEquals("http://www.example.com/user/john;doe/dashboard", actual.toString()); + } + + @Test + public void strictEncodingOnWithMap() throws Exception { + this.handler.setStrictEncoding(true); + Map<String, String> vars = new HashMap<>(2); + vars.put("userId", "john;doe"); + String template = "http://www.example.com/user/{userId}/dashboard"; + URI actual = this.handler.expand(template, vars); + + assertEquals("http://www.example.com/user/john%3Bdoe/dashboard", actual.toString()); + } + + @Test + public void strictEncodingOnWithArray() throws Exception { + this.handler.setStrictEncoding(true); + String template = "http://www.example.com/user/{userId}/dashboard"; + URI actual = this.handler.expand(template, "john;doe"); + + assertEquals("http://www.example.com/user/john%3Bdoe/dashboard", actual.toString()); + } + + @Test // SPR-14147 + public void strictEncodingAndDefaultUriVariables() throws Exception { + Map<String, String> defaultVars = new HashMap<>(1); + defaultVars.put("host", "www.example.com"); + this.handler.setDefaultUriVariables(defaultVars); + this.handler.setStrictEncoding(true); + + Map<String, Object> vars = new HashMap<>(1); + vars.put("userId", "john;doe"); + + String template = "http://{host}/user/{userId}/dashboard"; URI actual = this.handler.expand(template, vars); - URI expected = new URI("http://example.com/hotels/1/pic/pics%2Flogo.png/size/150x150"); - assertEquals(expected, actual); + assertEquals("http://www.example.com/user/john%3Bdoe/dashboard", actual.toString()); } } diff --git a/spring-web/src/test/java/org/springframework/web/util/Log4jWebConfigurerTests.java b/spring-web/src/test/java/org/springframework/web/util/Log4jWebConfigurerTests.java index 5001f9fb..c32405e4 100644 --- a/spring-web/src/test/java/org/springframework/web/util/Log4jWebConfigurerTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/Log4jWebConfigurerTests.java @@ -101,7 +101,8 @@ public class Log4jWebConfigurerTests { try { assertLogOutput(); - } finally { + } + finally { Log4jWebConfigurer.shutdownLogging(sc); } assertTrue(MockLog4jAppender.closeCalled); @@ -132,7 +133,8 @@ public class Log4jWebConfigurerTests { try { assertLogOutput(); - } finally { + } + finally { listener.contextDestroyed(new ServletContextEvent(sc)); } assertTrue(MockLog4jAppender.closeCalled); diff --git a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java index a047af03..99c642f0 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -30,116 +30,126 @@ import static org.junit.Assert.*; /** * @author Arjen Poutsma * @author Juergen Hoeller + * @author Rossen Stoyanchev */ public class UriTemplateTests { @Test public void getVariableNames() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); List<String> variableNames = template.getVariableNames(); assertEquals("Invalid variable names", Arrays.asList("hotel", "booking"), variableNames); } @Test public void expandVarArgs() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand("1", "42"); - assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result); + assertEquals("Invalid expanded template", new URI("/hotels/1/bookings/42"), result); + } + + // SPR-9712 + + @Test + public void expandVarArgsWithArrayValue() throws Exception { + UriTemplate template = new UriTemplate("/sum?numbers={numbers}"); + URI result = template.expand(new int[] {1, 2, 3}); + assertEquals(new URI("/sum?numbers=1,2,3"), result); } @Test(expected = IllegalArgumentException.class) public void expandVarArgsNotEnoughVariables() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); template.expand("1"); } @Test public void expandMap() throws Exception { - Map<String, String> uriVariables = new HashMap<String, String>(2); + Map<String, String> uriVariables = new HashMap<>(2); uriVariables.put("booking", "42"); uriVariables.put("hotel", "1"); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand(uriVariables); - assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result); + assertEquals("Invalid expanded template", new URI("/hotels/1/bookings/42"), result); } @Test public void expandMapDuplicateVariables() throws Exception { UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}"); - assertEquals("Invalid variable names", Arrays.asList("c", "c", "c"), template.getVariableNames()); + assertEquals(Arrays.asList("c", "c", "c"), template.getVariableNames()); URI result = template.expand(Collections.singletonMap("c", "cheeseburger")); - assertEquals("Invalid expanded template", new URI("/order/cheeseburger/cheeseburger/cheeseburger"), result); + assertEquals(new URI("/order/cheeseburger/cheeseburger/cheeseburger"), result); } @Test public void expandMapNonString() throws Exception { - Map<String, Integer> uriVariables = new HashMap<String, Integer>(2); + Map<String, Integer> uriVariables = new HashMap<>(2); uriVariables.put("booking", 42); uriVariables.put("hotel", 1); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); URI result = template.expand(uriVariables); - assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result); + assertEquals("Invalid expanded template", new URI("/hotels/1/bookings/42"), result); } @Test public void expandMapEncoded() throws Exception { Map<String, String> uriVariables = Collections.singletonMap("hotel", "Z\u00fcrich"); - UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}"); + UriTemplate template = new UriTemplate("/hotel list/{hotel}"); URI result = template.expand(uriVariables); - assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result); + assertEquals("Invalid expanded template", new URI("/hotel%20list/Z%C3%BCrich"), result); } @Test(expected = IllegalArgumentException.class) public void expandMapUnboundVariables() throws Exception { - Map<String, String> uriVariables = new HashMap<String, String>(2); + Map<String, String> uriVariables = new HashMap<>(2); uriVariables.put("booking", "42"); uriVariables.put("bar", "1"); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); template.expand(uriVariables); } @Test public void expandEncoded() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}"); + UriTemplate template = new UriTemplate("/hotel list/{hotel}"); URI result = template.expand("Z\u00fcrich"); - assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result); + assertEquals("Invalid expanded template", new URI("/hotel%20list/Z%C3%BCrich"), result); } @Test public void matches() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); - assertTrue("UriTemplate does not match", template.matches("http://example.com/hotels/1/bookings/42")); - assertFalse("UriTemplate matches", template.matches("http://example.com/hotels/bookings")); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); + assertTrue("UriTemplate does not match", template.matches("/hotels/1/bookings/42")); + assertFalse("UriTemplate matches", template.matches("/hotels/bookings")); assertFalse("UriTemplate matches", template.matches("")); assertFalse("UriTemplate matches", template.matches(null)); } @Test public void matchesCustomRegex() throws Exception { - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel:\\d+}"); - assertTrue("UriTemplate does not match", template.matches("http://example.com/hotels/42")); - assertFalse("UriTemplate matches", template.matches("http://example.com/hotels/foo")); + UriTemplate template = new UriTemplate("/hotels/{hotel:\\d+}"); + assertTrue("UriTemplate does not match", template.matches("/hotels/42")); + assertFalse("UriTemplate matches", template.matches("/hotels/foo")); } @Test public void match() throws Exception { - Map<String, String> expected = new HashMap<String, String>(2); + Map<String, String> expected = new HashMap<>(2); expected.put("booking", "42"); expected.put("hotel", "1"); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); - Map<String, String> result = template.match("http://example.com/hotels/1/bookings/42"); + UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); + Map<String, String> result = template.match("/hotels/1/bookings/42"); assertEquals("Invalid match", expected, result); } @Test public void matchCustomRegex() throws Exception { - Map<String, String> expected = new HashMap<String, String>(2); + Map<String, String> expected = new HashMap<>(2); expected.put("booking", "42"); expected.put("hotel", "1"); - UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel:\\d}/bookings/{booking:\\d+}"); - Map<String, String> result = template.match("http://example.com/hotels/1/bookings/42"); + UriTemplate template = new UriTemplate("/hotels/{hotel:\\d}/bookings/{booking:\\d+}"); + Map<String, String> result = template.match("/hotels/1/bookings/42"); assertEquals("Invalid match", expected, result); } @@ -164,7 +174,7 @@ public class UriTemplateTests { public void matchMultipleInOneSegment() throws Exception { UriTemplate template = new UriTemplate("/{foo}-{bar}"); Map<String, String> result = template.match("/12-34"); - Map<String, String> expected = new HashMap<String, String>(2); + Map<String, String> expected = new HashMap<>(2); expected.put("foo", "12"); expected.put("bar", "34"); assertEquals("Invalid match", expected, result); 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 6480c5fa..2227674a 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-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -24,6 +24,7 @@ import static org.junit.Assert.*; /** * @author Arjen Poutsma + * @author Juergen Hoeller */ public class UriUtilsTests { @@ -104,4 +105,22 @@ public class UriUtilsTests { UriUtils.decode("foo%2", ENC); } + @Test + public void extractFileExtension() { + assertEquals("html", UriUtils.extractFileExtension("index.html")); + assertEquals("html", UriUtils.extractFileExtension("/index.html")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/a")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/path/a")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html#/path/a.do")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=a")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a.do")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a#/path/a")); + assertEquals("html", UriUtils.extractFileExtension("/products/view.html?param=/path/a.do#/path/a.do")); + assertEquals("html", UriUtils.extractFileExtension("/products;q=11/view.html?param=/path/a.do")); + assertEquals("html", UriUtils.extractFileExtension("/products;q=11/view.html;r=22?param=/path/a.do")); + assertEquals("html", UriUtils.extractFileExtension("/products;q=11/view.html;r=22;s=33?param=/path/a.do")); + } + } |