MCPcopy
hub / github.com/caddyserver/caddy / TestHeaderREMatcher

Function TestHeaderREMatcher

modules/caddyhttp/matchers_test.go:900–981  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

898}
899
900func TestHeaderREMatcher(t *testing.T) {
901 for i, tc := range []struct {
902 match MatchHeaderRE
903 input http.Header // make sure these are canonical cased (std lib will do that in a real request)
904 host string
905 expect bool
906 expectRepl map[string]string
907 }{
908 {
909 match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "foo"}},
910 input: http.Header{"Field": []string{"foo"}},
911 expect: true,
912 },
913 {
914 match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "$foo^"}},
915 input: http.Header{"Field": []string{"foobar"}},
916 expect: false,
917 },
918 {
919 match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo(.*)$", Name: "name"}},
920 input: http.Header{"Field": []string{"foobar"}},
921 expect: true,
922 expectRepl: map[string]string{"name.1": "bar"},
923 },
924 {
925 match: MatchHeaderRE{"Field": &MatchRegexp{Pattern: "^foo.*$", Name: "name"}},
926 input: http.Header{"Field": []string{"barfoo", "foobar"}},
927 expect: true,
928 },
929 {
930 match: MatchHeaderRE{"host": &MatchRegexp{Pattern: "^localhost$", Name: "name"}},
931 input: http.Header{},
932 host: "localhost",
933 expect: true,
934 },
935 {
936 match: MatchHeaderRE{"host": &MatchRegexp{Pattern: "^local$", Name: "name"}},
937 input: http.Header{},
938 host: "localhost",
939 expect: false,
940 },
941 } {
942 // compile the regexp and validate its name
943 err := tc.match.Provision(caddy.Context{})
944 if err != nil {
945 t.Errorf("Test %d %v: Provisioning: %v", i, tc.match, err)
946 continue
947 }
948 err = tc.match.Validate()
949 if err != nil {
950 t.Errorf("Test %d %v: Validating: %v", i, tc.match, err)
951 continue
952 }
953
954 // set up the fake request and its Replacer
955 req := &http.Request{Header: tc.input, URL: new(url.URL), Host: tc.host}
956 repl := caddy.NewReplacer()
957 ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)

Callers

nothing calls this directly

Calls 6

ReplaceAllMethod · 0.95
addHTTPVarsToReplacerFunction · 0.85
WithValueMethod · 0.80
ProvisionMethod · 0.65
ValidateMethod · 0.65
MatchWithErrorMethod · 0.65

Tested by

no test coverage detected