♻️ Refactored health endpoint
This commit is contained in:
parent
bb6beaba44
commit
f02bde306d
|
@ -1,27 +1,25 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"github.com/fasthttp/router"
|
||||
http "github.com/valyala/fasthttp"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"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 {
|
||||
return &RequestHandler{}
|
||||
func NewHandler() *Handler {
|
||||
return &Handler{}
|
||||
}
|
||||
|
||||
func (h *RequestHandler) Register(r *router.Router) {
|
||||
chain := middleware.Chain{
|
||||
middleware.LogFmt(),
|
||||
}
|
||||
|
||||
r.GET("/health", chain.RequestHandler(h.read))
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
http.HandlerFunc(middleware.HandlerFunc(h.handleFunc).Intercept(middleware.LogFmt())).ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (h *RequestHandler) read(ctx *http.RequestCtx) {
|
||||
ctx.SuccessString(common.MIMEApplicationJSONCharsetUTF8, `{"ok": true}`)
|
||||
func (h *Handler) handleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(common.HeaderContentType, common.MIMETextPlainCharsetUTF8)
|
||||
fmt.Fprint(w, `👌`)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
|
|
@ -1,42 +1,34 @@
|
|||
package http_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/fasthttp/router"
|
||||
http "github.com/valyala/fasthttp"
|
||||
|
||||
delivery "source.toby3d.me/toby3d/auth/internal/health/delivery/http"
|
||||
"source.toby3d.me/toby3d/auth/internal/testing/httptest"
|
||||
)
|
||||
|
||||
func TestRequestHandler(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r := router.New()
|
||||
delivery.NewRequestHandler().Register(r)
|
||||
req := httptest.NewRequest(http.MethodGet, "https://example.com/health", nil)
|
||||
w := httptest.NewRecorder()
|
||||
delivery.NewHandler().ServeHTTP(w, req)
|
||||
|
||||
client, _, cleanup := httptest.New(t, r.Handler)
|
||||
t.Cleanup(cleanup)
|
||||
resp := w.Result()
|
||||
|
||||
const requestURL = "https://app.example.com/health"
|
||||
req, resp := httptest.NewRequest(http.MethodGet, requestURL, nil), http.AcquireResponse()
|
||||
if exp := http.StatusOK; resp.StatusCode != exp {
|
||||
t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, resp.StatusCode, exp)
|
||||
}
|
||||
|
||||
t.Cleanup(func() {
|
||||
http.ReleaseRequest(req)
|
||||
http.ReleaseResponse(resp)
|
||||
})
|
||||
|
||||
if err := client.Do(req, resp); err != nil {
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if result := resp.StatusCode(); result != http.StatusOK {
|
||||
t.Errorf("GET %s = %d, want %d", requestURL, result, http.StatusOK)
|
||||
}
|
||||
|
||||
const expBody = `{"ok": true}`
|
||||
if result := string(resp.Body()); result != expBody {
|
||||
t.Errorf("GET %s = %s, want %s", requestURL, result, expBody)
|
||||
if exp := `👌`; string(body) != exp {
|
||||
t.Errorf("%s %s = '%s', want '%s'", req.Method, req.RequestURI, body, exp)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue