(t *testing.T)
| 1006 | } |
| 1007 | |
| 1008 | func TestVarREMatcher(t *testing.T) { |
| 1009 | for i, tc := range []struct { |
| 1010 | desc string |
| 1011 | match MatchVarsRE |
| 1012 | input VarsMiddleware |
| 1013 | headers http.Header |
| 1014 | expect bool |
| 1015 | expectRepl map[string]string |
| 1016 | }{ |
| 1017 | { |
| 1018 | desc: "match static value within var set by the VarsMiddleware succeeds", |
| 1019 | match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "foo"}}, |
| 1020 | input: VarsMiddleware{"Var1": "here is foo val"}, |
| 1021 | expect: true, |
| 1022 | }, |
| 1023 | { |
| 1024 | desc: "value set by VarsMiddleware not satisfying regexp matcher fails to match", |
| 1025 | match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "$foo^"}}, |
| 1026 | input: VarsMiddleware{"Var1": "foobar"}, |
| 1027 | expect: false, |
| 1028 | }, |
| 1029 | { |
| 1030 | desc: "successfully matched value is captured and its placeholder is added to replacer", |
| 1031 | match: MatchVarsRE{"Var1": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}}, |
| 1032 | input: VarsMiddleware{"Var1": "foobar"}, |
| 1033 | expect: true, |
| 1034 | expectRepl: map[string]string{"name.1": "bar"}, |
| 1035 | }, |
| 1036 | { |
| 1037 | desc: "matching against a value of standard variables succeeds", |
| 1038 | match: MatchVarsRE{"{http.request.method}": &MatchRegexp{Pattern: "^G.[tT]$"}}, |
| 1039 | input: VarsMiddleware{}, |
| 1040 | expect: true, |
| 1041 | }, |
| 1042 | { |
| 1043 | desc: "matching against value of var set by the VarsMiddleware and referenced by its placeholder succeeds", |
| 1044 | match: MatchVarsRE{"{http.vars.Var1}": &MatchRegexp{Pattern: "[vV]ar[0-9]"}}, |
| 1045 | input: VarsMiddleware{"Var1": "var1Value"}, |
| 1046 | expect: true, |
| 1047 | }, |
| 1048 | { |
| 1049 | desc: "placeholder key value containing braces is not double-expanded", |
| 1050 | match: MatchVarsRE{"{http.request.header.X-Input}": &MatchRegexp{Pattern: ".+", Name: "val"}}, |
| 1051 | input: VarsMiddleware{}, |
| 1052 | headers: http.Header{"X-Input": []string{"{env.HOME}"}}, |
| 1053 | expect: true, |
| 1054 | expectRepl: map[string]string{"val.0": "{env.HOME}"}, |
| 1055 | }, |
| 1056 | } { |
| 1057 | t.Run(tc.desc, func(t *testing.T) { |
| 1058 | t.Parallel() |
| 1059 | // compile the regexp and validate its name |
| 1060 | err := tc.match.Provision(caddy.Context{}) |
| 1061 | if err != nil { |
| 1062 | t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err) |
| 1063 | return |
| 1064 | } |
| 1065 | err = tc.match.Validate() |
nothing calls this directly
no test coverage detected