| 1 | package maphandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src go1.25.1 context/" |
| 5 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 6 | "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/" |
| 7 | "reflectscip-go gomod github.com/golang/go/src go1.25.1 reflect/" |
| 8 | "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/" |
| 9 | |
| 10 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 11 | "github.com/caddyserver/caddy/v2/modules/caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/" |
| 12 | ) |
| 13 | |
| 14 | func TestHandlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/TestHandler().(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#) { |
| 15 | for i, tc := range []struct { |
| 16 | handler Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler# |
| 17 | reqURI string |
| 18 | expect map[string]any |
| 19 | }{ |
| 20 | { |
| 21 | reqURI: "/foo", |
| 22 | handler: Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#{ |
| 23 | Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source.: "{http.request.uri.path}", |
| 24 | Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.: []string{"{output}"}, |
| 25 | Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings.: []Mappingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#{ |
| 26 | { |
| 27 | Inputscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Input.: "/foo", |
| 28 | Outputsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Outputs.: []any{"FOO"}, |
| 29 | }, |
| 30 | }, |
| 31 | }, |
| 32 | expect: map[string]any{ |
| 33 | "output": "FOO", |
| 34 | }, |
| 35 | }, |
| 36 | { |
| 37 | reqURI: "/abcdef", |
| 38 | handler: Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#{ |
| 39 | Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source.: "{http.request.uri.path}", |
| 40 | Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.: []string{"{output}"}, |
| 41 | Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings.: []Mappingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#{ |
| 42 | { |
| 43 | InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp.: "(/abc)", |
| 44 | Outputsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Outputs.: []any{"ABC"}, |
| 45 | }, |
| 46 | }, |
| 47 | }, |
| 48 | expect: map[string]any{ |
| 49 | "output": "ABC", |
| 50 | }, |
| 51 | }, |
| 52 | { |
| 53 | reqURI: "/ABCxyzDEF", |
| 54 | handler: Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#{ |
| 55 | Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source.: "{http.request.uri.path}", |
| 56 | Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.: []string{"{output}"}, |
| 57 | Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings.: []Mappingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#{ |
| 58 | { |
| 59 | InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp.: "(xyz)", |
| 60 | Outputsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Outputs.: []any{"...${1}..."}, |
| 61 | }, |
| 62 | }, |
| 63 | }, |
| 64 | expect: map[string]any{ |
| 65 | "output": "...xyz...", |
| 66 | }, |
| 67 | }, |
| 68 | { |
| 69 | // Test case from https://caddy.community/t/map-directive-and-regular-expressions/13866/14?u=matt |
| 70 | reqURI: "/?s=0%27+AND+%28SELECT+0+FROM+%28SELECT+count%28%2A%29%2C+CONCAT%28%28SELECT+%40%40version%29%2C+0x23%2C+FLOOR%28RAND%280%29%2A2%29%29+AS+x+FROM+information_schema.columns+GROUP+BY+x%29+y%29+-+-+%27", |
| 71 | handler: Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#{ |
| 72 | Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source.: "{http.request.uri}", |
| 73 | Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.: []string{"{output}"}, |
| 74 | Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings.: []Mappingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#{ |
| 75 | { |
| 76 | InputRegexpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#InputRegexp.: "(?i)(\\^|`|<|>|%|\\\\|\\{|\\}|\\|)", |
| 77 | Outputsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Outputs.: []any{"3"}, |
| 78 | }, |
| 79 | }, |
| 80 | }, |
| 81 | expect: map[string]any{ |
| 82 | "output": "3", |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | reqURI: "/foo", |
| 87 | handler: Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#{ |
| 88 | Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source.: "{http.request.uri.path}", |
| 89 | Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.: []string{"{output}"}, |
| 90 | Mappingsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Mappings.: []Mappingscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#{ |
| 91 | { |
| 92 | Inputscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Input.: "/foo", |
| 93 | Outputsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Mapping#Outputs.: []any{"{testvar}"}, |
| 94 | }, |
| 95 | }, |
| 96 | }, |
| 97 | expect: map[string]any{ |
| 98 | "output": "testing", |
| 99 | }, |
| 100 | }, |
| 101 | { |
| 102 | reqURI: "/foo", |
| 103 | handler: Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#{ |
| 104 | Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source.: "{http.request.uri.path}", |
| 105 | Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.: []string{"{output}"}, |
| 106 | Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults.: []string{"default"}, |
| 107 | }, |
| 108 | expect: map[string]any{ |
| 109 | "output": "default", |
| 110 | }, |
| 111 | }, |
| 112 | { |
| 113 | reqURI: "/foo", |
| 114 | handler: Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#{ |
| 115 | Sourcescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Source.: "{http.request.uri.path}", |
| 116 | Destinationsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Destinations.: []string{"{output}"}, |
| 117 | Defaultsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#Defaults.: []string{"{testvar}"}, |
| 118 | }, |
| 119 | expect: map[string]any{ |
| 120 | "output": "testing", |
| 121 | }, |
| 122 | }, |
| 123 | } { |
| 124 | if err := tc.handler.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#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#{}); err != nil { |
| 125 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Provisioning handler: %v", i, err) |
| 126 | } |
| 127 | |
| 128 | req, err := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/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., tc.reqURI, nil) |
| 129 | if err != nil { |
| 130 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("Test %d: Creating request: %v", i, err) |
| 131 | } |
| 132 | repl := caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.NewTestReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/NewTestReplacer().(req) |
| 133 | repl.Setscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Set().("testvar", "testing") |
| 134 | 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) |
| 135 | req = req.WithContextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#WithContext().(ctx) |
| 136 | |
| 137 | rr := 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().() |
| 138 | noop := caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/.HandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerFunc#(func(_ httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, _ *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#) error { return nil }) |
| 139 | |
| 140 | if err := tc.handler.ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp/map`/Handler#ServeHTTP().(rr, req, noop); err != nil { |
| 141 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Handler returned error: %v", i, err) |
| 142 | continue |
| 143 | } |
| 144 | |
| 145 | for key, expected := range tc.expect { |
| 146 | actual, _ := repl.Getscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#Get().(key) |
| 147 | if !reflectscip-go gomod github.com/golang/go/src go1.25.1 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.1 reflect/DeepEqual().(actual, expected) { |
| 148 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected %#v but got %#v for {%s}", i, expected, actual, key) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |