2021-09-27 23:30:43 +03:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-11-19 03:03:53 +03:00
|
|
|
func TestTrieBackwards(t *testing.T) {
|
|
|
|
_ = &Trie{}
|
|
|
|
}
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
func TestTrieWildcardPathPrefix(t *testing.T) {
|
|
|
|
var err error
|
2021-10-02 18:34:22 +03:00
|
|
|
type handler struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
tr := NewTrie()
|
2021-11-09 16:34:05 +03:00
|
|
|
if err = tr.Insert([]string{http.MethodPost}, "/v1/update", &handler{name: "post_update"}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err = tr.Insert([]string{http.MethodPost}, "/v1/*", &handler{name: "post_create"}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
h, _, ok := tr.Search(http.MethodPost, "/v1/test/one")
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected error handler not found")
|
|
|
|
}
|
|
|
|
if h.(*handler).name != "post_create" {
|
|
|
|
t.Fatalf("invalid handler %v", h)
|
|
|
|
}
|
|
|
|
h, _, ok = tr.Search(http.MethodPost, "/v1/update")
|
2021-10-02 18:34:22 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected error")
|
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
if h.(*handler).name != "post_update" {
|
|
|
|
t.Fatalf("invalid handler %v", h)
|
|
|
|
}
|
|
|
|
h, _, ok = tr.Search(http.MethodPost, "/v1/update/some/{x}")
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected error")
|
|
|
|
}
|
|
|
|
if h.(*handler).name != "post_create" {
|
2021-10-02 18:34:22 +03:00
|
|
|
t.Fatalf("invalid handler %v", h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
func TestTriePathPrefix(t *testing.T) {
|
2021-09-29 13:41:47 +03:00
|
|
|
type handler struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
tr := NewTrie()
|
2021-11-09 16:34:05 +03:00
|
|
|
_ = tr.Insert([]string{http.MethodPost}, "/v1/create/{id}", &handler{name: "post_create"})
|
|
|
|
_ = tr.Insert([]string{http.MethodPost}, "/v1/update/{id}", &handler{name: "post_update"})
|
|
|
|
_ = tr.Insert([]string{http.MethodPost}, "/", &handler{name: "post_wildcard"})
|
|
|
|
h, _, ok := tr.Search(http.MethodPost, "/")
|
2021-09-29 13:41:47 +03:00
|
|
|
if !ok {
|
|
|
|
t.Fatalf("unexpected error")
|
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
if h.(*handler).name != "post_wildcard" {
|
|
|
|
t.Fatalf("invalid handler %v", h)
|
|
|
|
}
|
2021-09-29 13:41:47 +03:00
|
|
|
}
|
|
|
|
|
2021-11-09 16:34:05 +03:00
|
|
|
func TestTrieFixedPattern(t *testing.T) {
|
2021-09-28 23:43:43 +03:00
|
|
|
type handler struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
tr := NewTrie()
|
2021-11-09 16:34:05 +03:00
|
|
|
_ = tr.Insert([]string{http.MethodPut}, "/v1/create/{id}", &handler{name: "pattern"})
|
|
|
|
_ = tr.Insert([]string{http.MethodPut}, "/v1/create/12", &handler{name: "fixed"})
|
|
|
|
h, _, ok := tr.Search(http.MethodPut, "/v1/create/12")
|
2021-09-28 23:43:43 +03:00
|
|
|
if !ok {
|
2021-11-09 16:34:05 +03:00
|
|
|
t.Fatalf("unexpected error")
|
2021-09-28 23:43:43 +03:00
|
|
|
}
|
2021-11-09 16:34:05 +03:00
|
|
|
if h.(*handler).name != "fixed" {
|
2021-09-28 23:43:43 +03:00
|
|
|
t.Fatalf("invalid handler %v", h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 23:30:43 +03:00
|
|
|
func TestTrieNoMatchMethod(t *testing.T) {
|
|
|
|
tr := NewTrie()
|
2021-11-09 16:34:05 +03:00
|
|
|
_ = tr.Insert([]string{http.MethodPut}, "/v1/create/{id}", nil)
|
2021-09-27 23:30:43 +03:00
|
|
|
_, _, ok := tr.Search(http.MethodPost, "/v1/create")
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("must be not found error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrieMatchRegexp(t *testing.T) {
|
2021-09-28 01:02:28 +03:00
|
|
|
type handler struct{}
|
2021-09-27 23:30:43 +03:00
|
|
|
tr := NewTrie()
|
2021-11-09 16:34:05 +03:00
|
|
|
_ = tr.Insert([]string{http.MethodPut}, "/v1/create/{category}/{id:[0-9]+}", &handler{})
|
2021-09-27 23:30:43 +03:00
|
|
|
_, params, ok := tr.Search(http.MethodPut, "/v1/create/test_cat/12345")
|
2021-09-29 13:19:07 +03:00
|
|
|
switch {
|
|
|
|
case !ok:
|
2021-09-27 23:30:43 +03:00
|
|
|
t.Fatalf("route not found")
|
2021-09-29 13:19:07 +03:00
|
|
|
case len(params) != 2:
|
2021-09-27 23:30:43 +03:00
|
|
|
t.Fatalf("param matching error %v", params)
|
2021-09-29 13:19:07 +03:00
|
|
|
case params["category"] != "test_cat":
|
2021-09-27 23:30:43 +03:00
|
|
|
t.Fatalf("param matching error %v", params)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTrieMatchRegexpFail(t *testing.T) {
|
2021-09-28 01:02:28 +03:00
|
|
|
type handler struct{}
|
2021-09-27 23:30:43 +03:00
|
|
|
tr := NewTrie()
|
2021-11-09 16:34:05 +03:00
|
|
|
_ = tr.Insert([]string{http.MethodPut}, "/v1/create/{id:[a-z]+}", &handler{})
|
2021-09-27 23:30:43 +03:00
|
|
|
_, _, ok := tr.Search(http.MethodPut, "/v1/create/12345")
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("route must not be not found")
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 01:02:28 +03:00
|
|
|
|
|
|
|
func TestTrieMatchLongest(t *testing.T) {
|
|
|
|
type handler struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
tr := NewTrie()
|
2021-11-09 16:34:05 +03:00
|
|
|
_ = tr.Insert([]string{http.MethodPut}, "/v1/create", &handler{name: "first"})
|
|
|
|
_ = tr.Insert([]string{http.MethodPut}, "/v1/create/{id:[0-9]+}", &handler{name: "second"})
|
2021-09-28 01:02:28 +03:00
|
|
|
if h, _, ok := tr.Search(http.MethodPut, "/v1/create/12345"); !ok {
|
|
|
|
t.Fatalf("route must be found")
|
|
|
|
} else if h.(*handler).name != "second" {
|
|
|
|
t.Fatalf("invalid handler found: %s != %s", h.(*handler).name, "second")
|
|
|
|
}
|
|
|
|
if h, _, ok := tr.Search(http.MethodPut, "/v1/create"); !ok {
|
|
|
|
t.Fatalf("route must be found")
|
|
|
|
} else if h.(*handler).name != "first" {
|
|
|
|
t.Fatalf("invalid handler found: %s != %s", h.(*handler).name, "first")
|
|
|
|
}
|
|
|
|
}
|