diff --git a/snippetbox/cmd/web/handlers.go b/snippetbox/cmd/web/handlers.go index fa7363a..d45bfcd 100644 --- a/snippetbox/cmd/web/handlers.go +++ b/snippetbox/cmd/web/handlers.go @@ -11,6 +11,11 @@ import ( "snippetbox.chaosfem.tw/internal/validator" ) +// ping ... +func ping(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("OK")) +} + // home ... func (app *application) home(w http.ResponseWriter, r *http.Request) { snippets, err := app.snippets.Latest() diff --git a/snippetbox/cmd/web/handlers_test.go b/snippetbox/cmd/web/handlers_test.go new file mode 100644 index 0000000..14b31d9 --- /dev/null +++ b/snippetbox/cmd/web/handlers_test.go @@ -0,0 +1,34 @@ +package main + +import ( + "bytes" + "io" + "net/http" + "net/http/httptest" + "testing" + + "snippetbox.chaosfem.tw/internal/assert" +) + +func TestPing(t *testing.T) { + rr := httptest.NewRecorder() + + r, err := http.NewRequest(http.MethodGet, "/", nil) + if err != nil { + t.Fatal(err) + } + + ping(rr, r) + + rs := rr.Result() + + assert.Equal(t, rs.StatusCode, http.StatusOK) + + defer rs.Body.Close() + body, err := io.ReadAll(rs.Body) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, string(bytes.TrimSpace(body)), "OK") +}