From 19836622167b77c7555cbe440bb8631ee3b149ad Mon Sep 17 00:00:00 2001 From: yequari Date: Mon, 26 May 2025 00:57:01 -0700 Subject: [PATCH] unit test scaffolding --- cmd/web/handlers.go | 4 +++ cmd/web/handlers_guestbook_test.go | 19 ++++++++++++ cmd/web/routes.go | 2 ++ cmd/web/testutils_test.go | 50 ++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 cmd/web/handlers_guestbook_test.go create mode 100644 cmd/web/testutils_test.go diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 9534a56..a72a3b4 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -17,3 +17,7 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) { func (app *application) notImplemented(w http.ResponseWriter, r *http.Request) { views.ComingSoon("Coming Soon", app.newCommonData(r)).Render(r.Context(), w) } + +func ping(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("OK")) +} diff --git a/cmd/web/handlers_guestbook_test.go b/cmd/web/handlers_guestbook_test.go new file mode 100644 index 0000000..5ee8aeb --- /dev/null +++ b/cmd/web/handlers_guestbook_test.go @@ -0,0 +1,19 @@ +package main + +import ( + "net/http" + "testing" + + "git.32bit.cafe/32bitcafe/guestbook/internal/assert" +) + +func TestPing(t *testing.T) { + app := newTestApplication(t) + ts := newTestServer(t, app.routes()) + defer ts.Close() + + code, _, body := ts.get(t, "/ping") + + assert.Equal(t, code, http.StatusOK) + assert.Equal(t, body, "OK") +} diff --git a/cmd/web/routes.go b/cmd/web/routes.go index c2fe48a..31d0d41 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -11,6 +11,8 @@ func (app *application) routes() http.Handler { mux := http.NewServeMux() mux.Handle("GET /static/", http.FileServerFS(ui.Files)) + mux.HandleFunc("GET /ping", ping) + dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate) standard := alice.New(app.recoverPanic, app.logRequest, commonHeaders) diff --git a/cmd/web/testutils_test.go b/cmd/web/testutils_test.go new file mode 100644 index 0000000..ca542a6 --- /dev/null +++ b/cmd/web/testutils_test.go @@ -0,0 +1,50 @@ +package main + +import ( + "bytes" + "io" + "log/slog" + "net/http" + "net/http/cookiejar" + "net/http/httptest" + "testing" +) + +func newTestApplication(t *testing.T) *application { + return &application{ + logger: slog.New(slog.NewTextHandler(io.Discard, nil)), + } +} + +type testServer struct { + *httptest.Server +} + +func newTestServer(t *testing.T, h http.Handler) *testServer { + ts := httptest.NewTLSServer(h) + jar, err := cookiejar.New(nil) + if err != nil { + t.Fatal(err) + } + ts.Client().Jar = jar + ts.Client().CheckRedirect = func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + } + return &testServer{ts} +} + +func (ts *testServer) get(t *testing.T, urlPath string) (int, http.Header, string) { + rs, err := ts.Client().Get(ts.URL + urlPath) + if err != nil { + t.Fatal(err) + } + + defer rs.Body.Close() + body, err := io.ReadAll(rs.Body) + if err != nil { + t.Fatal(err) + } + body = bytes.TrimSpace(body) + + return rs.StatusCode, rs.Header, string(body) +}