134 lines
4.9 KiB
Go
134 lines
4.9 KiB
Go
package test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"ascii-live/frames"
|
|
handlers "ascii-live/handlers"
|
|
)
|
|
|
|
// --- Helper function to setup router and server for testing Handler ---
|
|
func setupRouterWithHandler(handlerFunc http.HandlerFunc, path string, method string) *mux.Router {
|
|
r := mux.NewRouter()
|
|
r.HandleFunc(path, handlerFunc).Methods(method)
|
|
return r
|
|
}
|
|
|
|
// --- Helper to create a minimal mock FrameType ---
|
|
func minimalMockFrameType() frames.FrameType {
|
|
return frames.FrameType{
|
|
GetFrame: func(i int) string { return "mock_frame" },
|
|
GetLength: func() int { return 1 },
|
|
GetSleep: func() time.Duration { return 10 * time.Millisecond },
|
|
}
|
|
}
|
|
|
|
// --- Test Cases ---
|
|
|
|
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)
|
|
|
|
// --- Execute ---
|
|
handler.ServeHTTP(recorder, req)
|
|
|
|
// --- Assert ---
|
|
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")
|
|
|
|
// Check response body
|
|
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) {
|
|
// --- Setup ---
|
|
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
|
|
|
|
// Expected response body (marshal the global NotFoundMessage)
|
|
// Ensure NotFoundMessage is exported from handlers package if needed, or redefine here.
|
|
// Assuming handlers.NotFoundMessage is accessible (it's a var, should be fine).
|
|
expectedBodyBytes, err := json.Marshal(handlers.NotFoundMessage)
|
|
require.NoError(t, err, "Failed to marshal expected NotFoundMessage")
|
|
|
|
// --- Execute ---
|
|
handler.ServeHTTP(recorder, req)
|
|
|
|
// --- Assert ---
|
|
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) {
|
|
// --- Setup ---
|
|
req, err := http.NewRequest("GET", "/ascii/someframe", nil) // Path doesn't matter for this handler itself
|
|
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
|
|
|
|
// Expected response body (marshal the global NotCurledMessage)
|
|
// Assuming handlers.NotCurledMessage is accessible.
|
|
expectedBodyBytes, err := json.Marshal(handlers.NotCurledMessage)
|
|
require.NoError(t, err, "Failed to marshal expected NotCurledMessage")
|
|
|
|
// --- Execute ---
|
|
handler.ServeHTTP(recorder, req)
|
|
|
|
// --- Assert ---
|
|
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")
|
|
}
|
|
|
|
// --- Tests for the main Handler ---
|
|
|
|
func TestHandler_NotCurl(t *testing.T) {
|
|
// --- Setup ---
|
|
// Ensure the frame we request DOES exist
|
|
originalFrameMap := frames.FrameMap
|
|
frames.FrameMap = map[string]frames.FrameType{
|
|
"testframe": minimalMockFrameType(),
|
|
}
|
|
t.Cleanup(func() { frames.FrameMap = originalFrameMap }) // Restore
|
|
|
|
router := setupRouterWithHandler(handlers.Handler, "/ascii/{frameSource}", "GET")
|
|
req, err := http.NewRequest("GET", "/ascii/testframe", nil)
|
|
require.NoError(t, err, "Failed to create request")
|
|
// Set User-Agent to something OTHER than 'curl'
|
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)")
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
// Expected response body (NotCurledMessage)
|
|
expectedBodyBytes, err := json.Marshal(handlers.NotCurledMessage)
|
|
require.NoError(t, err, "Failed to marshal expected NotCurledMessage")
|
|
|
|
// --- Execute ---
|
|
router.ServeHTTP(rr, req)
|
|
|
|
// --- Assert ---
|
|
// Should delegate to NotCurledHandler
|
|
assert.Equal(t, http.StatusExpectationFailed, rr.Code, "Status code should be Expectation Failed")
|
|
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"), "Content-Type should be application/json")
|
|
assert.JSONEq(t, string(expectedBodyBytes), rr.Body.String(), "Response body should match NotCurledMessage")
|
|
}
|