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:
126
modules/surf/routetable_test.go
Normal file
126
modules/surf/routetable_test.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package surf
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"git.golem15.com/golem15/summercms/modules/party"
|
||||
)
|
||||
|
||||
func TestRouteTableRawFlagAndStickyInheritance(t *testing.T) {
|
||||
r := New(nil)
|
||||
r.BindPlugin("golem15.demo")
|
||||
r.Get("/plain", func(http.ResponseWriter, *http.Request) {})
|
||||
r.Group("/g", nil, func(g pact.Router) {
|
||||
g.Get("/nested", func(http.ResponseWriter, *http.Request) {})
|
||||
})
|
||||
r.GroupRaw("/raw", nil, func(g pact.Router) {
|
||||
g.Get("/a", func(http.ResponseWriter, *http.Request) {})
|
||||
g.Group("/child", nil, func(c pact.Router) {
|
||||
c.Get("/b", func(http.ResponseWriter, *http.Request) {})
|
||||
})
|
||||
})
|
||||
|
||||
byPattern := map[string]RouteInfo{}
|
||||
for _, rt := range r.Routes() {
|
||||
byPattern[rt.Pattern] = rt
|
||||
}
|
||||
|
||||
want := map[string]bool{
|
||||
"/plain": false,
|
||||
"/g/nested": false,
|
||||
"/raw/a": true,
|
||||
"/raw/child/b": true,
|
||||
}
|
||||
for pattern, raw := range want {
|
||||
got, ok := byPattern[pattern]
|
||||
if !ok {
|
||||
t.Fatalf("missing route %s in %v", pattern, keys(byPattern))
|
||||
}
|
||||
if got.Raw != raw {
|
||||
t.Fatalf("%s Raw = %v, want %v", pattern, got.Raw, raw)
|
||||
}
|
||||
if got.Method != http.MethodGet {
|
||||
t.Fatalf("%s Method = %s", pattern, got.Method)
|
||||
}
|
||||
if got.PluginID != "golem15.demo" {
|
||||
t.Fatalf("%s PluginID = %s", pattern, got.PluginID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func keys(m map[string]RouteInfo) []string {
|
||||
out := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
out = append(out, k)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestRouteTableDoesNotAliasInternalSlice(t *testing.T) {
|
||||
r := New(nil)
|
||||
r.BindPlugin("golem15.demo")
|
||||
r.Group("/api", Use("jwt.auth"), func(g pact.Router) {
|
||||
g.Get("/items", func(http.ResponseWriter, *http.Request) {})
|
||||
})
|
||||
first := r.Routes()
|
||||
if len(first) != 1 || len(first[0].Middleware) != 1 {
|
||||
t.Fatalf("got %+v", first)
|
||||
}
|
||||
first[0].Middleware[0] = "mutated"
|
||||
first[0].Pattern = "/changed"
|
||||
second := r.Routes()
|
||||
if second[0].Middleware[0] != "jwt.auth" {
|
||||
t.Fatalf("internal middleware aliased: %v", second[0].Middleware)
|
||||
}
|
||||
if second[0].Pattern != "/api/items" {
|
||||
t.Fatalf("internal pattern aliased: %s", second[0].Pattern)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRawGroupHouseMiddlewareRefusedAtBuild(t *testing.T) {
|
||||
identity := func(next http.Handler) http.Handler { return next }
|
||||
p := houseRoutePlugin{
|
||||
id: "golem15.demo",
|
||||
house: map[string]pact.Middleware{
|
||||
"house.err": identity,
|
||||
},
|
||||
rawUse: []string{"house.err"},
|
||||
}
|
||||
_, err := BuildRouter(backpack.New(nil), []party.Plugin{p})
|
||||
if err == nil {
|
||||
t.Fatal("want BuildRouter error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "house.err") {
|
||||
t.Fatalf("want middleware name in error, got %v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "golem15.demo") {
|
||||
t.Fatalf("want plugin in error, got %v", err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "raw group cannot use house-envelope middleware") {
|
||||
t.Fatalf("want raw-group refusal, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type houseRoutePlugin struct {
|
||||
id string
|
||||
house map[string]pact.Middleware
|
||||
mw map[string]pact.Middleware
|
||||
rawUse []string
|
||||
}
|
||||
|
||||
func (p houseRoutePlugin) ID() string { return p.id }
|
||||
func (p houseRoutePlugin) Requires() []string { return nil }
|
||||
func (p houseRoutePlugin) Register(*backpack.App) error { return nil }
|
||||
func (p houseRoutePlugin) Boot(*backpack.App) error { return nil }
|
||||
func (p houseRoutePlugin) Middlewares() map[string]pact.Middleware { return p.mw }
|
||||
func (p houseRoutePlugin) HouseMiddlewares() map[string]pact.Middleware { return p.house }
|
||||
func (p houseRoutePlugin) Routes(r pact.Router) error {
|
||||
r.GroupRaw("/oauth", Use(p.rawUse...), func(g pact.Router) {
|
||||
g.Get("/x", func(http.ResponseWriter, *http.Request) {})
|
||||
})
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user