Skip to content

Commit

Permalink
Add tests for URL path unescaping
Browse files Browse the repository at this point in the history
  • Loading branch information
v3n committed Sep 10, 2021
1 parent 229eed3 commit a18708e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
66 changes: 65 additions & 1 deletion runtime/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func TestMuxServeHTTP(t *testing.T) {
respContent string

disablePathLengthFallback bool
unescapingMode runtime.UnescapingMode
}{
{
patterns: nil,
Expand Down Expand Up @@ -330,11 +331,74 @@ func TestMuxServeHTTP(t *testing.T) {
respStatus: http.StatusOK,
respContent: "POST /foo/{id=*}:verb:subverb",
},
{
patterns: []stubPattern{
{
method: "GET",
ops: []int{int(utilities.OpLitPush), 0, int(utilities.OpPush), 1, int(utilities.OpCapture), 1, int(utilities.OpLitPush), 2},
pool: []string{"foo", "id", "bar"},
},
},
reqMethod: "POST",
reqPath: "/foo/404%2fwith%2Fspace/bar",
headers: map[string]string{
"Content-Type": "application/json",
},
respStatus: http.StatusNotFound,
unescapingMode: runtime.UnescapingModeLegacy,
},
{
patterns: []stubPattern{
{
method: "GET",
ops: []int{
int(utilities.OpLitPush), 0,
int(utilities.OpPush), 0,
int(utilities.OpConcatN), 1,
int(utilities.OpCapture), 1,
int(utilities.OpLitPush), 2},
pool: []string{"foo", "id", "bar"},
},
},
reqMethod: "GET",
reqPath: "/foo/success%2fwith%2Fspace/bar",
headers: map[string]string{
"Content-Type": "application/json",
},
respStatus: http.StatusOK,
unescapingMode: runtime.UnescapingModeAllExceptReserved,
respContent: "GET /foo/{id=*}/bar",
},
{
patterns: []stubPattern{
{
method: "GET",
ops: []int{
int(utilities.OpLitPush), 0,
int(utilities.OpPushM), 0,
int(utilities.OpConcatN), 1,
int(utilities.OpCapture), 1,
},
pool: []string{"foo", "id", "bar"},
},
},
reqMethod: "GET",
reqPath: "/foo/success%2fwith%2Fspace",
headers: map[string]string{
"Content-Type": "application/json",
},
respStatus: http.StatusOK,
unescapingMode: runtime.UnescapingModeAllExceptReserved,
respContent: "GET /foo/{id=**}",
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var opts []runtime.ServeMuxOption
opts = append(opts, runtime.WithUnescapingMode(spec.unescapingMode))
if spec.disablePathLengthFallback {
opts = append(opts, runtime.WithDisablePathLengthFallback())
opts = append(opts,
runtime.WithDisablePathLengthFallback(),
)
}
mux := runtime.NewServeMux(opts...)
for _, p := range spec.patterns {
Expand Down
47 changes: 46 additions & 1 deletion runtime/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ func TestMatchWithBinding(t *testing.T) {
pool []string
path string
verb string
mode UnescapingMode

want map[string]string
}{
Expand Down Expand Up @@ -477,14 +478,58 @@ func TestMatchWithBinding(t *testing.T) {
"oname": "obj",
},
},
{
ops: []int{
int(utilities.OpLitPush), 0,
int(utilities.OpPush), 0,
int(utilities.OpConcatN), 1,
int(utilities.OpCapture), 1,
int(utilities.OpLitPush), 2,
},
pool: []string{"foo", "id", "bar"},
path: "foo/part1%2Fpart2/bar",
want: map[string]string{
"id": "part1/part2",
},
mode: UnescapingModeAllExceptReserved,
},
{
ops: []int{
int(utilities.OpLitPush), 0,
int(utilities.OpPushM), 0,
int(utilities.OpConcatN), 1,
int(utilities.OpCapture), 1,
},
pool: []string{"foo", "id"},
path: "foo/test%2Fbar",
want: map[string]string{
"id": "test%2Fbar",
},
mode: UnescapingModeAllExceptReserved,
},
{
ops: []int{
int(utilities.OpLitPush), 0,
int(utilities.OpPushM), 0,
int(utilities.OpConcatN), 1,
int(utilities.OpCapture), 1,
},
pool: []string{"foo", "id"},
path: "foo/test%2Fbar",
want: map[string]string{
"id": "test/bar",
},
mode: UnescapingModeAllCharacters,
},
} {
pat, err := NewPattern(validVersion, spec.ops, spec.pool, spec.verb)
if err != nil {
t.Errorf("NewPattern(%d, %v, %q, %q) failed with %v; want success", validVersion, spec.ops, spec.pool, spec.verb, err)
continue
}

got, err := pat.Match(segments(spec.path))
components, verb := segments(spec.path)
got, err := pat.MatchAndEscape(components, verb, spec.mode)
if err != nil {
t.Errorf("pat.Match(%q) failed with %v; want success; pattern = (%v, %q)", spec.path, err, spec.ops, spec.pool)
}
Expand Down

0 comments on commit a18708e

Please sign in to comment.