ascii-live/tests/handlers_testify_test.go

67 lines
2.4 KiB
Go
Raw Normal View History

2025-03-31 15:30:09 +00:00
package test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2025-04-03 12:38:27 +00:00
"ascii-live/handlers"
2025-03-31 15:30:09 +00:00
)
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) {
2025-04-03 12:38:27 +00:00
req, err := http.NewRequest("GET", "/ascii/someframe", nil)
2025-03-31 15:30:09 +00:00
require.NoError(t, err, "Failed to create request")
2025-04-03 12:38:27 +00:00
// crucially, DO NOT set User-Agent to 'curl'
2025-03-31 15:30:09 +00:00
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")
}