Skip to content

Commit

Permalink
Rewrite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit committed May 20, 2024
1 parent 72c218f commit 217e5b3
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions quesma/quesma/mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,58 @@ import (
"testing"
)

func TestMatches_ShouldIgnoreTrailingSlash(t *testing.T) {
func TestPathRouter_Matches_ShouldIgnoreTrailingSlash(t *testing.T) {
router := NewPathRouter()
router.RegisterPath("/:index/_bulk", "POST", mockHandler)
router.RegisterPath("/:index/_doc", "POST", mockHandler)
router.RegisterPath("/:index/_count", "GET", mockHandler)

assert.True(t, router.Matches("/i1,i2/_count", "GET", ""))
assert.True(t, router.Matches("/_all/_count/", "GET", ""))

assert.True(t, router.Matches("/index1/_doc", "POST", ""))
assert.True(t, router.Matches("/index2/_doc/", "POST", ""))

assert.True(t, router.Matches("/indexABC/_bulk", "POST", ""))
assert.True(t, router.Matches("/indexABC/_bulk/", "POST", ""))
type args struct {
path string
httpMethod string
body string
}
tests := []struct {
args args
want bool
}{
{args: args{path: "/i1,i2/_count", httpMethod: "GET", body: ""}, want: true},
{args: args{path: "/_all/_count/", httpMethod: "GET", body: ""}, want: true},
{args: args{path: "/index1/_doc", httpMethod: "POST", body: ""}, want: true},
{args: args{path: "/index2/_doc/", httpMethod: "POST", body: ""}, want: true},
{args: args{path: "/indexABC/_bulk", httpMethod: "POST", body: ""}, want: true},
{args: args{path: "/indexABC/_bulk/", httpMethod: "POST", body: ""}, want: true},
}
for _, tt := range tests {
t.Run(tt.args.httpMethod+" "+tt.args.path, func(t *testing.T) {
_, _, found := router.Matches(tt.args.path, tt.args.httpMethod, tt.args.body)
assert.Equalf(t, tt.want, found, "Matches(%v, %v, %v)", tt.args.path, tt.args.httpMethod, tt.args.body)
})
}
}

func TestShouldMatchMultipleHttpMethods(t *testing.T) {
router := NewPathRouter()
router.RegisterPathMatcher("/:index/_bulk", []string{"POST", "GET"}, always, mockHandler)

assert.True(t, router.Matches("/index1/_bulk", "POST", ""))
assert.True(t, router.Matches("/index1/_bulk", "GET", ""))
type args struct {
path string
httpMethod string
body string
}
tests := []struct {
args args
want bool
}{
{args: args{path: "/index1/_bulk", httpMethod: "POST", body: ""}, want: true},
{args: args{path: "/index1/_bulk", httpMethod: "GET", body: ""}, want: true},
}
for _, tt := range tests {
t.Run(tt.args.httpMethod+" "+tt.args.path, func(t *testing.T) {
_, _, found := router.Matches(tt.args.path, tt.args.httpMethod, tt.args.body)
assert.Equalf(t, tt.want, found, "Matches(%v, %v, %v)", tt.args.path, tt.args.httpMethod, tt.args.body)
})
}
}

func always(_ map[string]string, _ string) bool {
Expand Down

0 comments on commit 217e5b3

Please sign in to comment.