View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.impl.routing;
29  
30  import java.util.Arrays;
31  import java.util.List;
32  import java.util.regex.Pattern;
33  
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  
37  public class TestUriPathRouter {
38  
39      @Test
40      public void testBestMatchWildCardMatching() throws Exception {
41          final UriPathRouter.BestMatcher<Long> matcher = new UriPathRouter.BestMatcher<>();
42          final List<PathRoute<String, Long>> routes = Arrays.asList(
43                  new PathRoute<>("*", 0L),
44                  new PathRoute<>("/one/*", 1L),
45                  new PathRoute<>("/one/two/*", 2L),
46                  new PathRoute<>("/one/two/three/*", 3L),
47                  new PathRoute<>("*.view", 4L),
48                  new PathRoute<>("*.form", 5L));
49  
50          Assertions.assertEquals(1L, matcher.apply("/one/request", routes));
51          Assertions.assertEquals(2L, matcher.apply("/one/two/request", routes));
52          Assertions.assertEquals(3L, matcher.apply("/one/two/three/request", routes));
53          Assertions.assertEquals(0L, matcher.apply("default/request", routes));
54          Assertions.assertEquals(4L, matcher.apply("that.view", routes));
55          Assertions.assertEquals(5L, matcher.apply("that.form", routes));
56          Assertions.assertEquals(0L, matcher.apply("whatever", routes));
57      }
58  
59      @Test
60      public void testBestMatchWildCardMatchingSuffixPrefixPrecedence() throws Exception {
61          final UriPathRouter.BestMatcher<Long> matcher = new UriPathRouter.BestMatcher<>();
62          final List<PathRoute<String, Long>> routes = Arrays.asList(
63                  new PathRoute<>("/ma*", 1L),
64                  new PathRoute<>("*tch", 2L));
65          Assertions.assertEquals(1L, matcher.apply("/match", routes));
66      }
67  
68      @Test
69      public void testBestMatchWildCardMatchingExactMatch() throws Exception {
70          final UriPathRouter.BestMatcher<Long> matcher = new UriPathRouter.BestMatcher<>();
71          final List<PathRoute<String, Long>> routes = Arrays.asList(
72                  new PathRoute<>("exact", 1L),
73                  new PathRoute<>("*", 0L));
74          Assertions.assertEquals(1L, matcher.apply("exact", routes));
75      }
76  
77      @Test
78      public void testBestMatchWildCardMatchingNoMatch() throws Exception {
79          final UriPathRouter.BestMatcher<Long> matcher = new UriPathRouter.BestMatcher<>();
80          final List<PathRoute<String, Long>> routes = Arrays.asList(
81                  new PathRoute<>("/this/*", 1L),
82                  new PathRoute<>("/that/*", 2L));
83          Assertions.assertNull(matcher.apply("huh?", routes));
84      }
85  
86      @Test
87      public void testOrderedWildCardMatching1() throws Exception {
88          final UriPathRouter.OrderedMatcher<Long> matcher = new UriPathRouter.OrderedMatcher<>();
89          final List<PathRoute<String, Long>> routes = Arrays.asList(
90                  new PathRoute<>("*", 0L),
91                  new PathRoute<>("/one/*", 1L),
92                  new PathRoute<>("/one/two/*", 2L),
93                  new PathRoute<>("/one/two/three/*", 3L),
94                  new PathRoute<>("*.view", 4L),
95                  new PathRoute<>("*.form", 5L));
96  
97          Assertions.assertEquals(0L, matcher.apply("/one/request", routes));
98          Assertions.assertEquals(0L, matcher.apply("/one/two/request", routes));
99          Assertions.assertEquals(0L, matcher.apply("/one/two/three/request", routes));
100         Assertions.assertEquals(0L, matcher.apply("default/request", routes));
101         Assertions.assertEquals(0L, matcher.apply("that.view", routes));
102         Assertions.assertEquals(0L, matcher.apply("that.form", routes));
103         Assertions.assertEquals(0L, matcher.apply("whatever", routes));
104 
105         final List<PathRoute<String, Long>> routes2 = Arrays.asList(
106                 new PathRoute<>("/one/two/three/*", 3L),
107                 new PathRoute<>("/one/two/*", 2L),
108                 new PathRoute<>("/one/*", 1L),
109                 new PathRoute<>("*.view", 4L),
110                 new PathRoute<>("*.form", 5L),
111                 new PathRoute<>("*", 0L));
112 
113         Assertions.assertEquals(3L, matcher.apply("/one/two/three/request", routes2));
114         Assertions.assertEquals(2L, matcher.apply("/one/two/request", routes2));
115         Assertions.assertEquals(1L, matcher.apply("/one/request", routes2));
116         Assertions.assertEquals(0L, matcher.apply("default/request", routes2));
117         Assertions.assertEquals(4L, matcher.apply("that.view", routes2));
118         Assertions.assertEquals(5L, matcher.apply("that.form", routes2));
119         Assertions.assertEquals(0L, matcher.apply("whatever", routes2));
120     }
121 
122     @Test
123     public void testOrderedWildCardMatchingSuffixPrefixPrecedence() throws Exception {
124         final UriPathRouter.OrderedMatcher<Long> matcher = new UriPathRouter.OrderedMatcher<>();
125         final List<PathRoute<String, Long>> routes = Arrays.asList(
126                 new PathRoute<>("/ma*", 1L),
127                 new PathRoute<>("*tch", 2L));
128         Assertions.assertEquals(1L, matcher.apply("/match", routes));
129     }
130 
131     @Test
132     public void testOrderedStarAndExact() {
133         final UriPathRouter.OrderedMatcher<Long> matcher = new UriPathRouter.OrderedMatcher<>();
134         final List<PathRoute<String, Long>> routes = Arrays.asList(
135                 new PathRoute<>("*", 0L),
136                 new PathRoute<>("exact", 1L));
137         Assertions.assertEquals(0L, matcher.apply("exact", routes));
138     }
139 
140     @Test
141     public void testOrderedExactAndStar() {
142         final UriPathRouter.OrderedMatcher<Long> matcher = new UriPathRouter.OrderedMatcher<>();
143         final List<PathRoute<String, Long>> routes = Arrays.asList(
144                 new PathRoute<>("exact", 1L),
145                 new PathRoute<>("*", 0L));
146         Assertions.assertEquals(1L, matcher.apply("exact", routes));
147     }
148 
149     @Test
150     public void testRegExMatching() throws Exception {
151         final UriPathRouter.RegexMatcher<Long> matcher = new UriPathRouter.RegexMatcher<>();
152         final List<PathRoute<Pattern, Long>> routes = Arrays.asList(
153                 new PathRoute<>(Pattern.compile("/one/two/three/.*"), 3L),
154                 new PathRoute<>(Pattern.compile("/one/two/.*"), 2L),
155                 new PathRoute<>(Pattern.compile("/one/.*"), 1L),
156                 new PathRoute<>(Pattern.compile(".*\\.view"), 4L),
157                 new PathRoute<>(Pattern.compile(".*\\.form"), 5L),
158                 new PathRoute<>(Pattern.compile(".*"), 0L));
159 
160         Assertions.assertEquals(1L, matcher.apply("/one/request", routes));
161         Assertions.assertEquals(2L, matcher.apply("/one/two/request", routes));
162         Assertions.assertEquals(3L, matcher.apply("/one/two/three/request", routes));
163         Assertions.assertEquals(4L, matcher.apply("/that.view", routes));
164         Assertions.assertEquals(5L, matcher.apply("/that.form", routes));
165         Assertions.assertEquals(0L, matcher.apply("/default/request", routes));
166     }
167 
168     @Test
169     public void testRegExWildCardMatchingSuffixPrefixPrecedence() throws Exception {
170         final UriPathRouter.RegexMatcher<Long> matcher = new UriPathRouter.RegexMatcher<>();
171         final List<PathRoute<Pattern, Long>> routes = Arrays.asList(
172                 new PathRoute<>(Pattern.compile("/ma.*"), 1L),
173                 new PathRoute<>(Pattern.compile(".*tch"), 2L));
174         Assertions.assertEquals(1L, matcher.apply("/match", routes));
175     }
176 
177 }