Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apitest/apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,16 @@ 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, 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{}),
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),
})

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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 5 additions & 3 deletions apitest/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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()

Expand All @@ -34,6 +35,7 @@ func Replay(t testing.TB, path string) http.RoundTripper {

match := cassette.MatchRequest(
cassette.CompareHeaders("User-Agent"),
cassette.OptionalHeaders("Accept"),
cassette.OptionalAuthorization(func(req *http.Request) bool { return req.Context().Value(optionalAuthKey{}) != nil }),
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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-20261125180804-407fe91b7b7d

require (
cloud.google.com/go v0.123.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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-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=
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/MarkRosemaker/cassette/replay.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
@@ -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-20261125180804-407fe91b7b7d
## explicit; go 1.27
github.com/MarkRosemaker/cassette
# github.com/MarkRosemaker/jsonutil v0.0.0-20261118230255-4b04cfc02484
Expand Down
Loading