From ae31e38c8b3e6225ceba4ed81fc4666ad1c6b094 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Thu, 25 Oct 2018 11:09:54 +0200 Subject: [PATCH 1/2] Add RequestExcludeHeaderFunc for easy upgrade To make a v2 -> v3 upgrade easier we can add a similar function that can filter request headers with the same signature as before. --- request.go | 25 +++++++++++++++++++++++++ request_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/request.go b/request.go index abd0350..25e054a 100644 --- a/request.go +++ b/request.go @@ -46,6 +46,31 @@ func RequestDeleteHeaderKeys(keys ...string) RequestFilter { } } +// RequestExcludeHeaderFunc is a hook function that is used to filter the Header. +// +// Typically this can be used to remove / amend undesirable custom headers from the request. +// +// For instance, if your application sends requests with a timestamp held in a custom header, +// you likely want to exclude it from the comparison to ensure that the request headers are +// considered a match with those saved on the cassette's track. +// +// Parameters: +// - parameter 1 - Name of header key in the Request +/// +// Return value: +// true - exclude header key from comparison +// false - retain header key for comparison +func RequestExcludeHeaderFunc(fn func(key string) bool) RequestFilter { + return func(req Request) Request { + for key := range req.Header { + if fn(key) { + req.Header.Del(key) + } + } + return req + } +} + // OnMethod will return a new filter that will only apply 'r' // if the method of the request matches. // Original filter is unmodified. diff --git a/request_test.go b/request_test.go index d3576ae..0b2ba3b 100644 --- a/request_test.go +++ b/request_test.go @@ -93,6 +93,35 @@ func TestRequestDeleteHeaderKeys(t *testing.T) { } } +func TestRequestExcludeHeaderFunc(t *testing.T) { + req := requestTestBase() + req.Header.Add("another-header", "yeah") + header1, header2 := textproto.CanonicalMIMEHeaderKey("a-header"), textproto.CanonicalMIMEHeaderKey("another-header") + + // We expect both headers to be checked. + want := map[string]struct{}{header1:{}, header2: {}} + r := RequestExcludeHeaderFunc(func(key string) bool { + _, ok := want[key] + if !ok { + t.Errorf("got unexpected key %q", key) + } + // Delete so we check we only get key once. + delete(want, key) + // Delete 'a-header' + return header1 == key + }) + req = r(req) + if len(want) > 0 { + t.Errorf("header was not checked: %v", want) + } + if len(req.Header) != 1 { + t.Fatalf("unexpected header count, want one: %v", req.Header) + } + if req.Header.Get("another-header") != "yeah" { + t.Errorf("unexpected header value: %s", req.Header.Get("another-header")) + } +} + func TestRequestFilters_Add(t *testing.T) { var f RequestFilters f1, ok1 := mustCallRequestFilterOnce(t) From 1df4573b6d97f32cc44f611441acd0f49c7c278a Mon Sep 17 00:00:00 2001 From: seborama Date: Thu, 25 Oct 2018 19:48:01 +0100 Subject: [PATCH 2/2] Added deprecation details to RequestExcludeHeaderFunc --- request.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/request.go b/request.go index 25e054a..7745a29 100644 --- a/request.go +++ b/request.go @@ -56,10 +56,12 @@ func RequestDeleteHeaderKeys(keys ...string) RequestFilter { // // Parameters: // - parameter 1 - Name of header key in the Request -/// +// // Return value: // true - exclude header key from comparison // false - retain header key for comparison +// +// Deprecated - This function will be removed on or after April 25th 2019 func RequestExcludeHeaderFunc(fn func(key string) bool) RequestFilter { return func(req Request) Request { for key := range req.Header {