adds unit tests (#5)
All checks were successful
ci / build (push) Successful in 46s

Reviewed-on: #5
Co-authored-by: franz.germann <franz.germann@telekom.de>
Co-committed-by: franz.germann <franz.germann@telekom.de>
This commit is contained in:
franz.germann 2025-04-03 12:40:41 +00:00 committed by Franz.Germann
parent 3517d64da8
commit 6b779b1721
6 changed files with 156 additions and 3 deletions

9
go.mod
View file

@ -1,8 +1,15 @@
module forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Franz.Germann/ascii-live module ascii-live
go 1.24.1 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
) )

10
go.sum
View file

@ -1,6 +1,16 @@
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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=

View file

@ -7,7 +7,8 @@ import (
"strings" "strings"
"time" "time"
"forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Franz.Germann/ascii-live/frames" "ascii-live/frames"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )

View file

@ -4,7 +4,7 @@ import (
"flag" "flag"
"net/http" "net/http"
"forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Franz.Germann/ascii-live/handlers" "ascii-live/handlers"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/gorilla/mux" "github.com/gorilla/mux"

69
tests/handlers_test.go Normal file
View file

@ -0,0 +1,69 @@
package test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"ascii-live/handlers"
"github.com/gorilla/mux"
)
func TestListHandler(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/ascii-live/list", handlers.ListHandler)
req, err := http.NewRequest("GET", "/ascii-live/list", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusOK {
t.Errorf("handler 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)
}
}
func TestNotFoundHandler(t *testing.T) {
r := mux.NewRouter()
r.NotFoundHandler = http.HandlerFunc(handlers.NotFoundHandler)
req, err := http.NewRequest("GET", "/not-existing", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, req) // Router verarbeitet die Anfrage
if status := recorder.Code; status != http.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusNotFound)
}
}
func TestNotCurledHandler(t *testing.T) {
r := mux.NewRouter()
r.HandleFunc("/ascii-live/donut", handlers.NotCurledHandler)
req, err := http.NewRequest("GET", "/ascii-live/donut", nil)
if err != nil {
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()
r.ServeHTTP(recorder, req)
if status := recorder.Code; status != http.StatusExpectationFailed {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusExpectationFailed)
}
}

View file

@ -0,0 +1,66 @@
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")
}