From 119479da62a6c9d1c1303dc369641b42fe034ac6 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 17:23:28 +0000 Subject: [PATCH 1/4] fix: replay ignores a recording's Accept header Accept, like User-Agent, says what tool made a recording, not what a call asks for: go-api-libs/ibkr's newest recording was made by a tool sending Accept: application/json, which its generated client does not send, so the replay failed. Replay now compares headers but those two. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y --- apitest/apitest_test.go | 4 ++-- apitest/replay.go | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/apitest/apitest_test.go b/apitest/apitest_test.go index 682d7d4..fa71c4a 100644 --- a/apitest/apitest_test.go +++ b/apitest/apitest_test.go @@ -386,9 +386,9 @@ func call(t testing.TB, rt http.RoundTripper, ctx context.Context, method, rawUR func TestReplay(t *testing.T) { json1 := http.Header{"Content-Type": {"application/json"}} - t.Run("answers as recorded, a body with a tag the recording leaves out too", func(t *testing.T) { + t.Run("answers as recorded, but for the recording tool's headers, a body with a tag the recording leaves out too", func(t *testing.T) { path := interactionsFile(t, cassette.Interactions{ - recorded(t, http.MethodGet, "https://api.example.com/a", "", `{"id":"a"}`, http.Header{}), + recorded(t, http.MethodGet, "https://api.example.com/a", "", `{"id":"a"}`, http.Header{"Accept": {"application/json"}}), recorded(t, http.MethodPost, "https://api.example.com/q", `{"filter":{"date":{"equals":"today"}}}`, `{}`, json1), }) diff --git a/apitest/replay.go b/apitest/replay.go index f6b8e99..536e43f 100644 --- a/apitest/replay.go +++ b/apitest/replay.go @@ -21,9 +21,10 @@ func OptionalAuth(ctx context.Context) context.Context { } // Replay is a transport that answers calls with the interactions recorded in the file at path. Every call must be the -// next recording, with its headers as recorded but the User-Agent, which names whatever tool made the recording; a -// call marked with [OptionalAuth] may have credentials its recording lacks. A request body may hold a tag its -// recording leaves out, which a client fills in (see jsonSameButTags). The test fails if a recording was never called. +// next recording, with its headers as recorded but the User-Agent and Accept, which say what tool made the recording, +// not what the call asks for; a call marked with [OptionalAuth] may have credentials its recording lacks. A request +// body may hold a tag its recording leaves out, which a client fills in (see jsonSameButTags). The test fails if a +// recording was never called. func Replay(t testing.TB, path string) http.RoundTripper { t.Helper() @@ -33,7 +34,7 @@ func Replay(t testing.TB, path string) http.RoundTripper { } match := cassette.MatchRequest( - cassette.CompareHeaders("User-Agent"), + cassette.CompareHeaders("User-Agent", "Accept"), cassette.OptionalAuthorization(func(req *http.Request) bool { return req.Context().Value(optionalAuthKey{}) != nil }), ) From d1f0ebfae1f8f02650b059b3ac4bf2b45f22d5f6 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 18:06:51 +0000 Subject: [PATCH 2/4] fix: replay allows an Accept its recording lacks, compares one it has Rather than ignoring Accept, Replay compares it where the recording has it, and allows it where a recording made before clients sent it lacks it, through cassette's OptionalHeaders. A recording made with Accept: application/json, as go-api-libs/ibkr's newest, then matches a client that sends it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y --- apitest/apitest_test.go | 6 +++--- apitest/replay.go | 11 ++++++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/apitest/apitest_test.go b/apitest/apitest_test.go index fa71c4a..5abe35a 100644 --- a/apitest/apitest_test.go +++ b/apitest/apitest_test.go @@ -386,7 +386,7 @@ func call(t testing.TB, rt http.RoundTripper, ctx context.Context, method, rawUR func TestReplay(t *testing.T) { json1 := http.Header{"Content-Type": {"application/json"}} - t.Run("answers as recorded, but for the recording tool's headers, a body with a tag the recording leaves out too", func(t *testing.T) { + t.Run("answers as recorded, an Accept the recording lacks and a body with a tag it leaves out too", func(t *testing.T) { path := interactionsFile(t, cassette.Interactions{ recorded(t, http.MethodGet, "https://api.example.com/a", "", `{"id":"a"}`, http.Header{"Accept": {"application/json"}}), recorded(t, http.MethodPost, "https://api.example.com/q", `{"filter":{"date":{"equals":"today"}}}`, `{}`, json1), @@ -395,7 +395,7 @@ func TestReplay(t *testing.T) { f := run(func(tb testing.TB) { rt := &apitest.LastBody{Next: apitest.Replay(tb, path)} - rsp, err := call(tb, rt, t.Context(), http.MethodGet, "https://api.example.com/a", "", http.Header{"User-Agent": {"some tool"}}) + rsp, err := call(tb, rt, t.Context(), http.MethodGet, "https://api.example.com/a", "", http.Header{"User-Agent": {"some tool"}, "Accept": {"application/json"}}) if err != nil { tb.Fatal(err) } @@ -406,7 +406,7 @@ func TestReplay(t *testing.T) { tb.Errorf("kept %s", rt.Body) } - rsp, err = call(tb, rt, t.Context(), http.MethodPost, "https://api.example.com/q", `{"filter":{"type":"date","date":{"equals":"today"}}}`, json1) + rsp, err = call(tb, rt, t.Context(), http.MethodPost, "https://api.example.com/q", `{"filter":{"type":"date","date":{"equals":"today"}}}`, http.Header{"Content-Type": {"application/json"}, "Accept": {"application/json"}}) if err != nil { tb.Fatal(err) } diff --git a/apitest/replay.go b/apitest/replay.go index 536e43f..70b7481 100644 --- a/apitest/replay.go +++ b/apitest/replay.go @@ -21,10 +21,10 @@ func OptionalAuth(ctx context.Context) context.Context { } // Replay is a transport that answers calls with the interactions recorded in the file at path. Every call must be the -// next recording, with its headers as recorded but the User-Agent and Accept, which say what tool made the recording, -// not what the call asks for; a call marked with [OptionalAuth] may have credentials its recording lacks. A request -// body may hold a tag its recording leaves out, which a client fills in (see jsonSameButTags). The test fails if a -// recording was never called. +// next recording, with its headers as recorded but the User-Agent, which names whatever tool made the recording, and an +// Accept its recording lacks, as one made before clients sent it; a call marked with [OptionalAuth] may have credentials +// its recording lacks. A request body may hold a tag its recording leaves out, which a client fills in (see +// jsonSameButTags). The test fails if a recording was never called. func Replay(t testing.TB, path string) http.RoundTripper { t.Helper() @@ -34,7 +34,8 @@ func Replay(t testing.TB, path string) http.RoundTripper { } match := cassette.MatchRequest( - cassette.CompareHeaders("User-Agent", "Accept"), + cassette.CompareHeaders("User-Agent"), + cassette.OptionalHeaders("Accept"), cassette.OptionalAuthorization(func(req *http.Request) bool { return req.Context().Value(optionalAuthKey{}) != nil }), ) From 5e04ca203b565362fbe35f68a38e23610c642463 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 18:08:04 +0000 Subject: [PATCH 3/4] chore: pin cassette to its OptionalHeaders commit MarkRosemaker/cassette#7 adds the OptionalHeaders Replay now uses; until it merges, cassette is pinned to its commit. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y --- go.mod | 2 +- go.sum | 4 ++-- .../MarkRosemaker/cassette/replay.go | 22 +++++++++++++++++++ vendor/modules.txt | 2 +- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 41507be..3c3c49e 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/go-api-libs/api go 1.27 -require github.com/MarkRosemaker/cassette v0.0.0-20261124170238-3b0b504cc01a +require github.com/MarkRosemaker/cassette v0.0.0-20261125180338-b758ec7cd132 require ( cloud.google.com/go v0.123.0 // indirect diff --git a/go.sum b/go.sum index 038d84c..2ca0cb7 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ cloud.google.com/go v0.123.0 h1:2NAUJwPR47q+E35uaJeYoNhuNEM9kM8SjgRgdeOJUSE= cloud.google.com/go v0.123.0/go.mod h1:xBoMV08QcqUGuPW65Qfm1o9Y4zKZBpGS+7bImXLTAZU= -github.com/MarkRosemaker/cassette v0.0.0-20261124170238-3b0b504cc01a h1:iMV+JecKObVuGDQw0YhgsChj9Q8JZXVAk0tsyw1vCq0= -github.com/MarkRosemaker/cassette v0.0.0-20261124170238-3b0b504cc01a/go.mod h1:mIy1SlNqKDRALYBqZGjj+MWPU9dASkyP1d1X+I1HP6k= +github.com/MarkRosemaker/cassette v0.0.0-20261125180338-b758ec7cd132 h1:Lo3IOIsseUaFH7zM9qjrSgMgLt+23wGByQYZWn+m39Q= +github.com/MarkRosemaker/cassette v0.0.0-20261125180338-b758ec7cd132/go.mod h1:mIy1SlNqKDRALYBqZGjj+MWPU9dASkyP1d1X+I1HP6k= github.com/MarkRosemaker/jsonutil v0.0.0-20261118230255-4b04cfc02484 h1:mWQovC60M6QVWXpitDiB2bwT+Jr8uQH2eyjOcUE2EcI= github.com/MarkRosemaker/jsonutil v0.0.0-20261118230255-4b04cfc02484/go.mod h1:SzjJlgYFlzpbFGvUUoQgfEsWlbai63bvhDPK8J0MNYM= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= diff --git a/vendor/github.com/MarkRosemaker/cassette/replay.go b/vendor/github.com/MarkRosemaker/cassette/replay.go index 141c819..874d02b 100644 --- a/vendor/github.com/MarkRosemaker/cassette/replay.go +++ b/vendor/github.com/MarkRosemaker/cassette/replay.go @@ -209,6 +209,7 @@ type MatchOption func(*matcher) type matcher struct { headers bool ignore map[string]bool + optional map[string]bool optionalAuth func(*http.Request) bool } @@ -246,6 +247,21 @@ func OptionalAuthorization(optional func(*http.Request) bool) MatchOption { return func(m *matcher) { m.optionalAuth = optional } } +// OptionalHeaders makes [MatchRequest] match a recording without the headers named to a request that has them, and +// compare them where the recording has them: a header a client has begun to send, such as Accept, is missing from what +// was recorded before. +func OptionalHeaders(names ...string) MatchOption { + return func(m *matcher) { + if m.optional == nil { + m.optional = map[string]bool{} + } + + for _, name := range names { + m.optional[http.CanonicalHeaderKey(name)] = true + } + } +} + func (m *matcher) match(req *http.Request, rec Request) error { got, err := NewRequest(req) if err != nil { @@ -302,6 +318,12 @@ func (m *matcher) matchHeaders(req *http.Request, got http.Header, rec Request) want.Del(name) } + for name := range m.optional { + if _, ok := want[name]; !ok { + got.Del(name) + } + } + got.Del("Authorization") want.Del("Authorization") diff --git a/vendor/modules.txt b/vendor/modules.txt index 631b56e..05b7cb3 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,7 +1,7 @@ # cloud.google.com/go v0.123.0 ## explicit; go 1.24.0 cloud.google.com/go/civil -# github.com/MarkRosemaker/cassette v0.0.0-20261124170238-3b0b504cc01a +# github.com/MarkRosemaker/cassette v0.0.0-20261125180338-b758ec7cd132 ## explicit; go 1.27 github.com/MarkRosemaker/cassette # github.com/MarkRosemaker/jsonutil v0.0.0-20261118230255-4b04cfc02484 From 8a6898f454d564a96f413e5227dbb90bd8384633 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 18:10:27 +0000 Subject: [PATCH 4/4] chore: pin cassette to main, which has OptionalHeaders Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y --- go.mod | 2 +- go.sum | 4 ++-- vendor/modules.txt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 3c3c49e..ab760e2 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/go-api-libs/api go 1.27 -require github.com/MarkRosemaker/cassette v0.0.0-20261125180338-b758ec7cd132 +require github.com/MarkRosemaker/cassette v0.0.0-20261125180804-407fe91b7b7d require ( cloud.google.com/go v0.123.0 // indirect diff --git a/go.sum b/go.sum index 2ca0cb7..8cec24d 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ cloud.google.com/go v0.123.0 h1:2NAUJwPR47q+E35uaJeYoNhuNEM9kM8SjgRgdeOJUSE= cloud.google.com/go v0.123.0/go.mod h1:xBoMV08QcqUGuPW65Qfm1o9Y4zKZBpGS+7bImXLTAZU= -github.com/MarkRosemaker/cassette v0.0.0-20261125180338-b758ec7cd132 h1:Lo3IOIsseUaFH7zM9qjrSgMgLt+23wGByQYZWn+m39Q= -github.com/MarkRosemaker/cassette v0.0.0-20261125180338-b758ec7cd132/go.mod h1:mIy1SlNqKDRALYBqZGjj+MWPU9dASkyP1d1X+I1HP6k= +github.com/MarkRosemaker/cassette v0.0.0-20261125180804-407fe91b7b7d h1:QJ5PKBI67moq/DdVbA2yxn+TC+MKNAnGqA/KPaHp0TM= +github.com/MarkRosemaker/cassette v0.0.0-20261125180804-407fe91b7b7d/go.mod h1:mIy1SlNqKDRALYBqZGjj+MWPU9dASkyP1d1X+I1HP6k= github.com/MarkRosemaker/jsonutil v0.0.0-20261118230255-4b04cfc02484 h1:mWQovC60M6QVWXpitDiB2bwT+Jr8uQH2eyjOcUE2EcI= github.com/MarkRosemaker/jsonutil v0.0.0-20261118230255-4b04cfc02484/go.mod h1:SzjJlgYFlzpbFGvUUoQgfEsWlbai63bvhDPK8J0MNYM= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= diff --git a/vendor/modules.txt b/vendor/modules.txt index 05b7cb3..4348e3d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,7 +1,7 @@ # cloud.google.com/go v0.123.0 ## explicit; go 1.24.0 cloud.google.com/go/civil -# github.com/MarkRosemaker/cassette v0.0.0-20261125180338-b758ec7cd132 +# github.com/MarkRosemaker/cassette v0.0.0-20261125180804-407fe91b7b7d ## explicit; go 1.27 github.com/MarkRosemaker/cassette # github.com/MarkRosemaker/jsonutil v0.0.0-20261118230255-4b04cfc02484