package httpapi import ( "encoding/json" "net/http" "net/http/httptest" "testing" ) func TestEndpoints(t *testing.T) { for _, tc := range []struct{ path, key, value string }{ {"/health", "status", "ok"}, {"/api/test", "message", "API connection successful"}, } { t.Run(tc.path, func(t *testing.T) { response := httptest.NewRecorder() NewRouter().ServeHTTP(response, httptest.NewRequest(http.MethodGet, tc.path, nil)) if response.Code != http.StatusOK { t.Fatalf("status = %d", response.Code) } if got := response.Header().Get("Content-Type"); got != "application/json" { t.Fatalf("content type = %q", got) } var body map[string]string if err := json.Unmarshal(response.Body.Bytes(), &body); err != nil { t.Fatal(err) } if body[tc.key] != tc.value { t.Fatalf("body = %v", body) } }) } } func TestRoutingErrors(t *testing.T) { for _, tc := range []struct { method, path string status int }{ {http.MethodPost, "/api/test", http.StatusMethodNotAllowed}, {http.MethodGet, "/missing", http.StatusNotFound}, } { response := httptest.NewRecorder() NewRouter().ServeHTTP(response, httptest.NewRequest(tc.method, tc.path, nil)) if response.Code != tc.status { t.Fatalf("%s %s: status = %d", tc.method, tc.path, response.Code) } } }