/
RegexPathSpecTest.java
158 lines (135 loc) · 6.06 KB
/
RegexPathSpecTest.java
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.http.pathmap;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class RegexPathSpecTest
{
public static void assertMatches(PathSpec spec, String path)
{
String msg = String.format("Spec(\"%s\").matches(\"%s\")", spec.getDeclaration(), path);
assertThat(msg, spec.matches(path), is(true));
}
public static void assertNotMatches(PathSpec spec, String path)
{
String msg = String.format("!Spec(\"%s\").matches(\"%s\")", spec.getDeclaration(), path);
assertThat(msg, spec.matches(path), is(false));
}
@Test
public void testExactSpec()
{
RegexPathSpec spec = new RegexPathSpec("^/a$");
assertEquals("^/a$", spec.getDeclaration(), "Spec.pathSpec");
assertEquals("^/a$", spec.getPattern().pattern(), "Spec.pattern");
assertEquals(1, spec.getPathDepth(), "Spec.pathDepth");
assertEquals(PathSpecGroup.EXACT, spec.getGroup(), "Spec.group");
assertMatches(spec, "/a");
assertNotMatches(spec, "/aa");
assertNotMatches(spec, "/a/");
}
@Test
public void testMiddleSpec()
{
RegexPathSpec spec = new RegexPathSpec("^/rest/([^/]*)/list$");
assertEquals("^/rest/([^/]*)/list$", spec.getDeclaration(), "Spec.pathSpec");
assertEquals("^/rest/([^/]*)/list$", spec.getPattern().pattern(), "Spec.pattern");
assertEquals(3, spec.getPathDepth(), "Spec.pathDepth");
assertEquals(PathSpecGroup.MIDDLE_GLOB, spec.getGroup(), "Spec.group");
assertMatches(spec, "/rest/api/list");
assertMatches(spec, "/rest/1.0/list");
assertMatches(spec, "/rest/2.0/list");
assertMatches(spec, "/rest/accounts/list");
assertNotMatches(spec, "/a");
assertNotMatches(spec, "/aa");
assertNotMatches(spec, "/aa/bb");
assertNotMatches(spec, "/rest/admin/delete");
assertNotMatches(spec, "/rest/list");
}
@Test
public void testPathInfo()
{
RegexPathSpec spec = new RegexPathSpec("^/test(/.*)?$");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test"));
assertThat(spec.getPathInfo("/test/info"), equalTo("/info"));
spec = new RegexPathSpec("^/[Tt]est(/.*)?$");
assertTrue(spec.matches("/test/info"));
assertThat(spec.getPathMatch("/test/info"), equalTo("/test/info"));
assertThat(spec.getPathInfo("/test/info"), nullValue());
}
@Test
public void testMiddleSpecNoGrouping()
{
RegexPathSpec spec = new RegexPathSpec("^/rest/[^/]+/list$");
assertEquals("^/rest/[^/]+/list$", spec.getDeclaration(), "Spec.pathSpec");
assertEquals("^/rest/[^/]+/list$", spec.getPattern().pattern(), "Spec.pattern");
assertEquals(3, spec.getPathDepth(), "Spec.pathDepth");
assertEquals(PathSpecGroup.MIDDLE_GLOB, spec.getGroup(), "Spec.group");
assertMatches(spec, "/rest/api/list");
assertMatches(spec, "/rest/1.0/list");
assertMatches(spec, "/rest/2.0/list");
assertMatches(spec, "/rest/accounts/list");
assertNotMatches(spec, "/a");
assertNotMatches(spec, "/aa");
assertNotMatches(spec, "/aa/bb");
assertNotMatches(spec, "/rest/admin/delete");
assertNotMatches(spec, "/rest/list");
}
@Test
public void testPrefixSpec()
{
RegexPathSpec spec = new RegexPathSpec("^/a/(.*)$");
assertEquals("^/a/(.*)$", spec.getDeclaration(), "Spec.pathSpec");
assertEquals("^/a/(.*)$", spec.getPattern().pattern(), "Spec.pattern");
assertEquals(2, spec.getPathDepth(), "Spec.pathDepth");
assertEquals(PathSpecGroup.PREFIX_GLOB, spec.getGroup(), "Spec.group");
assertMatches(spec, "/a/");
assertMatches(spec, "/a/b");
assertMatches(spec, "/a/b/c/d/e");
assertNotMatches(spec, "/a");
assertNotMatches(spec, "/aa");
assertNotMatches(spec, "/aa/bb");
}
@Test
public void testSuffixSpec()
{
RegexPathSpec spec = new RegexPathSpec("^(.*).do$");
assertEquals("^(.*).do$", spec.getDeclaration(), "Spec.pathSpec");
assertEquals("^(.*).do$", spec.getPattern().pattern(), "Spec.pattern");
assertEquals(0, spec.getPathDepth(), "Spec.pathDepth");
assertEquals(PathSpecGroup.SUFFIX_GLOB, spec.getGroup(), "Spec.group");
assertMatches(spec, "/a.do");
assertMatches(spec, "/a/b/c.do");
assertMatches(spec, "/abcde.do");
assertMatches(spec, "/abc/efg.do");
assertNotMatches(spec, "/a");
assertNotMatches(spec, "/aa");
assertNotMatches(spec, "/aa/bb");
assertNotMatches(spec, "/aa/bb.do/more");
}
@Test
public void testEquals()
{
assertThat(new RegexPathSpec("^(.*).do$"), equalTo(new RegexPathSpec("^(.*).do$")));
assertThat(new RegexPathSpec("/foo"), equalTo(new RegexPathSpec("/foo")));
assertThat(new RegexPathSpec("^(.*).do$"), not(equalTo(new RegexPathSpec("^(.*).gz$"))));
assertThat(new RegexPathSpec("^(.*).do$"), not(equalTo(new RegexPathSpec("^.*.do$"))));
assertThat(new RegexPathSpec("/foo"), not(equalTo(new ServletPathSpec("/foo"))));
}
}