package test import ( "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "ascii-live/handlers" ) func TestListHandler2(t *testing.T) { req, err := http.NewRequest("GET", "/ascii/list", nil) require.NoError(t, err, "Failed to create request") recorder := httptest.NewRecorder() handler := http.HandlerFunc(handlers.ListHandler) handler.ServeHTTP(recorder, req) assert.Equal(t, http.StatusOK, recorder.Code, "Status code should be OK") assert.Equal(t, "application/json", recorder.Header().Get("Content-Type"), "Content-Type should be application/json") var responseBody map[string][]string err = json.Unmarshal(recorder.Body.Bytes(), &responseBody) require.NoError(t, err, "Failed to unmarshal response JSON") } func TestNotFoundHandler2(t *testing.T) { req, err := http.NewRequest("GET", "/some/unknown/path", nil) require.NoError(t, err, "Failed to create request") recorder := httptest.NewRecorder() handler := http.HandlerFunc(handlers.NotFoundHandler) // Test directly expectedBodyBytes, err := json.Marshal(handlers.NotFoundMessage) require.NoError(t, err, "Failed to marshal expected NotFoundMessage") handler.ServeHTTP(recorder, req) assert.Equal(t, http.StatusNotFound, recorder.Code, "Status code should be Not Found") assert.Equal(t, "application/json", recorder.Header().Get("Content-Type"), "Content-Type should be application/json") assert.JSONEq(t, string(expectedBodyBytes), recorder.Body.String(), "Response body does not match expected NotFoundMessage") } func TestNotCurledHandler2(t *testing.T) { req, err := http.NewRequest("GET", "/ascii/someframe", nil) require.NoError(t, err, "Failed to create request") // crucially, DO NOT set User-Agent to 'curl' recorder := httptest.NewRecorder() handler := http.HandlerFunc(handlers.NotCurledHandler) // Test directly expectedBodyBytes, err := json.Marshal(handlers.NotCurledMessage) require.NoError(t, err, "Failed to marshal expected NotCurledMessage") handler.ServeHTTP(recorder, req) assert.Equal(t, http.StatusExpectationFailed, recorder.Code, "Status code should be Expectation Failed") assert.Equal(t, "application/json", recorder.Header().Get("Content-Type"), "Content-Type should be application/json") assert.JSONEq(t, string(expectedBodyBytes), recorder.Body.String(), "Response body does not match expected NotCurledMessage") }