aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/test/java/com/yahoo/security/tls/UriGlobPatternTest.java
blob: a0d7d39516fef6cc04d9fb60209b80209638f53e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security.tls;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author bjorncs
 */
class UriGlobPatternTest {

    @Test
    void matches_correctly() {
        assertMatches("scheme://hostname/*", "scheme://hostname/mypath");
        assertMatches("scheme://hostname/*/segment2", "scheme://hostname/segment1/segment2");
        assertMatches("scheme://hostname/segment1/*", "scheme://hostname/segment1/segment2");
        assertNotMatches("scheme://hostname/*", "scheme://hostname/segment1/segment2");
        assertMatches("scheme://*/segment1/segment2", "scheme://hostname/segment1/segment2");
        assertMatches("scheme://*.name/", "scheme://host.name/");
        assertNotMatches("scheme://*", "scheme://hostname/");
        assertMatches("scheme://hostname/mypath?query=value", "scheme://hostname/mypath?query=value");
        assertNotMatches("scheme://hostname/?", "scheme://hostname/p");
    }

    private void assertMatches(String pattern, String value) {
        assertTrue(new UriGlobPattern(pattern).matches(value),
                () -> String.format("Expected '%s' to match '%s'", pattern, value));
    }

    private void assertNotMatches(String pattern, String value) {
        assertFalse(new UriGlobPattern(pattern).matches(value),
                () -> String.format("Expected '%s' to not match '%s'", pattern, value));
    }

}