(t *testing.T)
| 898 | } |
| 899 | |
| 900 | func 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) |
nothing calls this directly
no test coverage detected