| 1 | package maphandler |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "reflect" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/caddyserver/caddy/v2" |
| 11 | "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
| 12 | ) |
| 13 | |
| 14 | func TestHandler(t *testing.T) { |
| 15 | for i, tc := range []struct { |
| 16 | handler Handler |
| 17 | reqURI string |
| 18 | expect map[string]any |
| 19 | }{ |
| 20 | { |
| 21 | reqURI: "/foo", |
| 22 | handler: Handler{ |
| 23 | Source: "{http.request.uri.path}", |
| 24 | Destinations: []string{"{output}"}, |
| 25 | Mappings: []Mapping{ |
| 26 | { |
| 27 | Input: "/foo", |
| 28 | Outputs: []any{"FOO"}, |
| 29 | }, |
| 30 | }, |
| 31 | }, |
| 32 | expect: map[string]any{ |
| 33 | "output": "FOO", |
| 34 | }, |
| 35 | }, |
| 36 | { |
| 37 | reqURI: "/abcdef", |
| 38 | handler: Handler{ |
| 39 | Source: "{http.request.uri.path}", |
| 40 | Destinations: []string{"{output}"}, |
| 41 | Mappings: []Mapping{ |
| 42 | { |
| 43 | InputRegexp: "(/abc)", |
| 44 | Outputs: []any{"ABC"}, |
| 45 | }, |
| 46 | }, |
| 47 | }, |
| 48 | expect: map[string]any{ |
| 49 | "output": "ABC", |
| 50 | }, |
| 51 | }, |
| 52 | { |
| 53 | reqURI: "/ABCxyzDEF", |
| 54 | handler: Handler{ |
| 55 | Source: "{http.request.uri.path}", |
| 56 | Destinations: []string{"{output}"}, |
| 57 | Mappings: []Mapping{ |
| 58 | { |
| 59 | InputRegexp: "(xyz)", |
| 60 | 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: Handler{ |
| 72 | Source: "{http.request.uri}", |
| 73 | Destinations: []string{"{output}"}, |
| 74 | Mappings: []Mapping{ |
| 75 | { |
| 76 | InputRegexp: "(?i)(\\^|`|<|>|%|\\\\|\\{|\\}|\\|)", |
| 77 | Outputs: []any{"3"}, |
| 78 | }, |
| 79 | }, |
| 80 | }, |
| 81 | expect: map[string]any{ |
| 82 | "output": "3", |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | reqURI: "/foo", |
| 87 | handler: Handler{ |
| 88 | Source: "{http.request.uri.path}", |
| 89 | Destinations: []string{"{output}"}, |
| 90 | Mappings: []Mapping{ |
| 91 | { |
| 92 | Input: "/foo", |
| 93 | Outputs: []any{"{testvar}"}, |
| 94 | }, |
| 95 | }, |
| 96 | }, |
| 97 | expect: map[string]any{ |
| 98 | "output": "testing", |
| 99 | }, |
| 100 | }, |
| 101 | { |
| 102 | reqURI: "/foo", |
| 103 | handler: Handler{ |
| 104 | Source: "{http.request.uri.path}", |
| 105 | Destinations: []string{"{output}"}, |
| 106 | Defaults: []string{"default"}, |
| 107 | }, |
| 108 | expect: map[string]any{ |
| 109 | "output": "default", |
| 110 | }, |
| 111 | }, |
| 112 | { |
| 113 | reqURI: "/foo", |
| 114 | handler: Handler{ |
| 115 | Source: "{http.request.uri.path}", |
| 116 | Destinations: []string{"{output}"}, |
| 117 | Defaults: []string{"{testvar}"}, |
| 118 | }, |
| 119 | expect: map[string]any{ |
| 120 | "output": "testing", |
| 121 | }, |
| 122 | }, |
| 123 | } { |
| 124 | if err := tc.handler.Provision(caddy.Context{}); err != nil { |
| 125 | t.Fatalf("Test %d: Provisioning handler: %v", i, err) |
| 126 | } |
| 127 | |
| 128 | req, err := http.NewRequest(http.MethodGet, tc.reqURI, nil) |
| 129 | if err != nil { |
| 130 | t.Fatalf("Test %d: Creating request: %v", i, err) |
| 131 | } |
| 132 | repl := caddyhttp.NewTestReplacer(req) |
| 133 | repl.Set("testvar", "testing") |
| 134 | ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl) |
| 135 | req = req.WithContext(ctx) |
| 136 | |
| 137 | rr := httptest.NewRecorder() |
| 138 | noop := caddyhttp.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) error { return nil }) |
| 139 | |
| 140 | if err := tc.handler.ServeHTTP(rr, req, noop); err != nil { |
| 141 | t.Errorf("Test %d: Handler returned error: %v", i, err) |
| 142 | continue |
| 143 | } |
| 144 | |
| 145 | for key, expected := range tc.expect { |
| 146 | actual, _ := repl.Get(key) |
| 147 | if !reflect.DeepEqual(actual, expected) { |
| 148 | t.Errorf("Test %d: Expected %#v but got %#v for {%s}", i, expected, actual, key) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |