diff options
Diffstat (limited to 'spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java')
-rw-r--r-- | spring-web/src/test/java/org/springframework/web/util/DefaultUriTemplateHandlerTests.java | 115 |
1 files changed, 88 insertions, 27 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()); } } |