69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
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)
|
|
}
|
|
}
|