Something went wrong. Try again.
ETag middleware for Go pkg.go.dev/tangled.org/akimo.dev/etag
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132package etag
import "testing"
func TestWeakMatch(t *testing.T) { t.Parallel()
tests := []struct { name string header string etag string want bool }{ {name: "exact strong match", header: `"foo"`, etag: `"foo"`, want: true}, {name: "weak header matches strong etag", header: `W/"foo"`, etag: `"foo"`, want: true}, {name: "strong header matches weak etag", header: `"foo"`, etag: `W/"foo"`, want: true}, {name: "weak header matches weak etag", header: `W/"foo"`, etag: `W/"foo"`, want: true}, {name: "no match", header: `"foo"`, etag: `"bar"`, want: false}, { name: "comma inside quotes matches first", header: `"a,b", "c"`, etag: `"a,b"`, want: true, }, {name: "comma inside quotes matches second", header: `"a,b", "c"`, etag: `"c"`, want: true}, { name: "comma inside quotes does not split", header: `"a,b", "c"`, etag: `"a"`, want: false, }, { name: "comma inside quotes does not split second", header: `"a,b", "c"`, etag: `"b"`, want: false, }, {name: "weak comma inside quotes", header: `W/"a,b", "c"`, etag: `"a,b"`, want: true}, { name: "multiple tags with OWS", header: `"foo" , W/"bar" , "baz"`, etag: `"bar"`, want: true, }, {name: "star matches", header: `*`, etag: `"foo"`, want: true}, {name: "star with OWS matches", header: ` * `, etag: `"foo"`, want: true}, {name: "star matches weak etag", header: `*`, etag: `W/"foo"`, want: true}, {name: "empty header", header: ``, etag: `"foo"`, want: false}, {name: "blank header", header: ` `, etag: `"foo"`, want: false}, {name: "unterminated header", header: `"foo`, etag: `"foo"`, want: false}, {name: "unterminated with comma", header: `"foo,bar`, etag: `"foo,bar"`, want: false}, {name: "trailing garbage is invalid", header: `"foo"bar`, etag: `"foo"`, want: false}, {name: "lowercase weak prefix is invalid", header: `w/"foo"`, etag: `"foo"`, want: false}, {name: "space in opaque-tag is invalid", header: `"a b"`, etag: `"a b"`, want: false}, {name: "unquoted etag arg", header: `"foo"`, etag: `foo`, want: false}, {name: "empty opaque-tag", header: `""`, etag: `""`, want: true}, {name: "empty list elements are ignored", header: `,,"foo",,`, etag: `"foo"`, want: true}, {name: "trailing comma", header: `W/"foo",`, etag: `W/"foo"`, want: true}, }
for _, tt := range tests { tt := tt
t.Run(tt.name, func(t *testing.T) { t.Parallel()
if got := WeakMatch(tt.header, tt.etag); got != tt.want { t.Errorf("WeakMatch(%q, %q) = %v, want %v", tt.header, tt.etag, got, tt.want) } }) }}
func TestStrongMatch(t *testing.T) { t.Parallel()
tests := []struct { name string header string etag string want bool }{ {name: "exact strong match", header: `"foo"`, etag: `"foo"`, want: true}, {name: "weak header never matches", header: `W/"foo"`, etag: `"foo"`, want: false}, {name: "weak etag never matches", header: `"foo"`, etag: `W/"foo"`, want: false}, {name: "weak both never matches", header: `W/"foo"`, etag: `W/"foo"`, want: false}, { name: "comma inside quotes matches first", header: `"a,b", "c"`, etag: `"a,b"`, want: true, }, {name: "comma inside quotes matches second", header: `"a,b", "c"`, etag: `"c"`, want: true}, { name: "comma inside quotes does not split", header: `"a,b", "c"`, etag: `"a"`, want: false, }, {name: "weak entry skipped", header: `"foo" , W/"bar" , "baz"`, etag: `"bar"`, want: false}, { name: "strong entry matches", header: `"foo" , W/"bar" , "baz"`, etag: `"baz"`, want: true, }, {name: "star matches", header: `*`, etag: `"foo"`, want: true}, {name: "star with OWS matches", header: ` * `, etag: `"foo"`, want: true}, {name: "star matches weak etag", header: `*`, etag: `W/"foo"`, want: true}, {name: "empty header", header: ``, etag: `"foo"`, want: false}, {name: "unterminated header", header: `"foo`, etag: `"foo"`, want: false}, {name: "trailing garbage is invalid", header: `"foo"bar`, etag: `"foo"`, want: false}, {name: "lowercase weak prefix is invalid", header: `w/"foo"`, etag: `"foo"`, want: false}, {name: "space in opaque-tag is invalid", header: `"a b"`, etag: `"a b"`, want: false}, {name: "unquoted etag arg", header: `"foo"`, etag: `foo`, want: false}, {name: "empty opaque-tag", header: `""`, etag: `""`, want: true}, {name: "empty list elements are ignored", header: `,,"foo",,`, etag: `"foo"`, want: true}, }
for _, tt := range tests { tt := tt
t.Run(tt.name, func(t *testing.T) { t.Parallel()
if got := StrongMatch(tt.header, tt.etag); got != tt.want { t.Errorf("StrongMatch(%q, %q) = %v, want %v", tt.header, tt.etag, got, tt.want) } }) }}