github.com/cnotch/ipchub@v1.1.0/provider/auth/path_matcher_test.go (about) 1 // Copyright (c) 2019,CAOHONGJU All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package auth 6 7 import "testing" 8 9 func Test_alwaysMatcher_Match(t *testing.T) { 10 tests := []struct { 11 name string 12 path string 13 want bool 14 }{ 15 {"always", "/a/b", true}, 16 {"always", "/a", true}, 17 } 18 for _, tt := range tests { 19 t.Run(tt.name, func(t *testing.T) { 20 m := NewPathMatcher(endWildcard) 21 if got := m.Match(tt.path); got != tt.want { 22 t.Errorf("alwaysMatcher.Match() = %v, want %v", got, tt.want) 23 } 24 }) 25 } 26 } 27 28 func Test_pathMacher_Match(t *testing.T) { 29 tests := []struct { 30 name string 31 pathMask string 32 path string 33 want bool 34 }{ 35 {"g1", "/a", "/a", true}, 36 {"g2", "/a", "/a/b", false}, 37 {"e1", "/a/*", "/a", true}, 38 {"e2", "/a/*", "/a/b", true}, 39 {"e3", "/a/*", "/a/b/c", true}, 40 {"e4", "/a/*", "/b", false}, 41 {"c1", "/a/+/c/*", "/a/b/c", true}, 42 {"c2", "/a/+/c/*", "/a/d/c", true}, 43 {"c3", "/a/+/c/*", "/a/b/c/d", true}, 44 {"c4", "/a/+/c/*", "/a/b/c/d/e", true}, 45 {"c5", "/a/+/c/*", "/a/c/d/e", false}, 46 } 47 for _, tt := range tests { 48 t.Run(tt.name, func(t *testing.T) { 49 m := NewPathMatcher(tt.pathMask) 50 if got := m.Match(tt.path); got != tt.want { 51 t.Errorf("pathMacher.Match() = %v, want %v", got, tt.want) 52 } 53 }) 54 } 55 }