From 207a7894f726b7184bf853476cb8a0b1680371d5 Mon Sep 17 00:00:00 2001 From: "franz.germann" Date: Fri, 28 Mar 2025 10:13:30 +0100 Subject: [PATCH] fixes some module and package problems --- go.mod | 2 +- handlers/handlers.go | 3 +- main.go | 2 +- tests/handlers_test.go | 91 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 tests/handlers_test.go diff --git a/go.mod b/go.mod index bc5ce83..56d71ba 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Franz.Germann/ascii-live +module ascii-live go 1.24.1 diff --git a/handlers/handlers.go b/handlers/handlers.go index ea0364f..2368fa8 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -7,7 +7,8 @@ import ( "strings" "time" - "forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Franz.Germann/ascii-live/frames" + "ascii-live/frames" + "github.com/golang/glog" "github.com/gorilla/mux" ) diff --git a/main.go b/main.go index 4d75192..90c71fd 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ import ( "flag" "net/http" - "forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Franz.Germann/ascii-live/handlers" + "ascii-live/handlers" "github.com/golang/glog" "github.com/gorilla/mux" diff --git a/tests/handlers_test.go b/tests/handlers_test.go new file mode 100644 index 0000000..a26cf09 --- /dev/null +++ b/tests/handlers_test.go @@ -0,0 +1,91 @@ +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() + + req, err := http.NewRequest("GET", "/not-existing", nil) + if err != nil { + t.Fatal(err) + } + + recorder := httptest.NewRecorder() + r.ServeHTTP(recorder, req) + + 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) { + req, err := http.NewRequest("GET", "/ascii-live/test", nil) + if err != nil { + t.Fatal(err) + } + + recorder := httptest.NewRecorder() + handler := http.HandlerFunc(handlers.NotCurledHandler) + handler.ServeHTTP(recorder, req) + + if status := recorder.Code; 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") + } +}