This commit is contained in:
parent
fba717183b
commit
7241e730d7
3 changed files with 6 additions and 98 deletions
1
go.sum
1
go.sum
|
@ -10,6 +10,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
|
@ -52,12 +52,13 @@ func TestNotFoundHandler(t *testing.T) {
|
||||||
|
|
||||||
func TestNotCurledHandler(t *testing.T) {
|
func TestNotCurledHandler(t *testing.T) {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/ascii-live/donut", handlers.ListHandler)
|
r.HandleFunc("/ascii-live/donut", handlers.NotCurledHandler)
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", "/ascii-live/donut", nil)
|
req, err := http.NewRequest("GET", "/ascii-live/donut", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36")
|
||||||
|
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
r.ServeHTTP(recorder, req)
|
r.ServeHTTP(recorder, req)
|
||||||
|
@ -65,30 +66,4 @@ func TestNotCurledHandler(t *testing.T) {
|
||||||
if status := recorder.Code; status != http.StatusExpectationFailed {
|
if status := recorder.Code; status != http.StatusExpectationFailed {
|
||||||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusExpectationFailed)
|
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusExpectationFailed)
|
||||||
}
|
}
|
||||||
|
|
||||||
var response map[string]string
|
|
||||||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
||||||
t.Errorf("error decoding response: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteJson(t *testing.T) {
|
|
||||||
recorder := httptest.NewRecorder()
|
|
||||||
// req, _ := http.NewRequest("GET", "/", nil)
|
|
||||||
|
|
||||||
//testData := map[string]string{"message": "hello"}
|
|
||||||
// writeJson(recorder, req, testData, http.StatusOK)
|
|
||||||
|
|
||||||
if status := recorder.Code; status != http.StatusOK {
|
|
||||||
t.Errorf("writeJson returned wrong status code: got %v want %v", status, http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
var response map[string]string
|
|
||||||
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
||||||
t.Errorf("error decoding response: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if response["message"] != "hello" {
|
|
||||||
t.Errorf("writeJson returned unexpected body: got %v want %v", response["message"], "hello")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,34 +5,13 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"ascii-live/frames"
|
"ascii-live/handlers"
|
||||||
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) {
|
func TestListHandler2(t *testing.T) {
|
||||||
req, err := http.NewRequest("GET", "/ascii/list", nil)
|
req, err := http.NewRequest("GET", "/ascii/list", nil)
|
||||||
require.NoError(t, err, "Failed to create request")
|
require.NoError(t, err, "Failed to create request")
|
||||||
|
@ -40,14 +19,11 @@ func TestListHandler2(t *testing.T) {
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
handler := http.HandlerFunc(handlers.ListHandler)
|
handler := http.HandlerFunc(handlers.ListHandler)
|
||||||
|
|
||||||
// --- Execute ---
|
|
||||||
handler.ServeHTTP(recorder, req)
|
handler.ServeHTTP(recorder, req)
|
||||||
|
|
||||||
// --- Assert ---
|
|
||||||
assert.Equal(t, http.StatusOK, recorder.Code, "Status code should be OK")
|
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")
|
assert.Equal(t, "application/json", recorder.Header().Get("Content-Type"), "Content-Type should be application/json")
|
||||||
|
|
||||||
// Check response body
|
|
||||||
var responseBody map[string][]string
|
var responseBody map[string][]string
|
||||||
err = json.Unmarshal(recorder.Body.Bytes(), &responseBody)
|
err = json.Unmarshal(recorder.Body.Bytes(), &responseBody)
|
||||||
require.NoError(t, err, "Failed to unmarshal response JSON")
|
require.NoError(t, err, "Failed to unmarshal response JSON")
|
||||||
|
@ -55,80 +31,36 @@ func TestListHandler2(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotFoundHandler2(t *testing.T) {
|
func TestNotFoundHandler2(t *testing.T) {
|
||||||
// --- Setup ---
|
|
||||||
req, err := http.NewRequest("GET", "/some/unknown/path", nil)
|
req, err := http.NewRequest("GET", "/some/unknown/path", nil)
|
||||||
require.NoError(t, err, "Failed to create request")
|
require.NoError(t, err, "Failed to create request")
|
||||||
|
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
handler := http.HandlerFunc(handlers.NotFoundHandler) // Test directly
|
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)
|
expectedBodyBytes, err := json.Marshal(handlers.NotFoundMessage)
|
||||||
require.NoError(t, err, "Failed to marshal expected NotFoundMessage")
|
require.NoError(t, err, "Failed to marshal expected NotFoundMessage")
|
||||||
|
|
||||||
// --- Execute ---
|
|
||||||
handler.ServeHTTP(recorder, req)
|
handler.ServeHTTP(recorder, req)
|
||||||
|
|
||||||
// --- Assert ---
|
|
||||||
assert.Equal(t, http.StatusNotFound, recorder.Code, "Status code should be Not Found")
|
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.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")
|
assert.JSONEq(t, string(expectedBodyBytes), recorder.Body.String(), "Response body does not match expected NotFoundMessage")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotCurledHandler2(t *testing.T) {
|
func TestNotCurledHandler2(t *testing.T) {
|
||||||
// --- Setup ---
|
req, err := http.NewRequest("GET", "/ascii/someframe", nil)
|
||||||
req, err := http.NewRequest("GET", "/ascii/someframe", nil) // Path doesn't matter for this handler itself
|
|
||||||
require.NoError(t, err, "Failed to create request")
|
require.NoError(t, err, "Failed to create request")
|
||||||
// Crucially, DO NOT set User-Agent to 'curl'
|
// crucially, DO NOT set User-Agent to 'curl'
|
||||||
|
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
handler := http.HandlerFunc(handlers.NotCurledHandler) // Test directly
|
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)
|
expectedBodyBytes, err := json.Marshal(handlers.NotCurledMessage)
|
||||||
require.NoError(t, err, "Failed to marshal expected NotCurledMessage")
|
require.NoError(t, err, "Failed to marshal expected NotCurledMessage")
|
||||||
|
|
||||||
// --- Execute ---
|
|
||||||
handler.ServeHTTP(recorder, req)
|
handler.ServeHTTP(recorder, req)
|
||||||
|
|
||||||
// --- Assert ---
|
|
||||||
assert.Equal(t, http.StatusExpectationFailed, recorder.Code, "Status code should be Expectation Failed")
|
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.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")
|
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")
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue