move to GitHub.
This commit is contained in:
@ -1,11 +1,18 @@
|
||||
/*
|
||||
Copyright (c) JSC iCore.
|
||||
|
||||
This source code is licensed under the MIT license found in the
|
||||
LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package stat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/i-core/rlog"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.i-core.ru/logutil"
|
||||
)
|
||||
|
||||
// Handler provides HTTP handlers for health checking and versioning.
|
||||
@ -20,14 +27,14 @@ func NewHandler(version string) *Handler {
|
||||
|
||||
// AddRoutes registers all required routes for the package stat.
|
||||
func (h *Handler) AddRoutes(apply func(m, p string, h http.Handler, mws ...func(http.Handler) http.Handler)) {
|
||||
apply(http.MethodGet, "/health/alive", newHealthAliveAndReadyHandler())
|
||||
apply(http.MethodGet, "/health/ready", newHealthAliveAndReadyHandler())
|
||||
apply(http.MethodGet, "/health/alive", newHealthHandler())
|
||||
apply(http.MethodGet, "/health/ready", newHealthHandler())
|
||||
apply(http.MethodGet, "/version", newVersionHandler(h.version))
|
||||
}
|
||||
|
||||
func newHealthAliveAndReadyHandler() http.HandlerFunc {
|
||||
func newHealthHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log := logutil.FromContext(r.Context())
|
||||
log := rlog.FromContext(r.Context())
|
||||
resp := struct {
|
||||
Status string `json:"status"`
|
||||
}{
|
||||
@ -44,7 +51,7 @@ func newHealthAliveAndReadyHandler() http.HandlerFunc {
|
||||
|
||||
func newVersionHandler(version string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log := logutil.FromContext(r.Context())
|
||||
log := rlog.FromContext(r.Context())
|
||||
resp := struct {
|
||||
Version string `json:"version"`
|
||||
}{
|
||||
|
64
internal/stat/stat_test.go
Normal file
64
internal/stat/stat_test.go
Normal file
@ -0,0 +1,64 @@
|
||||
package stat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/i-core/routegroup"
|
||||
)
|
||||
|
||||
func TestHealthHandler(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
h := newHealthHandler()
|
||||
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "http://example.org", nil))
|
||||
testResp(t, rr, http.StatusOK, "application/json", map[string]interface{}{"status": "ok"})
|
||||
}
|
||||
|
||||
func TestVersionHandler(t *testing.T) {
|
||||
rr := httptest.NewRecorder()
|
||||
h := newVersionHandler("test-version")
|
||||
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "http://example.org", nil))
|
||||
testResp(t, rr, http.StatusOK, "application/json", map[string]interface{}{"version": "test-version"})
|
||||
}
|
||||
|
||||
func TestStatHandler(t *testing.T) {
|
||||
var (
|
||||
rr *httptest.ResponseRecorder
|
||||
router = routegroup.NewRouter()
|
||||
)
|
||||
router.AddRoutes(NewHandler("test-version"), "/stat")
|
||||
|
||||
rr = httptest.NewRecorder()
|
||||
router.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/stat/health/alive", nil))
|
||||
testResp(t, rr, http.StatusOK, "application/json", map[string]interface{}{"status": "ok"})
|
||||
|
||||
rr = httptest.NewRecorder()
|
||||
router.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/stat/health/ready", nil))
|
||||
testResp(t, rr, http.StatusOK, "application/json", map[string]interface{}{"status": "ok"})
|
||||
|
||||
rr = httptest.NewRecorder()
|
||||
router.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/stat/version", nil))
|
||||
testResp(t, rr, http.StatusOK, "application/json", map[string]interface{}{"version": "test-version"})
|
||||
}
|
||||
|
||||
func testResp(t *testing.T, rr *httptest.ResponseRecorder, wantStatus int, wantMime string, wantBody interface{}) {
|
||||
if rr.Code != wantStatus {
|
||||
t.Errorf("got status %d, want status %d", rr.Code, wantStatus)
|
||||
}
|
||||
|
||||
if gotMime := rr.Header().Get("Content-Type"); gotMime != wantMime {
|
||||
t.Errorf("got content type %q, want content type %q", gotMime, wantMime)
|
||||
}
|
||||
|
||||
var gotBody interface{}
|
||||
if err := json.NewDecoder(rr.Body).Decode(&gotBody); err != nil {
|
||||
t.Fatalf("failed to decode the request body: %s", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(gotBody, wantBody) {
|
||||
t.Errorf("got body %#v, want body %#v", gotBody, wantBody)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user