2016-07-26 01:30:28 +05:30
|
|
|
package server
|
2016-10-05 20:31:35 +05:30
|
|
|
|
|
|
|
import (
|
2017-03-09 00:03:19 +05:30
|
|
|
"context"
|
2019-02-04 23:15:13 +05:30
|
|
|
"errors"
|
2016-10-05 20:31:35 +05:30
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2019-02-04 23:15:13 +05:30
|
|
|
|
|
|
|
"github.com/dexidp/dex/storage"
|
2016-10-05 20:31:35 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleHealth(t *testing.T) {
|
2016-10-13 07:21:32 +05:30
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2016-10-14 06:45:20 +05:30
|
|
|
httpServer, server := newTestServer(ctx, t, nil)
|
2016-10-05 20:31:35 +05:30
|
|
|
defer httpServer.Close()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
2019-02-04 23:15:13 +05:30
|
|
|
server.ServeHTTP(rr, httptest.NewRequest("GET", "/healthz", nil))
|
2016-10-05 20:31:35 +05:30
|
|
|
if rr.Code != http.StatusOK {
|
|
|
|
t.Errorf("expected 200 got %d", rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-02-04 23:15:13 +05:30
|
|
|
|
|
|
|
type badStorage struct {
|
|
|
|
storage.Storage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *badStorage) CreateAuthRequest(r storage.AuthRequest) error {
|
|
|
|
return errors.New("storage unavailable")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleHealthFailure(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
httpServer, server := newTestServer(ctx, t, func(c *Config) {
|
|
|
|
c.Storage = &badStorage{c.Storage}
|
|
|
|
})
|
|
|
|
defer httpServer.Close()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
server.ServeHTTP(rr, httptest.NewRequest("GET", "/healthz", nil))
|
|
|
|
if rr.Code != http.StatusInternalServerError {
|
|
|
|
t.Errorf("expected 500 got %d", rr.Code)
|
|
|
|
}
|
|
|
|
}
|