lets-go:14.2 ping handler/test

This commit is contained in:
tamsin johnson 2024-02-13 12:54:07 -08:00
parent 6da4184eb0
commit 6744d12001
2 changed files with 39 additions and 0 deletions

View File

@ -11,6 +11,11 @@ import (
"snippetbox.chaosfem.tw/internal/validator" "snippetbox.chaosfem.tw/internal/validator"
) )
// ping ...
func ping(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}
// home ... // home ...
func (app *application) home(w http.ResponseWriter, r *http.Request) { func (app *application) home(w http.ResponseWriter, r *http.Request) {
snippets, err := app.snippets.Latest() snippets, err := app.snippets.Latest()

View File

@ -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")
}