AFAIRE
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
2.7 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### kong ####### Copyright (c) 2021-2022 losyme ################################################ MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package router
import (
"net/http"
"net/http/httptest"
"testing"
"forge.chapril.org/losyme/kong/context"
)
var tests = []struct {
// Handle
HMethod string
HPath string
// Request
RMethod string
RPath string
// Want
WMatch bool
WParams map[string]string
}{
{
"GET", "",
"GET", "", true, nil,
},
{
"GET", "/",
"GET", "/", true, nil,
},
{
"GET", "////",
"GET", "/", true, nil,
},
{
"GET", "/one",
"GET", "/one", true, nil,
},
{
"GET", "/one/",
"GET", "/one/", true, nil,
},
{
"GET", "/one/two",
"GET", "/one/two", true, nil,
},
{
"GET", "/one/two/three",
"GET", "/one/two/three", true, nil,
},
{
"GET", "/one//two//three",
"GET", "/one/two/three", true, nil,
},
{
"GET", "/one/two/three",
"GET", "/one/three", false, nil,
},
{
"GET", "/one/two/three",
"get", "/one/two/three", true, nil,
},
{
"get", "/one/two/three",
"get", "/one/two/three", true, nil,
},
{
"get", "/one/two/three",
"GET", "/one/two/three", true, nil,
},
{
"GET", "/one/two",
"POST", "/one/two", false, nil,
},
{
"GET", "/user/abc",
"GET", "/user/123", false, nil,
},
{
"GET", "/user/:id",
"GET", "/user/123", true,
map[string]string{"id": "123"},
},
{
"delete", "/group/:gid//user/:uid",
"delete", "/group/123/user/456", true,
map[string]string{"gid": "123", "uid": "456"},
},
}
func TestRouter(t *testing.T) {
for n, test := range tests {
r := New(context.RendererTypeJSON)
match := false
r.Add(test.HMethod, test.HPath, func(c *context.Context) error {
match = true
if len(test.WParams) > 0 {
for k, v := range test.WParams {
s := c.Param(k)
if s != v {
t.Errorf("[N°%d] ctx(param), name: %q, want: %q, got: %q", n, k, v, s) /////////////////////////
}
}
}
return nil
})
req, err := http.NewRequest(test.RMethod, test.RPath, nil)
if err != nil {
t.Errorf("[N°%d] error(http.NewRequest) %v", n, err) ///////////////////////////////////////////////////////
}
rw := httptest.NewRecorder()
r.ServeHTTP(rw, req)
if match != test.WMatch {
t.Errorf("[N°%d] match, want: %v, got: %v", n, test.WMatch, match) /////////////////////////////////////////
}
}
}
/*
######################################################################################################## @(°_°)@ #######
*/