You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ratchet/internal/server/handler_test.go

49 lines
1.2 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 doesnt
// 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 cant 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")
}