aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_core/src/test/java/com/yahoo/jdisc/application/UriPatternTestCase.java
blob: fa949ccaabd39bb5ce52c09c393b632d2911afc8 (plain) (blame)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.application;

import org.junit.jupiter.api.Test;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author Simon Thoresen Hult
 */
public class UriPatternTestCase {

    private static final List<String> NO_GROUPS = Collections.emptyList();

    @Test
    void requireThatIllegalPatternsAreDetected() {
        assertIllegalPattern("scheme");
        assertIllegalPattern("scheme://");
        assertIllegalPattern("scheme://host");
        assertIllegalPattern("scheme://host:0");
        assertIllegalPattern("scheme://host:69");
        assertIllegalPattern("scheme://host:-69");
        assertIllegalPattern("scheme://host:6*/");
        assertIllegalPattern("scheme://host:6*9/");
        assertIllegalPattern("scheme://host:*9/");
    }

    @Test
    void requireThatNoPortImpliesWildcard() {
        assertEquals(new UriPattern("scheme://host/path"),
                new UriPattern("scheme://host:*/path"));
    }

    @Test
    void requireThatPatternMatches() {
        // scheme matching
        UriPattern pattern = new UriPattern("bar://host:69/path");
        assertNotMatch(pattern, "foobar://host:69/path");
        assertMatch(pattern, "bar://host:69/path", NO_GROUPS);
        assertNotMatch(pattern, "barbaz://host:69/path");

        pattern = new UriPattern("*://host:69/path");
        assertMatch(pattern, "foobar://host:69/path", Arrays.asList("foobar"));
        assertMatch(pattern, "bar://host:69/path", Arrays.asList("bar"));
        assertMatch(pattern, "barbaz://host:69/path", Arrays.asList("barbaz"));

        pattern = new UriPattern("*bar://host:69/path");
        assertMatch(pattern, "foobar://host:69/path", Arrays.asList("foo"));
        assertMatch(pattern, "bar://host:69/path", Arrays.asList(""));
        assertNotMatch(pattern, "barbaz://host:69/path");

        pattern = new UriPattern("bar*://host:69/path");
        assertNotMatch(pattern, "foobar://host:69/path");
        assertMatch(pattern, "bar://host:69/path", Arrays.asList(""));
        assertMatch(pattern, "barbaz://host:69/path", Arrays.asList("baz"));

        // host matching
        pattern = new UriPattern("scheme://bar:69/path");
        assertNotMatch(pattern, "scheme://foobar:69/path");
        assertMatch(pattern, "scheme://bar:69/path", NO_GROUPS);
        assertNotMatch(pattern, "scheme://barbaz:69/path");

        pattern = new UriPattern("scheme://*:69/path");
        assertMatch(pattern, "scheme://foobar:69/path", Arrays.asList("foobar"));
        assertMatch(pattern, "scheme://bar:69/path", Arrays.asList("bar"));
        assertMatch(pattern, "scheme://barbaz:69/path", Arrays.asList("barbaz"));

        pattern = new UriPattern("scheme://*bar:69/path");
        assertMatch(pattern, "scheme://foobar:69/path", Arrays.asList("foo"));
        assertMatch(pattern, "scheme://bar:69/path", Arrays.asList(""));
        assertNotMatch(pattern, "scheme://barbaz:69/path");

        pattern = new UriPattern("scheme://bar*:69/path");
        assertNotMatch(pattern, "scheme://foobar:69/path");
        assertMatch(pattern, "scheme://bar:69/path", Arrays.asList(""));
        assertMatch(pattern, "scheme://barbaz:69/path", Arrays.asList("baz"));

        // port matching
        pattern = new UriPattern("scheme://host:69/path");
        assertNotMatch(pattern, "scheme://host:669/path");
        assertMatch(pattern, "scheme://host:69/path", NO_GROUPS);
        assertNotMatch(pattern, "scheme://host:699/path");

        pattern = new UriPattern("scheme://host:*/path");
        assertMatch(pattern, "scheme://host:669/path", Arrays.asList("669"));
        assertMatch(pattern, "scheme://host:69/path", Arrays.asList("69"));
        assertMatch(pattern, "scheme://host:699/path", Arrays.asList("699"));

        // path matching
        pattern = new UriPattern("scheme://host:69/");
        assertMatch(pattern, "scheme://host:69/", NO_GROUPS);
        assertNotMatch(pattern, "scheme://host:69/foo");

        pattern = new UriPattern("scheme://host:69/bar");
        assertNotMatch(pattern, "scheme://host:69/foobar");
        assertMatch(pattern, "scheme://host:69/bar", NO_GROUPS);
        assertNotMatch(pattern, "scheme://host:69/barbaz");

        pattern = new UriPattern("scheme://host:69/*");
        assertMatch(pattern, "scheme://host:69/", Arrays.asList(""));
        assertMatch(pattern, "scheme://host:69/foobar", Arrays.asList("foobar"));
        assertMatch(pattern, "scheme://host:69/bar", Arrays.asList("bar"));
        assertMatch(pattern, "scheme://host:69/barbaz", Arrays.asList("barbaz"));

        pattern = new UriPattern("scheme://host:69/*bar");
        assertMatch(pattern, "scheme://host:69/foobar", Arrays.asList("foo"));
        assertMatch(pattern, "scheme://host:69/bar", Arrays.asList(""));
        assertNotMatch(pattern, "scheme://host:69/barbaz");

        pattern = new UriPattern("scheme://host:69/bar*");
        assertNotMatch(pattern, "scheme://host:69/foobar");
        assertMatch(pattern, "scheme://host:69/bar", Arrays.asList(""));
        assertMatch(pattern, "scheme://host:69/barbaz", Arrays.asList("baz"));
    }

    @Test
    void requireThatUriWithoutPathDoesNotThrowException() {
        UriPattern pattern = new UriPattern("scheme://host/path");
        assertNotMatch(pattern, "scheme://host");

        pattern = new UriPattern("scheme://host/*");
        assertMatch(pattern, "scheme://host", Arrays.asList(""));
    }

    @Test
    void requireThatOnlySchemeHostPortAndPathIsMatched() {
        UriPattern pattern = new UriPattern("scheme://host:69/path");
        assertMatch(pattern, "scheme://host:69/path?foo", NO_GROUPS);
        assertMatch(pattern, "scheme://host:69/path?foo#bar", NO_GROUPS);
    }

    @Test
    void requireThatHostSupportsWildcard() {
        UriPattern pattern = new UriPattern("scheme://*.host/path");
        assertMatch(pattern, "scheme://a.host/path", Arrays.asList("a"));
        assertMatch(pattern, "scheme://a.b.host/path", Arrays.asList("a.b"));
    }

    @Test
    void requireThatSchemesAreOrdered() {
        assertCompareLt("b://host:69/path",
                "a://host:69/path");
    }

    @Test
    void requireThatSchemeOrdersBeforeHost() {
        assertCompareLt("b://*:69/path",
                "a://host:69/path");
    }

    @Test
    void requireThatHostsAreOrdered() {
        assertCompareLt("scheme://b:69/path",
                "scheme://a:69/path");
    }

    @Test
    void requireThatHostOrdersBeforePath() {
        assertCompareLt("scheme://b:69/*",
                "scheme://a:69/path");
    }

    @Test
    void requireThatPortsAreOrdered() {
        for (int i = 1; i < 69; ++i) {
            assertCompareEq("scheme://host:" + i + "/path",
                    "scheme://host:" + i + "/path");
            assertCompareLt("scheme://host:" + (i + 1) + "/path",
                    "scheme://host:" + i + "/path");
            assertCompareLt("scheme://host:" + i + "/path",
                    "scheme://host:*/path");
        }
    }

    @Test
    void requireThatPathsAreOrdered() {
        assertCompareLt("scheme://host:69/b",
                "scheme://host:69/a");
    }

    @Test
    void requireThatPathOrdersBeforePort() {
        assertCompareLt("scheme://host:*/b",
                "scheme://host:69/a");
    }

    @Test
    void requireThatEqualPatternsOrderEqual() {
        assertCompareEq("scheme://host:69/path",
                "scheme://host:69/path");
        assertCompareEq("*://host:69/path",
                "*://host:69/path");
        assertCompareEq("scheme://*:69/path",
                "scheme://*:69/path");
        assertCompareEq("scheme://host:*/path",
                "scheme://host:*/path");
        assertCompareEq("scheme://host:69/*",
                "scheme://host:69/*");
    }

    @Test
    void requireThatStrictPatternsOrderBeforeWildcards() {
        assertCompareLt("scheme://host:69/path",
                "*://host:69/path");
        assertCompareLt("scheme://a:69/path",
                "scheme://*:69/path");
        assertCompareLt("scheme://a:69/path",
                "scheme://*a:69/path");
        assertCompareLt("scheme://*aa:69/path",
                "scheme://*a:69/path");
        assertCompareLt("scheme://host:69/path",
                "scheme://host:*/path");
        assertCompareLt("scheme://host:69/a",
                "scheme://host:69/*");
        assertCompareLt("scheme://host:69/a",
                "scheme://host:69/a*");
        assertCompareLt("scheme://host:69/aa*",
                "scheme://host:69/a*");
        assertCompareLt("scheme://*:69/path",
                "*://host:69/path");
        assertCompareLt("scheme://host:*/path",
                "scheme://*:69/path");
        assertCompareLt("scheme://host:*/path",
                "scheme://host:69/*");
        assertCompareLt("scheme://host:69/foo",
                "scheme://host:69/*");
        assertCompareLt("scheme://host:69/foo/bar",
                "scheme://host:69/foo/*");
        assertCompareLt("scheme://host:69/foo/bar/baz",
                "scheme://host:69/foo/bar/*");
    }

    @Test
    void requireThatLongPatternsOrderBeforeShort() {
        assertCompareLt("scheme://host:69/foo/bar",
                "scheme://host:69/foo");
        assertCompareLt("scheme://host:69/foo/bar/baz",
                "scheme://host:69/foo/bar");
    }

    @Test
    void requireThatHttpsSchemeIsHandledAsHttp() {
        UriPattern httpPattern = new UriPattern("http://host:80/path");
        assertMatch(httpPattern, "https://host:80/path", NO_GROUPS);

        UriPattern httpsPattern = new UriPattern("https://host:443/path");
        assertMatch(httpsPattern, "http://host:443/path", NO_GROUPS);
    }

    @Test
    void requireThatUrlEncodingIsNotDoneForPath() {
        UriPattern encodedSlashPattern = new UriPattern("http://host:80/one%2Fpath");
        assertMatch(encodedSlashPattern, "http://host:80/one%2Fpath", NO_GROUPS);
        assertNotMatch(encodedSlashPattern, "http://host:80/one/path");

        UriPattern actualSlashPattern = new UriPattern("http://host:80/two/paths");
        assertNotMatch(actualSlashPattern, "http://host:80/two%2Fpaths");
        assertMatch(actualSlashPattern, "http://host:80/two/paths", NO_GROUPS);
    }

    private static void assertIllegalPattern(String uri) {
        try {
            new UriPattern(uri);
            fail();
        } catch (IllegalArgumentException e) {
            // expected
        }
    }

    private static void assertCompareLt(String lhs, String rhs) {
        assertCompareLt(new UriPattern(lhs), new UriPattern(rhs));
    }

    private static void assertCompareLt(UriPattern lhs, UriPattern rhs) {
        assertEquals(-1, compare(lhs, rhs));
    }

    private static void assertCompareEq(String lhs, String rhs) {
        assertCompareEq(new UriPattern(lhs), new UriPattern(rhs));
    }

    private static void assertCompareEq(UriPattern lhs, UriPattern rhs) {
        assertEquals(0, compare(lhs, rhs));
    }

    private static int compare(UriPattern lhs, UriPattern rhs) {
        int lhsCmp = lhs.compareTo(rhs);
        int rhsCmp = rhs.compareTo(lhs);
        if (lhsCmp < 0) {
            assertTrue(rhsCmp > 0);
            return -1;
        }
        if (lhsCmp > 0) {
            assertTrue(rhsCmp < 0);
            return 1;
        }
        assertEquals(rhsCmp, 0);
        return 0;
    }

    private static void assertMatch(UriPattern pattern, String uri, List<String> expected) {
        UriPattern.Match match = pattern.match(URI.create(uri));
        assertNotNull(match);
        List<String> actual = new ArrayList<>(match.groupCount());
        for (int i = 0, len = match.groupCount(); i < len; ++i) {
            actual.add(match.group(i));
        }
        assertEquals(expected, actual);
    }

    private static void assertNotMatch(UriPattern pattern, String uri) {
        assertNull(pattern.match(URI.create(uri)));
    }
}