This commit is contained in:
parent
207a7894f7
commit
fba717183b
4 changed files with 157 additions and 4 deletions
7
go.mod
7
go.mod
|
@ -5,4 +5,11 @@ go 1.24.1
|
||||||
require (
|
require (
|
||||||
github.com/golang/glog v1.2.4
|
github.com/golang/glog v1.2.4
|
||||||
github.com/gorilla/mux v1.8.1
|
github.com/gorilla/mux v1.8.1
|
||||||
|
github.com/stretchr/testify v1.10.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
9
go.sum
9
go.sum
|
@ -1,6 +1,15 @@
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/golang/glog v1.2.4 h1:CNNw5U8lSiiBk7druxtSHHTsRWcxKoac6kZKm2peBBc=
|
github.com/golang/glog v1.2.4 h1:CNNw5U8lSiiBk7druxtSHHTsRWcxKoac6kZKm2peBBc=
|
||||||
github.com/golang/glog v1.2.4/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
|
github.com/golang/glog v1.2.4/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
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/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
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/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|
|
@ -35,6 +35,7 @@ func TestListHandler(t *testing.T) {
|
||||||
|
|
||||||
func TestNotFoundHandler(t *testing.T) {
|
func TestNotFoundHandler(t *testing.T) {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
|
r.NotFoundHandler = http.HandlerFunc(handlers.NotFoundHandler)
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", "/not-existing", nil)
|
req, err := http.NewRequest("GET", "/not-existing", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -42,7 +43,7 @@ func TestNotFoundHandler(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
r.ServeHTTP(recorder, req)
|
r.ServeHTTP(recorder, req) // Router verarbeitet die Anfrage
|
||||||
|
|
||||||
if status := recorder.Code; status != http.StatusNotFound {
|
if status := recorder.Code; status != http.StatusNotFound {
|
||||||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusNotFound)
|
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusNotFound)
|
||||||
|
@ -50,14 +51,16 @@ func TestNotFoundHandler(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNotCurledHandler(t *testing.T) {
|
func TestNotCurledHandler(t *testing.T) {
|
||||||
req, err := http.NewRequest("GET", "/ascii-live/test", nil)
|
r := mux.NewRouter()
|
||||||
|
r.HandleFunc("/ascii-live/donut", handlers.ListHandler)
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", "/ascii-live/donut", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
handler := http.HandlerFunc(handlers.NotCurledHandler)
|
r.ServeHTTP(recorder, req)
|
||||||
handler.ServeHTTP(recorder, req)
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
134
tests/handlers_testify_test.go
Normal file
134
tests/handlers_testify_test.go
Normal file
|
@ -0,0 +1,134 @@
|
||||||
|
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")
|
||||||
|
}
|
Loading…
Reference in a new issue