2015-01-05 10:18:41 +02:00
|
|
|
package authboss
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-01-15 23:24:12 +02:00
|
|
|
type clientStoreMock struct{}
|
|
|
|
|
|
|
|
func (c clientStoreMock) Get(_ string) (string, bool) { return "", false }
|
|
|
|
func (c clientStoreMock) Put(_, _ string) {}
|
2015-01-16 02:04:33 +02:00
|
|
|
func (c clientStoreMock) Del(_ string) {}
|
2015-01-15 23:24:12 +02:00
|
|
|
|
2015-01-05 10:18:41 +02:00
|
|
|
func TestMain(main *testing.M) {
|
|
|
|
RegisterModule("testmodule", testMod)
|
2015-01-11 08:52:39 +02:00
|
|
|
Init(NewConfig())
|
2015-01-05 10:18:41 +02:00
|
|
|
code := main.Run()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthBossInit(t *testing.T) {
|
|
|
|
c := NewConfig()
|
|
|
|
|
|
|
|
err := Init(c)
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "storer") {
|
|
|
|
t.Error("Expected error about a storer, got:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Storer = testStorer(0)
|
|
|
|
err = Init(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
if testMod.c == nil {
|
|
|
|
t.Error("Expected the modules to be passed the config.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthBossRouter(t *testing.T) {
|
|
|
|
c := NewConfig()
|
2015-01-17 08:03:40 +02:00
|
|
|
c.Storer = testStorer(0)
|
2015-01-16 00:01:01 +02:00
|
|
|
c.CookieStoreMaker = func(_ http.ResponseWriter, _ *http.Request) ClientStorer {
|
2015-01-15 23:24:12 +02:00
|
|
|
return clientStoreMock{}
|
|
|
|
}
|
|
|
|
c.SessionStoreMaker = SessionStoreMaker(c.CookieStoreMaker)
|
2015-01-05 10:18:41 +02:00
|
|
|
c.MountPath = "/candycanes"
|
|
|
|
|
2015-01-17 08:03:40 +02:00
|
|
|
if err := Init(c); err != nil {
|
|
|
|
t.Error("Unexpected error:", err)
|
|
|
|
}
|
|
|
|
router := NewRouter()
|
2015-01-05 10:18:41 +02:00
|
|
|
|
|
|
|
r, _ := http.NewRequest("GET", "/candycanes/testroute", nil)
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
|
|
|
router.ServeHTTP(response, r)
|
|
|
|
|
2015-01-11 08:52:39 +02:00
|
|
|
if response.Header().Get("testhandler") != "test" {
|
2015-01-05 10:18:41 +02:00
|
|
|
t.Error("Expected a header to have been set.")
|
|
|
|
}
|
|
|
|
}
|