refactor(10.2-01): nest framework packages under modules
- Move remaining beach packages and embedded admin assets\n- Rewrite framework, example, build, and gate paths
This commit is contained in:
63
modules/surf/params_test.go
Normal file
63
modules/surf/params_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user