Debian Codegraph @

modules/caddyhttp/vars_test.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
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 caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/
16
17 import (
18 "contextscip-go gomod github.com/golang/go/src go1.25.1 context/"
19 "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/"
20 "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/"
21 "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/"
22
23 "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/"
24 )
25
26 func newVarsTestRequestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/newVarsTestRequest().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#, target string, headers httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#, vars map[string]any) (*httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#, *caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Replacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#) {
27 t.Helperscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Helper().()
28
29 if target == "" {
30 target = "https://example.com/test"
31 }
32
33 req := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRequest().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.MethodGetscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/MethodGet., target, nil)
34 req.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header. = headers
35
36 repl := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewReplacer().()
37 ctx := contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(req.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey., repl)
38 if vars == nil {
39 vars = make(map[string]any)
40 }
41 // Inject vars directly so these tests exercise matcher-side handling of
42 // already-resolved values, not VarsMiddleware placeholder expansion.
43 ctx = contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(ctx, VarsCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/VarsCtxKey., vars)
44 req = req.WithContextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#WithContext().(ctx)
45
46 addHTTPVarsToReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/addHTTPVarsToReplacer().(repl, req, httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().())
47
48 return req, repl
49 }
50
51 func TestVarsMatcherDoesNotExpandResolvedValuesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/TestVarsMatcherDoesNotExpandResolvedValues().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
52 t.Setenvscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Setenv().("CADDY_VARS_TEST_SECRET", "topsecret")
53
54 for _, tc := range []struct {
55 name string
56 target string
57 match VarsMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/VarsMatcher#
58 headers httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#
59 vars map[string]any
60 expect bool
61 }{
62 {
63 name: "literal variable value containing placeholder syntax is not re-expanded",
64 match: VarsMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/VarsMatcher#{"secret": []string{"topsecret"}},
65 vars: map[string]any{"secret": "{env.CADDY_VARS_TEST_SECRET}"},
66 expect: false,
67 },
68 {
69 name: "placeholder key value containing placeholder syntax is not re-expanded",
70 match: VarsMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/VarsMatcher#{"{http.request.header.X-Input}": []string{"topsecret"}},
71 headers: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#{"X-Input": []string{"{env.CADDY_VARS_TEST_SECRET}"}},
72 expect: false,
73 },
74 {
75 name: "query placeholder value containing placeholder syntax is not re-expanded",
76 target: "https://example.com/test?foo=%7Benv.CADDY_VARS_TEST_SECRET%7D",
77 match: VarsMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/VarsMatcher#{"{http.request.uri.query.foo}": []string{"topsecret"}},
78 expect: false,
79 },
80 {
81 name: "matcher values still expand placeholders",
82 match: VarsMatcherscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/VarsMatcher#{"secret": []string{"{env.CADDY_VARS_TEST_SECRET}"}},
83 vars: map[string]any{"secret": "topsecret"},
84 expect: true,
85 },
86 } {
87 t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(tc.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
88 t.Parallelscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Parallel().()
89
90 req, _ := newVarsTestRequestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/newVarsTestRequest().(t, tc.target, tc.headers, tc.vars)
91
92 actual, err := tc.match.MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/VarsMatcher#MatchWithError().(req)
93 if err != nil {
94 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("MatchWithError() error = %v", err)
95 }
96
97 if actual != tc.expect {
98 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("MatchWithError() = %t, want %t", actual, tc.expect)
99 }
100 })
101 }
102 }
103
104 func TestMatchVarsREDoesNotExpandResolvedValuesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/TestMatchVarsREDoesNotExpandResolvedValues().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
105 t.Setenvscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Setenv().("CADDY_VARS_TEST_SECRET", "topsecret")
106
107 for _, tc := range []struct {
108 name string
109 target string
110 match MatchVarsREscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchVarsRE#
111 headers httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#
112 vars map[string]any
113 expect bool
114 }{
115 {
116 name: "literal variable value containing placeholder syntax is not re-expanded",
117 match: MatchVarsREscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchVarsRE#{"secret": &MatchRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchRegexp#{Patternscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchRegexp#Pattern.: "^topsecret$"}},
118 vars: map[string]any{"secret": "{env.CADDY_VARS_TEST_SECRET}"},
119 expect: false,
120 },
121 {
122 name: "placeholder key value containing placeholder syntax is not re-expanded",
123 match: MatchVarsREscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchVarsRE#{"{http.request.header.X-Input}": &MatchRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchRegexp#{Patternscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchRegexp#Pattern.: "^topsecret$"}},
124 headers: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#{"X-Input": []string{"{env.CADDY_VARS_TEST_SECRET}"}},
125 expect: false,
126 },
127 {
128 name: "query placeholder value containing placeholder syntax is not re-expanded",
129 target: "https://example.com/test?foo=%7Benv.CADDY_VARS_TEST_SECRET%7D",
130 match: MatchVarsREscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchVarsRE#{"{http.request.uri.query.foo}": &MatchRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchRegexp#{Patternscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchRegexp#Pattern.: "^topsecret$"}},
131 expect: false,
132 },
133 } {
134 t.Runscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Run().(tc.name, func(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) {
135 t.Parallelscip-go gomod github.com/golang/go/src go1.25.1 testing/T#Parallel().()
136
137 err := tc.match.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchVarsRE#Provision().(caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#{})
138 if err != nil {
139 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Provision() error = %v", err)
140 }
141
142 err = tc.match.Validatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchVarsRE#Validate().()
143 if err != nil {
144 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Validate() error = %v", err)
145 }
146
147 req, _ := newVarsTestRequestscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/newVarsTestRequest().(t, tc.target, tc.headers, tc.vars)
148
149 actual, err := tc.match.MatchWithErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchVarsRE#MatchWithError().(req)
150 if err != nil {
151 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("MatchWithError() error = %v", err)
152 }
153
154 if actual != tc.expect {
155 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("MatchWithError() = %t, want %t", actual, tc.expect)
156 }
157 })
158 }
159 }