2023-09-05 06:02:56 +04:00
|
|
|
package httpr
|
2023-05-28 03:46:09 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-07-23 23:26:35 +04:00
|
|
|
"net/http/httptest"
|
2023-05-28 03:46:09 +04:00
|
|
|
"os"
|
2023-07-23 23:26:35 +04:00
|
|
|
"strings"
|
2023-05-28 03:46:09 +04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test(t *testing.T) {
|
2023-09-05 06:02:56 +04:00
|
|
|
r := New()
|
2023-05-28 03:46:09 +04:00
|
|
|
|
|
|
|
err := r.Handler(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.Handler(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("path redefinition wasn't catched")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.Handler(http.MethodGet, "/a/b", func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.Handler(http.MethodGet, "/:a/:b", func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.Handler(http.MethodGet, "/:a/:lol", func(w http.ResponseWriter, r *http.Request) {})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("here is a different last param name is supplied, should be catched")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.ServeStatic("/assets/*filepath", http.FS(os.DirFS(".")))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.ServeStatic("/assets/*filepath/*filepath", nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("multiple catch-all params wasn't catched")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-07-23 23:26:35 +04:00
|
|
|
|
|
|
|
func TestPaths(t *testing.T) {
|
|
|
|
found := false
|
|
|
|
|
2023-09-05 06:02:56 +04:00
|
|
|
r := New()
|
2023-07-23 23:26:35 +04:00
|
|
|
|
|
|
|
err := r.Handler(http.MethodGet, "/:lel", func(w http.ResponseWriter, r *http.Request) { found = true })
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) { found = false }
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
p := "/xmpp://me@arav.su"
|
|
|
|
req := httptest.NewRequest(http.MethodGet, p, strings.NewReader(""))
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
if found {
|
|
|
|
t.Error("Path", p, "should return 404")
|
|
|
|
}
|
|
|
|
|
|
|
|
p = "/lel"
|
|
|
|
req = httptest.NewRequest(http.MethodGet, p, strings.NewReader(""))
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
if !found {
|
|
|
|
t.Error("Path", p, "should return 200")
|
|
|
|
}
|
|
|
|
|
|
|
|
p = "/lel/lol"
|
|
|
|
req = httptest.NewRequest(http.MethodGet, p, strings.NewReader(""))
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
if found {
|
|
|
|
t.Error("Path", p, "should return 404")
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 18:42:46 +04:00
|
|
|
|
|
|
|
func TestSubPaths(t *testing.T) {
|
|
|
|
found := true
|
|
|
|
|
2023-09-05 06:02:56 +04:00
|
|
|
r := New()
|
2023-08-11 18:42:46 +04:00
|
|
|
|
|
|
|
s := r.Sub("/api/v1")
|
|
|
|
|
|
|
|
err := s.Handler(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) { found = true })
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.Handler(http.MethodGet, "/test", func(w http.ResponseWriter, r *http.Request) { found = true })
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) { found = false }
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
p := "/api/v1/"
|
|
|
|
req := httptest.NewRequest(http.MethodGet, p, strings.NewReader(""))
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
if !found {
|
|
|
|
t.Error("Path", p, "should return 200")
|
|
|
|
}
|
|
|
|
|
|
|
|
p = "/api/v1/test"
|
|
|
|
req = httptest.NewRequest(http.MethodGet, p, strings.NewReader(""))
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
if !found {
|
|
|
|
t.Error("Path", p, "should return 200")
|
|
|
|
}
|
|
|
|
|
|
|
|
p = "/api/v1/nonexistent"
|
|
|
|
req = httptest.NewRequest(http.MethodGet, p, strings.NewReader(""))
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
if found {
|
|
|
|
t.Error(found, "Path", p, "should return 404")
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 06:03:29 +04:00
|
|
|
|
|
|
|
func TestPathParsing(t *testing.T) {
|
|
|
|
p, err := newPath("/api/v1/../.")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
t.Log(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
const testStr = "/api/v1/foo/bar/baz/abc/def/fucc/b0y/of/a/local/dungeon/master/got/his/ass/fisted/for/free/and/he/was/absolutely/happy/with/that/"
|
|
|
|
|
|
|
|
func BenchmarkPatternPathParsing(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
p, err := newPath(testStr)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.Log(len(p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkServePathParsing(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
p, err := newServePath(testStr)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
b.Log(len(p))
|
|
|
|
}
|
|
|
|
}
|