- Move remaining beach packages and embedded admin assets\n- Rewrite framework, example, build, and gate paths
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package surf
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestIntParamMalformedIsFalse(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/items/abc", nil)
|
|
req.SetPathValue("id", "abc")
|
|
if _, ok := IntParam(req, "id"); ok {
|
|
t.Fatal("malformed id must be false")
|
|
}
|
|
req.SetPathValue("id", "12")
|
|
n, ok := IntParam(req, "id")
|
|
if !ok || n != 12 {
|
|
t.Fatalf("got %d %t", n, ok)
|
|
}
|
|
req.SetPathValue("id", "0")
|
|
if _, ok := IntParam(req, "id"); ok {
|
|
t.Fatal("non-positive id must be false")
|
|
}
|
|
req.SetPathValue("id", "")
|
|
if _, ok := IntParam(req, "id"); ok {
|
|
t.Fatal("missing id must be false")
|
|
}
|
|
if _, ok := IntParam(nil, "id"); ok {
|
|
t.Fatal("nil request must be false")
|
|
}
|
|
}
|
|
|
|
func TestRegexCompilesAtRegistration(t *testing.T) {
|
|
c, err := Regex("id", `[0-9]+`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !c.match("12") || c.match("nope") || c.match("") {
|
|
t.Fatal("regex match")
|
|
}
|
|
if _, err := Regex("id", "["); err == nil {
|
|
t.Fatal("want invalid regex error")
|
|
}
|
|
if _, err := Regex("", `[0-9]+`); err == nil {
|
|
t.Fatal("want empty param error")
|
|
}
|
|
}
|
|
|
|
func TestEnumAllowListDoesNotBuildRegexFromValues(t *testing.T) {
|
|
c, err := Enum("kind", "widget", "gadget")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.re != nil {
|
|
t.Fatal("enum must not compile a regex")
|
|
}
|
|
if !c.match("widget") || !c.match("gadget") || c.match("other") || c.match("widget|gadget") {
|
|
t.Fatal("enum match")
|
|
}
|
|
if _, err := Enum("kind"); err == nil {
|
|
t.Fatal("want empty enum error")
|
|
}
|
|
}
|