package server import ( "bytes" "io" "net/http" "net/http/httptest" "testing" "git.runcible.io/learning/ratchet/internal/assert" ) func TestPing(t *testing.T) { // This is essentially an implementation of http.ResponseWriter // which records the response status code, headers and body instead // of actually writing them to a HTTP connection. rr := httptest.NewRecorder() // Initialize a dummy request r, err := http.NewRequest(http.MethodGet, "/", nil) if err != nil { // When called, t.Fatal() will mark the test as failed, log the error, // and then completely stop execution of the current test // (or sub-test). // Typically you should call t.Fatal() in situations where it doesn’t // make sense to continue the current test — such as an error during // a setup step, or where an unexpected error from a Go standard // library function means you can’t proceed with the test. t.Fatal(err) } ping := PingHandler() ping.ServeHTTP(rr, r) resp := rr.Result() assert.Equal(t, resp.StatusCode, http.StatusOK) defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { t.Fatal(err) } body = bytes.TrimSpace(body) assert.Equal(t, string(body), "OK") }