Something went wrong. Try again.
ETag middleware for Go pkg.go.dev/tangled.org/akimo.dev/etag
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223package etag
import ( "net/http" "net/http/httptest" "strings" "testing")
func TestMaxBufferSize(t *testing.T) { t.Parallel()
body := strings.Repeat("a", 64)
tests := []struct { name string opts []Option chunks []string wantETag bool wantBody string wantStatus int }{ { name: "body below the default limit is buffered and gets an ETag", chunks: []string{body}, wantETag: true, wantBody: body, wantStatus: http.StatusOK, }, { name: "body above the custom limit passes through without an ETag", opts: []Option{WithMaxBufferSize(16)}, chunks: []string{body}, wantETag: false, wantBody: body, wantStatus: http.StatusOK, }, { name: "body equal to the limit is buffered and gets an ETag", opts: []Option{WithMaxBufferSize(len(body))}, chunks: []string{body}, wantETag: true, wantBody: body, wantStatus: http.StatusOK, }, { name: "split writes below the limit are buffered and get an ETag", opts: []Option{WithMaxBufferSize(10)}, chunks: []string{"0123", "4567"}, wantETag: true, wantBody: "01234567", wantStatus: http.StatusOK, }, { name: "split write exceeding the limit passes through without an ETag", opts: []Option{WithMaxBufferSize(10)}, chunks: []string{"0123", "4567890"}, wantETag: false, wantBody: "01234567890", wantStatus: http.StatusOK, }, }
for _, tt := range tests { tt := tt
t.Run(tt.name, func(t *testing.T) { t.Parallel()
handler := NewMiddleware( tt.opts...)( http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { for _, chunk := range tt.chunks { w.Write([]byte(chunk)) } }), )
rec := httptest.NewRecorder() handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
if rec.Code != tt.wantStatus { t.Errorf("status code = %d, want %d", rec.Code, tt.wantStatus) }
if got := rec.Body.String(); got != tt.wantBody { t.Errorf("body = %q, want %q", got, tt.wantBody) }
hasETag := rec.Header().Get("ETag") != "" if hasETag != tt.wantETag { t.Errorf("has ETag = %v, want %v", hasETag, tt.wantETag) } }) }}
func TestMaxBufferSizePreservesHandlerETag(t *testing.T) { t.Parallel()
handler := NewMiddleware( WithMaxBufferSize(1), )( http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("ETag", `"custom"`) w.Write([]byte(strings.Repeat("a", 64))) }), )
rec := httptest.NewRecorder() handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
if got := rec.Header().Get("ETag"); got != `"custom"` { t.Errorf("ETag = %q, want %q", got, `"custom"`) }}
func TestEmptyHandlerLeavesResponseUncommitted(t *testing.T) { t.Parallel()
handler := NewMiddleware()(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
rec := httptest.NewRecorder() handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
if rec.Code != http.StatusOK { t.Errorf("status code = %d, want %d", rec.Code, http.StatusOK) }
if got := rec.Body.String(); got != "" { t.Errorf("body = %q, want %q", got, "") }
if got := rec.Header().Get("ETag"); got != "" { t.Errorf("ETag = %q, want empty", got) }}
func TestExplicitEmptyWriteHeaderGetsETag(t *testing.T) { t.Parallel()
handler := NewMiddleware()(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) }))
rec := httptest.NewRecorder() handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
if rec.Code != http.StatusOK { t.Errorf("status code = %d, want %d", rec.Code, http.StatusOK) }
if got := rec.Header().Get("ETag"); got == "" { t.Error("ETag is empty, want non-empty") }}
func TestLateWriteAfterEmptyHandlerIsForwarded(t *testing.T) { t.Parallel()
// Simulates Echo via echo.WrapMiddleware: the handler writes nothing and // returns an error, then Echo's HTTPErrorHandler writes the error response // through the captured ResponseWriter after the middleware has returned. var lateWriter http.ResponseWriter
handler := NewMiddleware()(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { lateWriter = w }))
rec := httptest.NewRecorder() handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
if lateWriter == nil { t.Fatal("handler did not capture ResponseWriter") }
lateWriter.WriteHeader(http.StatusNotFound) _, _ = lateWriter.Write([]byte("not found"))
if rec.Code != http.StatusNotFound { t.Errorf("status code = %d, want %d", rec.Code, http.StatusNotFound) }
if got := rec.Body.String(); got != "not found" { t.Errorf("body = %q, want %q", got, "not found") }
if got := rec.Header().Get("ETag"); got != "" { t.Errorf("ETag = %q, want empty", got) }}
func TestLateImplicitWriteAfterEmptyHandler(t *testing.T) { t.Parallel()
var lateWriter http.ResponseWriter
handler := NewMiddleware()(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { lateWriter = w }))
rec := httptest.NewRecorder() handler.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
if lateWriter == nil { t.Fatal("handler did not capture ResponseWriter") }
_, _ = lateWriter.Write([]byte("late"))
if rec.Code != http.StatusOK { t.Errorf("status code = %d, want %d", rec.Code, http.StatusOK) }
if got := rec.Body.String(); got != "late" { t.Errorf("body = %q, want %q", got, "late") }
if got := rec.Header().Get("ETag"); got != "" { t.Errorf("ETag = %q, want empty", got) }}