lets-go:14.3 end-to-end server testing

This commit is contained in:
tamsin johnson 2024-02-14 13:27:02 -08:00
parent be85d936a9
commit eeca8ca34c
2 changed files with 10 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
@ -11,17 +12,18 @@ import (
)
func TestPing(t *testing.T) {
rr := httptest.NewRecorder()
app := &application{
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
}
r, err := http.NewRequest(http.MethodGet, "/", nil)
ts := httptest.NewTLSServer(app.routes())
defer ts.Close()
rs, err := ts.Client().Get(ts.URL + "/ping")
if err != nil {
t.Fatal(err)
}
ping(rr, r)
rs := rr.Result()
assert.Equal(t, rs.StatusCode, http.StatusOK)
defer rs.Body.Close()

View File

@ -25,6 +25,8 @@ func (app *application) routes() http.Handler {
fileServer := http.FileServer(http.FS(ui.Files))
router.Handler(http.MethodGet, "/static/*filepath", fileServer)
router.HandlerFunc(http.MethodGet, "/ping", ping)
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate)
router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))