mirror of
https://github.com/wahyd4/loginsrv.git
synced 2026-08-09 04:46:29 +10:00
Add a test for /login
This commit is contained in:
@@ -44,4 +44,42 @@ func Test_ServeHTTP_200(t *testing.T) {
|
||||
if status != 200 {
|
||||
t.Errorf("Expected returned status code to be %d, got %d", 0, status)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ServeHTTP_login(t *testing.T) {
|
||||
//Set the ServeHTTP *http.Request
|
||||
r, err := http.NewRequest("GET", "/login", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to create request: %v", err)
|
||||
}
|
||||
|
||||
//Set the ServeHTTP http.ResponseWriter
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
//Set the CaddyHandler config
|
||||
configh := login.DefaultConfig()
|
||||
configh.Backends = login.Options{"simple": {"bob": "secret"}}
|
||||
loginh, err := login.NewHandler(configh)
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got: %v", err)
|
||||
}
|
||||
|
||||
//Set the CaddyHandler that will use ServeHTTP
|
||||
h := &CaddyHandler{
|
||||
next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
return http.StatusOK, nil // not t.Fatalf, or we will not see what other methods yield
|
||||
}),
|
||||
config: login.DefaultConfig(),
|
||||
loginHandler: loginh,
|
||||
}
|
||||
|
||||
status, err := h.ServeHTTP(w, r)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil error, got: %v", err)
|
||||
}
|
||||
|
||||
if status != 0 {
|
||||
t.Errorf("Expected returned status code to be %d, got %d", 0, status)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user