mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-12 04:04:26 +00:00
feat(oidcgate): healthz and readyz endpoints
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
|
||||
// readyReporter is satisfied by *traefikoidc.TraefikOidc via its Ready() method.
|
||||
type readyReporter interface {
|
||||
Ready() bool
|
||||
}
|
||||
|
||||
func newHealthzHandler() http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
})
|
||||
}
|
||||
|
||||
func newReadyzHandler(r readyReporter) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
|
||||
if r.Ready() {
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
rw.WriteHeader(http.StatusServiceUnavailable)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type readyStub struct{ ready bool }
|
||||
|
||||
func (r *readyStub) Ready() bool { return r.ready }
|
||||
|
||||
func TestHealthz_Always200(t *testing.T) {
|
||||
h := newHealthzHandler()
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/healthz", nil))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("healthz: want 200, got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyz_503BeforeDiscovery(t *testing.T) {
|
||||
h := newReadyzHandler(&readyStub{ready: false})
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/readyz", nil))
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("readyz pre-discovery: want 503, got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyz_200AfterDiscovery(t *testing.T) {
|
||||
h := newReadyzHandler(&readyStub{ready: true})
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/readyz", nil))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("readyz post-discovery: want 200, got %d", rec.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user