learn-go/snippetbox/cmd/web/handlers_test.go
2024-02-13 12:54:07 -08:00

35 lines
509 B
Go

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