go-web-template/handlers/not_found_test.go
Valentin Brandl 5a03c74642
All checks were successful
Build / build (pull_request) Successful in 1m18s
Lint / lint (pull_request) Successful in 2m9s
Fix lints
2025-04-09 14:47:05 +02:00

43 lines
885 B
Go

package handlers_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestNotFound(t *testing.T) {
app := PrepareApplication()
req := httptest.NewRequest(http.MethodGet, "/some/invalid/path", nil)
w := httptest.NewRecorder()
app.Handler().ServeHTTP(w, req)
res := w.Result()
defer func() {
if err := res.Body.Close(); err != nil {
fmt.Println("Received error: ", err)
}
}()
if res.StatusCode != 404 {
t.Errorf("expected status to be 404, got %d", res.StatusCode)
}
reqId := res.Header.Get("x-request-id")
if reqId == "" {
t.Errorf("expected `x-request-id` header to be present, got %s", reqId)
}
data, err := io.ReadAll(res.Body)
if err != nil {
t.Errorf("expected error to be nil, got %v", err)
}
if !strings.Contains(string(data), reqId) {
t.Errorf("expected body to contain request ID but it does not")
}
}