lets-go:14.2 ping handler/test
This commit is contained in:
parent
6da4184eb0
commit
6744d12001
@ -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()
|
||||
|
34
snippetbox/cmd/web/handlers_test.go
Normal file
34
snippetbox/cmd/web/handlers_test.go
Normal 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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user