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:
53
modules/surf/locale_from_principal_test.go
Normal file
53
modules/surf/locale_from_principal_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package surf
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/towel"
|
||||
)
|
||||
|
||||
func TestLocaleFromPrincipal(t *testing.T) {
|
||||
var got string
|
||||
var ok bool
|
||||
h := LocaleFromPrincipal(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
got, ok = towel.Locale(r.Context())
|
||||
}))
|
||||
|
||||
ctx := towel.WithLocale(t.Context(), "en")
|
||||
ctx = bouncer.WithUser(ctx, &bouncer.Principal{PreferredLocale: "pl"})
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
|
||||
h.ServeHTTP(httptest.NewRecorder(), req)
|
||||
if !ok || got != "pl" {
|
||||
t.Fatalf("locale = %q ok=%t", got, ok)
|
||||
}
|
||||
|
||||
got, ok = "", false
|
||||
ctx = towel.WithLocale(t.Context(), "en")
|
||||
ctx = bouncer.WithUser(ctx, &bouncer.Principal{})
|
||||
req = httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
|
||||
h.ServeHTTP(httptest.NewRecorder(), req)
|
||||
if !ok || got != "en" {
|
||||
t.Fatalf("empty preferred locale = %q ok=%t", got, ok)
|
||||
}
|
||||
|
||||
got, ok = "", false
|
||||
req = httptest.NewRequest(http.MethodGet, "/", nil).WithContext(towel.WithLocale(t.Context(), "de"))
|
||||
h.ServeHTTP(httptest.NewRecorder(), req)
|
||||
if !ok || got != "de" {
|
||||
t.Fatalf("no principal locale = %q ok=%t", got, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRouterRegistersLocaleFromPrincipal(t *testing.T) {
|
||||
r, err := BuildRouter(nil, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mw, ok := r.named["locale.from-principal"]
|
||||
if !ok || mw.pluginID != "surf" || mw.fn == nil {
|
||||
t.Fatalf("registration ok=%t mw=%+v", ok, mw)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user