package web import ( "io" "log/slog" "net/http" "net/http/httptest" "testing" ) // quietLogger returns a logger that throws its lines away. func quietLogger() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) } // visit sends GET / to handler, and returns the body of the reply. func visit(handler http.Handler) string { request := httptest.NewRequest("GET", "/", nil) recorder := httptest.NewRecorder() handler.ServeHTTP(recorder, request) return recorder.Body.String() } func TestFirstVisit(t *testing.T) { handler := New(quietLogger(), "Hello!").Handler() got := visit(handler) want := "Hello! You are visitor number 1.\n" if got != want { t.Errorf("got %q, want %q", got, want) } } func TestSecondVisit(t *testing.T) { handler := New(quietLogger(), "Hi!").Handler() visit(handler) got := visit(handler) want := "Hi! You are visitor number 2.\n" if got != want { t.Errorf("got %q, want %q", got, want) } } func TestTwoApps(t *testing.T) { first := New(quietLogger(), "A").Handler() second := New(quietLogger(), "B").Handler() visit(first) visit(first) got := visit(second) want := "B You are visitor number 1.\n" if got != want { t.Errorf("got %q, want %q", got, want) } }