♻️ Refactored health endpoint

This commit is contained in:
Maxim Lebedev 2023-01-02 06:11:47 +06:00
parent bb6beaba44
commit f02bde306d
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
2 changed files with 25 additions and 35 deletions

View File

@ -1,27 +1,25 @@
package http package http
import ( import (
"github.com/fasthttp/router" "fmt"
http "github.com/valyala/fasthttp" "net/http"
"source.toby3d.me/toby3d/auth/internal/common" "source.toby3d.me/toby3d/auth/internal/common"
"source.toby3d.me/toby3d/middleware" "source.toby3d.me/toby3d/auth/internal/middleware"
) )
type RequestHandler struct{} type Handler struct{}
func NewRequestHandler() *RequestHandler { func NewHandler() *Handler {
return &RequestHandler{} return &Handler{}
} }
func (h *RequestHandler) Register(r *router.Router) { func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
chain := middleware.Chain{ http.HandlerFunc(middleware.HandlerFunc(h.handleFunc).Intercept(middleware.LogFmt())).ServeHTTP(w, r)
middleware.LogFmt(),
}
r.GET("/health", chain.RequestHandler(h.read))
} }
func (h *RequestHandler) read(ctx *http.RequestCtx) { func (h *Handler) handleFunc(w http.ResponseWriter, r *http.Request) {
ctx.SuccessString(common.MIMEApplicationJSONCharsetUTF8, `{"ok": true}`) w.Header().Set(common.HeaderContentType, common.MIMETextPlainCharsetUTF8)
fmt.Fprint(w, `👌`)
w.WriteHeader(http.StatusOK)
} }

View File

@ -1,42 +1,34 @@
package http_test package http_test
import ( import (
"io"
"net/http/httptest"
"testing" "testing"
"github.com/fasthttp/router"
http "github.com/valyala/fasthttp" http "github.com/valyala/fasthttp"
delivery "source.toby3d.me/toby3d/auth/internal/health/delivery/http" delivery "source.toby3d.me/toby3d/auth/internal/health/delivery/http"
"source.toby3d.me/toby3d/auth/internal/testing/httptest"
) )
func TestRequestHandler(t *testing.T) { func TestRequestHandler(t *testing.T) {
t.Parallel() t.Parallel()
r := router.New() req := httptest.NewRequest(http.MethodGet, "https://example.com/health", nil)
delivery.NewRequestHandler().Register(r) w := httptest.NewRecorder()
delivery.NewHandler().ServeHTTP(w, req)
client, _, cleanup := httptest.New(t, r.Handler) resp := w.Result()
t.Cleanup(cleanup)
const requestURL = "https://app.example.com/health" if exp := http.StatusOK; resp.StatusCode != exp {
req, resp := httptest.NewRequest(http.MethodGet, requestURL, nil), http.AcquireResponse() t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, resp.StatusCode, exp)
}
t.Cleanup(func() { body, err := io.ReadAll(resp.Body)
http.ReleaseRequest(req) if err != nil {
http.ReleaseResponse(resp)
})
if err := client.Do(req, resp); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if result := resp.StatusCode(); result != http.StatusOK { if exp := `👌`; string(body) != exp {
t.Errorf("GET %s = %d, want %d", requestURL, result, http.StatusOK) t.Errorf("%s %s = '%s', want '%s'", req.Method, req.RequestURI, body, exp)
}
const expBody = `{"ok": true}`
if result := string(resp.Body()); result != expBody {
t.Errorf("GET %s = %s, want %s", requestURL, result, expBody)
} }
} }