github.com/erda-project/erda-infra@v1.0.9/pkg/transport/http/runtime/parser_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package runtime 16 17 import ( 18 "reflect" 19 "testing" 20 ) 21 22 func TestCompile(t *testing.T) { 23 type args struct { 24 path string 25 url string 26 } 27 type want struct { 28 params map[string]string 29 err error 30 } 31 tests := []struct { 32 name string 33 args args 34 want want 35 }{ 36 { 37 name: "empty path", 38 args: args{}, 39 want: want{}, 40 }, 41 { 42 name: "root path", 43 args: args{path: "/", url: "/"}, 44 want: want{}, 45 }, 46 { 47 name: "static path", 48 args: args{path: "/abc/def/g", url: "/abc/def/g"}, 49 want: want{}, 50 }, 51 { 52 name: "one path param", 53 args: args{ 54 path: "/abc/{def}/g", 55 url: "/abc/123/g", 56 }, 57 want: want{params: map[string]string{"def": "123"}}, 58 }, 59 { 60 name: "two path params", 61 args: args{ 62 path: "/abc/{def}/g/{xyz}", 63 url: "/abc/123/g/456", 64 }, 65 want: want{params: map[string]string{"def": "123", "xyz": "456"}}, 66 }, 67 { 68 name: "many path params", 69 args: args{ 70 path: "/abc/{def}/g/{h}/{x}/{yz}", 71 url: "/abc/123/g/4/56/7", 72 }, 73 want: want{params: map[string]string{"def": "123", "h": "4", "x": "56", "yz": "7"}}, 74 }, 75 { 76 args: args{ 77 path: "/abc/{def}/g/{h=xx}/yz", 78 url: "/abc/123/g/xx/yz", 79 }, 80 want: want{params: map[string]string{"def": "123", "h": "xx"}}, 81 }, 82 { 83 args: args{ 84 path: "/abc/{def}/g/{h=xx}/yz", 85 url: "/abc/123/g/xx123/yz", 86 }, 87 want: want{err: ErrNotMatch}, 88 }, 89 { 90 args: args{ 91 path: "/abc/{def}/g/{h=xx/123/*}/yz", 92 url: "/abc/123/g/xx/123/456/yz", 93 }, 94 want: want{params: map[string]string{"def": "123", "h": "xx/123/456"}}, 95 }, 96 { 97 args: args{ 98 path: "/abc/{def}/g/{h=xx/123/*}/yz:aaa", 99 url: "/abc/123/g/xx/123/456/yz", 100 }, 101 want: want{err: ErrNotMatch}, 102 }, 103 { 104 args: args{ 105 path: "/abc/{def}/g/{h=xx/123/*}/yz:aaa", 106 url: "/abc/123/g/xx/123/456/yz:bbb", 107 }, 108 want: want{err: ErrNotMatch}, 109 }, 110 { 111 args: args{ 112 path: "/abc/{def}/g/{h=xx/123/*}/yz:aaa", 113 url: "/abc/123/g/xx/123/456/yz:aaa", 114 }, 115 want: want{params: map[string]string{"def": "123", "h": "xx/123/456"}}, 116 }, 117 { 118 args: args{ 119 path: "/abc/{_}/d", 120 url: "/abc/123/d", 121 }, 122 want: want{params: map[string]string{"_": "123"}}, 123 }, 124 { 125 args: args{ 126 path: "invalid/path", 127 }, 128 want: want{err: ErrInvalidPattern}, 129 }, 130 } 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 matcher, err := Compile(tt.args.path) 134 if err != nil { 135 if tt.want.err != err { 136 t.Errorf("Compile() return %s", err) 137 } 138 return 139 } 140 vars, err := matcher.Match(tt.args.url) 141 if err != nil { 142 if tt.want.err != err { 143 if err != ErrNotMatch { 144 t.Errorf("matcher.Match() = %s", err) 145 } else { 146 t.Errorf("not match %q by %q", tt.args.url, tt.args.path) 147 } 148 return 149 } 150 return 151 } 152 if len(vars) == 0 && len(tt.want.params) == 0 { 153 return 154 } 155 if !reflect.DeepEqual(vars, tt.want.params) { 156 t.Errorf("params not match, got %v, want %v", vars, tt.want.params) 157 return 158 } 159 }) 160 } 161 }