go-finger/internal/middleware/wrapper_test.go
Claude f5d1577ec2
chore: update GitHub Actions to latest versions, fix cache paths
- Remove Gitea-specific "Fix docker sock permissions" step
- actions/checkout: v2 → v4
- docker/setup-qemu-action: v1 → v3
- docker/setup-buildx-action: v1 → v3
- docker/login-action: v2 → v3
- docker/metadata-action: v4 → v5
- actions/cache: v3 → v4; remove Gitea-runner-specific Go module cache
  paths (/go/pkg/mod/, /tmp/.go-build-cache) — only buildx cache is
  relevant for Docker-only builds on GitHub Actions
- docker/build-push-action: v2 → v5

https://claude.ai/code/session_01WSTmBCLVtmPMFqxCnjM9fh
2026-03-08 03:08:16 +00:00

98 lines
1.8 KiB
Go

package middleware_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/Maronato/go-finger/internal/middleware"
)
func TestWrapResponseWriter(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
wrapped := middleware.WrapResponseWriter(w)
if wrapped == nil {
t.Error("wrapper is nil")
}
}
func TestResponseWrapper_Status(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
wrapped := middleware.WrapResponseWriter(w)
if wrapped.Status() != 0 {
t.Error("status is not 0")
}
wrapped.WriteHeader(http.StatusOK)
if wrapped.Status() != http.StatusOK {
t.Error("status is not 200")
}
}
type FailWriter struct{}
func (w *FailWriter) Write(_ []byte) (int, error) {
return 0, fmt.Errorf("error") //nolint:goerr113 // We want to return an error
}
func (w *FailWriter) Header() http.Header {
return http.Header{}
}
func (w *FailWriter) WriteHeader(_ int) {}
func TestResponseWrapper_Write(t *testing.T) {
t.Parallel()
t.Run("writes success messages", func(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
wrapped := middleware.WrapResponseWriter(w)
size, err := wrapped.Write([]byte("test"))
if err != nil {
t.Errorf("error writing response: %v", err)
}
if size != 4 {
t.Error("size is not 4")
}
if wrapped.Status() != http.StatusOK {
t.Error("status is not 200")
}
})
t.Run("returns error on fail write", func(t *testing.T) {
t.Parallel()
w := &FailWriter{}
wrapped := middleware.WrapResponseWriter(w)
_, err := wrapped.Write([]byte("test"))
if err == nil {
t.Error("error is nil")
}
})
}
func TestResponseWrapper_Unwrap(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
wrapped := middleware.WrapResponseWriter(w)
if wrapped.Unwrap() != w {
t.Error("unwrapped response is not the same")
}
}