go-backend-api/internal/server/routes_test.go

32 lines
739 B
Go
Raw Normal View History

2024-12-29 14:35:26 -05:00
package server
import (
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestHandler(t *testing.T) {
s := &Server{}
server := httptest.NewServer(http.HandlerFunc(s.HelloWorldHandler))
defer server.Close()
resp, err := http.Get(server.URL)
if err != nil {
t.Fatalf("error making request to server. Err: %v", err)
}
defer resp.Body.Close()
// Assertions
if resp.StatusCode != http.StatusOK {
t.Errorf("expected status OK; got %v", resp.Status)
}
expected := "{\"message\":\"Hello World\"}"
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("error reading response body. Err: %v", err)
}
if expected != string(body) {
t.Errorf("expected response body to be %v; got %v", expected, string(body))
}
}