42 lines
979 B
Go
42 lines
979 B
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestHomeHandler_ServeHTTP(t *testing.T) {
|
|
// Create a new home handler
|
|
handler := NewHomeHandler()
|
|
|
|
// Create a test request
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
// Create a test response recorder
|
|
rr := httptest.NewRecorder()
|
|
|
|
// Call the handler
|
|
handler.ServeHTTP(rr, req)
|
|
|
|
// Check status code
|
|
if status := rr.Code; status != http.StatusOK {
|
|
t.Errorf("handler returned wrong status code: got %v want %v",
|
|
status, http.StatusOK)
|
|
}
|
|
|
|
// Check content type
|
|
if ct := rr.Header().Get("Content-Type"); ct != "application/json" {
|
|
t.Errorf("handler returned wrong content type: got %v want %v",
|
|
ct, "application/json")
|
|
}
|
|
|
|
// Check response body contains expected data
|
|
expectedBody := `"success":true`
|
|
if body := rr.Body.String(); !strings.Contains(body, expectedBody) {
|
|
t.Errorf("handler returned unexpected body: got %v want %v",
|
|
body, expectedBody)
|
|
}
|
|
}
|