2021-10-13 18:58:20 -05:00
|
|
|
package maphandler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandler(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
handler Handler
|
2021-10-19 13:25:36 -05:00
|
|
|
reqURI string
|
2022-08-02 15:39:09 -05:00
|
|
|
expect map[string]any
|
2021-10-13 18:58:20 -05:00
|
|
|
}{
|
|
|
|
{
|
2021-10-19 13:25:36 -05:00
|
|
|
reqURI: "/foo",
|
2021-10-13 18:58:20 -05:00
|
|
|
handler: Handler{
|
|
|
|
Source: "{http.request.uri.path}",
|
|
|
|
Destinations: []string{"{output}"},
|
|
|
|
Mappings: []Mapping{
|
|
|
|
{
|
|
|
|
Input: "/foo",
|
2022-08-02 15:39:09 -05:00
|
|
|
Outputs: []any{"FOO"},
|
2021-10-13 18:58:20 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-02 15:39:09 -05:00
|
|
|
expect: map[string]any{
|
2021-10-13 18:58:20 -05:00
|
|
|
"output": "FOO",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-19 13:25:36 -05:00
|
|
|
reqURI: "/abcdef",
|
2021-10-13 18:58:20 -05:00
|
|
|
handler: Handler{
|
|
|
|
Source: "{http.request.uri.path}",
|
|
|
|
Destinations: []string{"{output}"},
|
|
|
|
Mappings: []Mapping{
|
|
|
|
{
|
|
|
|
InputRegexp: "(/abc)",
|
2022-08-02 15:39:09 -05:00
|
|
|
Outputs: []any{"ABC"},
|
2021-10-13 18:58:20 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-02 15:39:09 -05:00
|
|
|
expect: map[string]any{
|
2021-10-13 18:58:20 -05:00
|
|
|
"output": "ABC",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-19 13:25:36 -05:00
|
|
|
reqURI: "/ABCxyzDEF",
|
2021-10-13 18:58:20 -05:00
|
|
|
handler: Handler{
|
|
|
|
Source: "{http.request.uri.path}",
|
|
|
|
Destinations: []string{"{output}"},
|
|
|
|
Mappings: []Mapping{
|
|
|
|
{
|
|
|
|
InputRegexp: "(xyz)",
|
2022-08-02 15:39:09 -05:00
|
|
|
Outputs: []any{"...${1}..."},
|
2021-10-13 18:58:20 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-02 15:39:09 -05:00
|
|
|
expect: map[string]any{
|
2021-10-13 18:58:20 -05:00
|
|
|
"output": "...xyz...",
|
|
|
|
},
|
|
|
|
},
|
2021-10-19 13:25:36 -05:00
|
|
|
{
|
|
|
|
// Test case from https://caddy.community/t/map-directive-and-regular-expressions/13866/14?u=matt
|
|
|
|
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",
|
|
|
|
handler: Handler{
|
|
|
|
Source: "{http.request.uri}",
|
|
|
|
Destinations: []string{"{output}"},
|
|
|
|
Mappings: []Mapping{
|
|
|
|
{
|
|
|
|
InputRegexp: "(?i)(\\^|`|<|>|%|\\\\|\\{|\\}|\\|)",
|
2022-08-02 15:39:09 -05:00
|
|
|
Outputs: []any{"3"},
|
2021-10-19 13:25:36 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-02 15:39:09 -05:00
|
|
|
expect: map[string]any{
|
2021-10-19 13:25:36 -05:00
|
|
|
"output": "3",
|
|
|
|
},
|
|
|
|
},
|
2022-03-21 18:05:38 -05:00
|
|
|
{
|
|
|
|
reqURI: "/foo",
|
|
|
|
handler: Handler{
|
|
|
|
Source: "{http.request.uri.path}",
|
|
|
|
Destinations: []string{"{output}"},
|
|
|
|
Mappings: []Mapping{
|
|
|
|
{
|
|
|
|
Input: "/foo",
|
2022-08-02 15:39:09 -05:00
|
|
|
Outputs: []any{"{testvar}"},
|
2022-03-21 18:05:38 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-02 15:39:09 -05:00
|
|
|
expect: map[string]any{
|
2022-03-21 18:05:38 -05:00
|
|
|
"output": "testing",
|
|
|
|
},
|
|
|
|
},
|
2022-09-28 14:38:20 -05:00
|
|
|
{
|
|
|
|
reqURI: "/foo",
|
|
|
|
handler: Handler{
|
|
|
|
Source: "{http.request.uri.path}",
|
|
|
|
Destinations: []string{"{output}"},
|
|
|
|
Defaults: []string{"default"},
|
|
|
|
},
|
|
|
|
expect: map[string]any{
|
|
|
|
"output": "default",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
reqURI: "/foo",
|
|
|
|
handler: Handler{
|
|
|
|
Source: "{http.request.uri.path}",
|
|
|
|
Destinations: []string{"{output}"},
|
|
|
|
Defaults: []string{"{testvar}"},
|
|
|
|
},
|
|
|
|
expect: map[string]any{
|
|
|
|
"output": "testing",
|
|
|
|
},
|
|
|
|
},
|
2021-10-13 18:58:20 -05:00
|
|
|
} {
|
|
|
|
if err := tc.handler.Provision(caddy.Context{}); err != nil {
|
|
|
|
t.Fatalf("Test %d: Provisioning handler: %v", i, err)
|
|
|
|
}
|
|
|
|
|
2021-10-19 13:25:36 -05:00
|
|
|
req, err := http.NewRequest(http.MethodGet, tc.reqURI, nil)
|
2021-10-13 18:58:20 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Creating request: %v", i, err)
|
|
|
|
}
|
|
|
|
repl := caddyhttp.NewTestReplacer(req)
|
2022-03-21 18:05:38 -05:00
|
|
|
repl.Set("testvar", "testing")
|
2021-10-13 18:58:20 -05:00
|
|
|
ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
noop := caddyhttp.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) error { return nil })
|
|
|
|
|
|
|
|
if err := tc.handler.ServeHTTP(rr, req, noop); err != nil {
|
|
|
|
t.Errorf("Test %d: Handler returned error: %v", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, expected := range tc.expect {
|
|
|
|
actual, _ := repl.Get(key)
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Errorf("Test %d: Expected %#v but got %#v for {%s}", i, expected, actual, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|