Tests fail if filter modifies URL
kayandra opened this issue · comments
Adding this filter to my recorder makes tests to fail.
r.AddFilter(func(_ *cassette.Interaction) error {
u, _ := url.Parse(i.Request.URL)
q := u.Query()
q.Set("access_key", "no-op")
u.RawQuery = q.Encode()
i.Request.URL = u.String()
return nil
})
The service I'm building this library for insists on using the access key as a URL param since they've been doing that since launch nearly 10 years ago.
Since I'm snapshot testing the output of the requests, I don't want the access key getting logged.
But, modifying the interaction causes subsequent tests to fail.
What can I do to fix this?
Thanks!
Hi @kayandra
I'm not really a contributor to this project, but I did write the filter functionality (quite a long time ago). Can you provide some more information about why your subsequent tests are failing?
It appears that you can set a custom matcher that also removes the query parameter from the URL when comparing the two.
func removeAPIKey(u *url.URL) *url.URL {
query := u.Query()
query.Del("apiKey")
u.RawQuery = query.Encode()
return u
}
func main() {
r.SetMatcher(func(r *http.Request, i cassette.Request) bool {
return r.Method == i.Method && removeAPIKey(r.URL).String() == i.URL
})
r.AddFilter(func(i *cassette.Interaction) error {
u, err := url.Parse(i.URL)
if err != nil {
return err
}
i.URL = removeAPIKey(u).String()
return nil
})
}
@ahornerr thanks! worked perfectly for me