Files
goauto/server/common/middleware/failure_snapshot_logger_test.go
T

46 lines
1.2 KiB
Go

package middleware
import (
"github.com/gin-gonic/gin"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type snapshotBodySpy struct {
reads int
body io.Reader
}
func (s *snapshotBodySpy) Read(p []byte) (int, error) { s.reads++; return s.body.Read(p) }
func (s *snapshotBodySpy) Close() error { return nil }
func TestFailureSnapshotLoggerNeverReadsOrRecordsBody(t *testing.T) {
gin.SetMode(gin.TestMode)
for _, path := range []string{
"/api/agent/v1/purchase-tasks/1/attempts/00000000-0000-4000-8000-000000000001/failure-snapshot",
"/api/admin/v1/purchase-tasks/1/failure-snapshots",
"/api/admin/v1/purchase-tasks/1/attempts/00000000-0000-4000-8000-000000000001/failure-snapshot/download",
} {
t.Run(path, func(t *testing.T) {
spy := &snapshotBodySpy{body: strings.NewReader("SYNTHETIC_PRIVATE_CONTENT")}
r := gin.New()
r.Use(LoggerToFile())
r.POST(path, func(c *gin.Context) {
if spy.reads != 0 {
t.Error("logger consumed private body before handler")
}
c.Status(http.StatusNoContent)
})
req := httptest.NewRequest(http.MethodPost, path, nil)
req.Body = spy
r.ServeHTTP(httptest.NewRecorder(), req)
if spy.reads != 0 {
t.Error("logger consumed private body")
}
})
}
}