mirror of
https://github.com/twhite96/go-backend-api.git
synced 2025-01-31 06:59:15 +00:00
32 lines
739 B
Go
32 lines
739 B
Go
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))
|
|
}
|
|
}
|