UriTemplateTests.java 7.6 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2015 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * 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.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

A
Arjen Poutsma 已提交
28 29
import static org.junit.Assert.*;

30 31 32 33
/**
 * @author Arjen Poutsma
 * @author Juergen Hoeller
 */
A
Arjen Poutsma 已提交
34 35 36 37
public class UriTemplateTests {

	@Test
	public void getVariableNames() throws Exception {
38
		UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
A
Arjen Poutsma 已提交
39 40 41 42 43 44
		List<String> variableNames = template.getVariableNames();
		assertEquals("Invalid variable names", Arrays.asList("hotel", "booking"), variableNames);
	}

	@Test
	public void expandVarArgs() throws Exception {
45
		UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
A
Arjen Poutsma 已提交
46 47 48 49 50
		URI result = template.expand("1", "42");
		assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
	}

	@Test(expected = IllegalArgumentException.class)
A
Arjen Poutsma 已提交
51
	public void expandVarArgsNotEnoughVariables() throws Exception {
52
		UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
A
Arjen Poutsma 已提交
53
		template.expand("1");
A
Arjen Poutsma 已提交
54 55
	}

P
Phillip Webb 已提交
56 57 58 59 60 61 62 63 64
	@Test
	public void expandMap() throws Exception {
		Map<String, String> uriVariables = new HashMap<String, String>(2);
		uriVariables.put("booking", "42");
		uriVariables.put("hotel", "1");
		UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
		URI result = template.expand(uriVariables);
		assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
	}
65

A
Arjen Poutsma 已提交
66 67
	@Test
	public void expandMapDuplicateVariables() throws Exception {
68
		UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
A
Arjen Poutsma 已提交
69 70 71 72 73
		assertEquals("Invalid variable names", 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);
	}

A
Arjen Poutsma 已提交
74 75 76 77 78 79 80 81 82
	@Test
	public void expandMapNonString() throws Exception {
		Map<String, Integer> uriVariables = new HashMap<String, Integer>(2);
		uriVariables.put("booking", 42);
		uriVariables.put("hotel", 1);
		UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
		URI result = template.expand(uriVariables);
		assertEquals("Invalid expanded template", new URI("http://example.com/hotels/1/bookings/42"), result);
	}
83

P
Phillip Webb 已提交
84 85 86 87 88 89 90
	@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}");
		URI result = template.expand(uriVariables);
		assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result);
	}
91

92 93 94 95 96 97 98 99 100
	@Test(expected = IllegalArgumentException.class)
	public void expandMapUnboundVariables() throws Exception {
		Map<String, String> uriVariables = new HashMap<String, String>(2);
		uriVariables.put("booking", "42");
		uriVariables.put("bar", "1");
		UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
		template.expand(uriVariables);
	}

101 102
	@Test
	public void expandEncoded() throws Exception {
103
		UriTemplate template = new UriTemplate("http://example.com/hotel list/{hotel}");
104
		URI result = template.expand("Z\u00fcrich");
105
		assertEquals("Invalid expanded template", new URI("http://example.com/hotel%20list/Z%C3%BCrich"), result);
106 107
	}

A
Arjen Poutsma 已提交
108 109
	@Test
	public void matches() throws Exception {
110
		UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
A
Arjen Poutsma 已提交
111 112 113 114 115
		assertTrue("UriTemplate does not match", template.matches("http://example.com/hotels/1/bookings/42"));
		assertFalse("UriTemplate matches", template.matches("http://example.com/hotels/bookings"));
		assertFalse("UriTemplate matches", template.matches(""));
		assertFalse("UriTemplate matches", template.matches(null));
	}
116

A
Arjen Poutsma 已提交
117 118 119 120 121 122
	@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"));
	}
A
Arjen Poutsma 已提交
123 124 125 126 127 128 129

	@Test
	public void match() throws Exception {
		Map<String, String> expected = new HashMap<String, String>(2);
		expected.put("booking", "42");
		expected.put("hotel", "1");

130
		UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
A
Arjen Poutsma 已提交
131 132 133 134
		Map<String, String> result = template.match("http://example.com/hotels/1/bookings/42");
		assertEquals("Invalid match", expected, result);
	}

A
Arjen Poutsma 已提交
135 136 137 138 139 140 141 142 143 144 145
	@Test
	public void matchCustomRegex() throws Exception {
		Map<String, String> expected = new HashMap<String, String>(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");
		assertEquals("Invalid match", expected, result);
	}

146 147 148 149 150 151 152 153 154
	// SPR-13627

	@Test
	public void matchCustomRegexWithNestedCurlyBraces() throws Exception {
		UriTemplate template = new UriTemplate("/site.{domain:co.[a-z]{2}}");
		Map<String, String> result = template.match("/site.co.eu");
		assertEquals("Invalid match", Collections.singletonMap("domain", "co.eu"), result);
	}

A
Arjen Poutsma 已提交
155 156
	@Test
	public void matchDuplicate() throws Exception {
157
		UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
A
Arjen Poutsma 已提交
158 159 160 161
		Map<String, String> result = template.match("/order/cheeseburger/cheeseburger/cheeseburger");
		Map<String, String> expected = Collections.singletonMap("c", "cheeseburger");
		assertEquals("Invalid match", expected, result);
	}
162 163 164

	@Test
	public void matchMultipleInOneSegment() throws Exception {
165
		UriTemplate template = new UriTemplate("/{foo}-{bar}");
166 167 168 169 170 171
		Map<String, String> result = template.match("/12-34");
		Map<String, String> expected = new HashMap<String, String>(2);
		expected.put("foo", "12");
		expected.put("bar", "34");
		assertEquals("Invalid match", expected, result);
	}
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

	@Test
	public void queryVariables() throws Exception {
		UriTemplate template = new UriTemplate("/search?q={query}");
		assertTrue(template.matches("/search?q=foo"));
	}

	@Test
	public void fragments() throws Exception {
		UriTemplate template = new UriTemplate("/search#{fragment}");
		assertTrue(template.matches("/search#foo"));

		template = new UriTemplate("/search?query={query}#{fragment}");
		assertTrue(template.matches("/search?query=foo#bar"));
	}
187 188 189 190 191 192 193 194

	@Test
	public void expandWithDollar() {
		UriTemplate template = new UriTemplate("/{a}");
		URI uri = template.expand("$replacement");
		assertEquals("/$replacement", uri.toString());
	}

195 196 197 198 199 200 201
	@Test
	public void expandWithAtSign() {
		UriTemplate template = new UriTemplate("http://localhost/query={query}");
		URI uri = template.expand("foo@bar");
		assertEquals("http://localhost/query=foo@bar", uri.toString());
	}

202
}