-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from seborama/FilterFuncs
[FilterFuncs] Streamlined the use of FilterFuncs
- Loading branch information
Showing
4 changed files
with
229 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"net/http" | ||
|
||
"github.com/seborama/govcr" | ||
) | ||
|
||
const example5CassetteName = "MyCassette5" | ||
|
||
// Example5 is an example use of govcr. | ||
// Supposing a fictional application where the request contains a custom header | ||
// 'X-Transaction-Id' which must be matched in the response from the server. | ||
// When replaying, the request will have a different Transaction Id than that which was recorded. | ||
// Hence the protocol (of this fictional example) is broken. | ||
// To circumvent that, we inject the new request's X-Transaction-Id into the recorded response. | ||
// Without the ResponseFilterFunc, the X-Transaction-Id in the header would not match that | ||
// of the recorded response and our fictional application would reject the response on validation! | ||
func Example5() { | ||
vcr := govcr.NewVCR(example5CassetteName, | ||
&govcr.VCRConfig{ | ||
ExcludeHeaderFunc: func(key string) bool { | ||
// ignore the X-Transaction-Id since it changes per-request | ||
return strings.ToLower(key) == "x-transaction-id" | ||
}, | ||
ResponseFilterFunc: func(respHeader http.Header, respBody string, reqHeader http.Header) (*http.Header, *string) { | ||
// overwrite X-Transaction-Id in the Response with that from the Request | ||
respHeader.Set("X-Transaction-Id", reqHeader.Get("X-Transaction-Id")) | ||
|
||
return &respHeader, &respBody | ||
}, | ||
Logging: true, | ||
}) | ||
|
||
// create a request with our custom header | ||
req, err := http.NewRequest("POST", "http://example.com/foo5", nil) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
req.Header.Add("X-Transaction-Id", time.Now().String()) | ||
|
||
// run the request | ||
resp, err := vcr.Client.Do(req) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
// verify outcome | ||
if req.Header.Get("X-Transaction-Id") != resp.Header.Get("X-Transaction-Id") { | ||
fmt.Println("Header transaction Id verification failed - this would be the live request!") | ||
} else { | ||
fmt.Println("Header transaction Id verification passed - this would be the replayed track!") | ||
} | ||
|
||
fmt.Printf("%+v\n", vcr.Stats()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.