fixes some module and package problems
All checks were successful
ci / build (push) Successful in 44s
All checks were successful
ci / build (push) Successful in 44s
This commit is contained in:
parent
3517d64da8
commit
207a7894f7
4 changed files with 95 additions and 3 deletions
2
go.mod
2
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
|
||||
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
|
2
main.go
2
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"
|
||||
|
|
91
tests/handlers_test.go
Normal file
91
tests/handlers_test.go
Normal file
|
@ -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")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue