go-web-template/handlers/not_found_test.go

38 lines
794 B
Go
Raw Normal View History

2024-08-25 16:40:48 +02:00
package handlers_test
import (
"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 res.Body.Close()
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")
}
}