github.com/erda-project/erda-infra@v1.0.10-0.20240327085753-f3a249292aeb/pkg/transport/http/httprule/compile_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 // Reference: https://github.com/grpc-ecosystem/grpc-gateway/blob/v2.3.0/internal/httprule/compile_test.go 16 17 package httprule 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/erda-project/erda-infra/pkg/transport/http/utilities" 24 ) 25 26 const ( 27 operandFiller = 0 28 ) 29 30 func TestCompile(t *testing.T) { 31 for _, spec := range []struct { 32 segs []segment 33 verb string 34 35 ops []int 36 pool []string 37 fields []string 38 }{ 39 {}, 40 { 41 segs: []segment{ 42 wildcard{}, 43 }, 44 ops: []int{int(utilities.OpPush), operandFiller}, 45 }, 46 { 47 segs: []segment{ 48 deepWildcard{}, 49 }, 50 ops: []int{int(utilities.OpPushM), operandFiller}, 51 }, 52 { 53 segs: []segment{ 54 literal("v1"), 55 }, 56 ops: []int{int(utilities.OpLitPush), 0}, 57 pool: []string{"v1"}, 58 }, 59 { 60 segs: []segment{ 61 literal("v1"), 62 }, 63 verb: "LOCK", 64 ops: []int{int(utilities.OpLitPush), 0}, 65 pool: []string{"v1"}, 66 }, 67 { 68 segs: []segment{ 69 variable{ 70 path: "name.nested", 71 segments: []segment{ 72 wildcard{}, 73 }, 74 }, 75 }, 76 ops: []int{ 77 int(utilities.OpPush), operandFiller, 78 int(utilities.OpConcatN), 1, 79 int(utilities.OpCapture), 0, 80 }, 81 pool: []string{"name.nested"}, 82 fields: []string{"name.nested"}, 83 }, 84 { 85 segs: []segment{ 86 literal("obj"), 87 variable{ 88 path: "name.nested", 89 segments: []segment{ 90 literal("a"), 91 wildcard{}, 92 literal("b"), 93 }, 94 }, 95 variable{ 96 path: "obj", 97 segments: []segment{ 98 deepWildcard{}, 99 }, 100 }, 101 }, 102 ops: []int{ 103 int(utilities.OpLitPush), 0, 104 int(utilities.OpLitPush), 1, 105 int(utilities.OpPush), operandFiller, 106 int(utilities.OpLitPush), 2, 107 int(utilities.OpConcatN), 3, 108 int(utilities.OpCapture), 3, 109 int(utilities.OpPushM), operandFiller, 110 int(utilities.OpConcatN), 1, 111 int(utilities.OpCapture), 0, 112 }, 113 pool: []string{"obj", "a", "b", "name.nested"}, 114 fields: []string{"name.nested", "obj"}, 115 }, 116 } { 117 tmpl := template{ 118 segments: spec.segs, 119 verb: spec.verb, 120 } 121 compiled := tmpl.Compile() 122 if got, want := compiled.Version, opcodeVersion; got != want { 123 t.Errorf("tmpl.Compile().Version = %d; want %d; segs=%#v, verb=%q", got, want, spec.segs, spec.verb) 124 } 125 if got, want := compiled.OpCodes, spec.ops; !reflect.DeepEqual(got, want) { 126 t.Errorf("tmpl.Compile().OpCodes = %v; want %v; segs=%#v, verb=%q", got, want, spec.segs, spec.verb) 127 } 128 if got, want := compiled.Pool, spec.pool; !reflect.DeepEqual(got, want) { 129 t.Errorf("tmpl.Compile().Pool = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb) 130 } 131 if got, want := compiled.Verb, spec.verb; got != want { 132 t.Errorf("tmpl.Compile().Verb = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb) 133 } 134 if got, want := compiled.Fields, spec.fields; !reflect.DeepEqual(got, want) { 135 t.Errorf("tmpl.Compile().Fields = %q; want %q; segs=%#v, verb=%q", got, want, spec.segs, spec.verb) 136 } 137 } 138 }