2025-03-28 09:13:30 +00:00
|
|
|
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()
|
2025-03-31 15:30:09 +00:00
|
|
|
r.NotFoundHandler = http.HandlerFunc(handlers.NotFoundHandler)
|
2025-03-28 09:13:30 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/not-existing", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
2025-03-31 15:30:09 +00:00
|
|
|
r.ServeHTTP(recorder, req) // Router verarbeitet die Anfrage
|
2025-03-28 09:13:30 +00:00
|
|
|
|
|
|
|
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) {
|
2025-03-31 15:30:09 +00:00
|
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/ascii-live/donut", handlers.ListHandler)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", "/ascii-live/donut", nil)
|
2025-03-28 09:13:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
2025-03-31 15:30:09 +00:00
|
|
|
r.ServeHTTP(recorder, req)
|
2025-03-28 09:13:30 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|