Skip to content

Commit

Permalink
Removed ExcludeHeaderFunc and obsolete issues folder
Browse files Browse the repository at this point in the history
  • Loading branch information
seborama committed Oct 24, 2018
1 parent a744811 commit 6c9a1f1
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 105 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ deps:
go get -d -t -v ./... $(DEPS)

test: deps
go test -cover -race -parallel 2 ./ ./issues/
go test -cover -race -parallel 2 ./

examples: deps
go run ./examples/*.go
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ This simply redirects all **govcr** logging to the OS's standard Null device (e.

### Influencing request comparison programatically at runtime.

`RequestFilters` receives the request Header / Body to allow their transformation. Both the live request and the replayed request are filtered at comparison time. **Transformations are not persisted and only for the purpose of influencing comparison**.
`RequestFilters` receives the request Header / Body to allow their transformation. Both the live request and the replayed request are filtered at comparison time. **Transformations are not persisted and only for the purpose of influencing comparison**.

### Runtime transforming of the response before sending it back to the client.

Expand Down
9 changes: 3 additions & 6 deletions examples/example4.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package main

import (
"fmt"
"strings"
"time"

"net/http"
"time"

"github.com/seborama/govcr"
)
Expand All @@ -21,9 +19,8 @@ const example4CassetteName = "MyCassette4"
func Example4() {
vcr := govcr.NewVCR(example4CassetteName,
&govcr.VCRConfig{
ExcludeHeaderFunc: func(key string) bool {
// HTTP headers are case-insensitive
return strings.ToLower(key) == "x-custom-my-date"
RequestFilters: govcr.RequestFilters{
govcr.RequestDeleteHeaderKeys("X-Transaction-Id"),
},
Logging: true,
})
Expand Down
9 changes: 3 additions & 6 deletions examples/example5.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package main

import (
"fmt"
"strings"
"time"

"net/http"
"time"

"github.com/seborama/govcr"
)
Expand All @@ -23,9 +21,8 @@ const example5CassetteName = "MyCassette5"
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"
RequestFilters: govcr.RequestFilters{
govcr.RequestDeleteHeaderKeys("X-Transaction-Id"),
},
ResponseFilters: govcr.ResponseFilters{
// overwrite X-Transaction-Id in the Response with that from the Request
Expand Down
1 change: 1 addition & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func runExample(name, cassetteName string, f func()) {
fmt.Println("2nd run =======================================================")
f()
fmt.Println("Complete ======================================================")
fmt.Println()
}
func main() {
runExample("Example1", example1CassetteName, Example1)
Expand Down
55 changes: 14 additions & 41 deletions govcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ const defaultCassettePath = "./govcr-fixtures/"

// VCRConfig holds a set of options for the VCR.
type VCRConfig struct {
Client *http.Client
ExcludeHeaderFunc ExcludeHeaderFunc
Client *http.Client

// Filter to run before request is matched against cassettes.
RequestFilters RequestFilters
Expand All @@ -43,13 +42,12 @@ type VCRConfig struct {
// PCB stands for Printed Circuit Board. It is a structure that holds some
// facilities that are passed to the VCR machine to modify its internals.
type pcb struct {
Transport http.RoundTripper
ExcludeHeaderFunc ExcludeHeaderFunc
RequestFilter RequestFilter
ResponseFilter ResponseFilter
Logger *log.Logger
DisableRecording bool
CassettePath string
Transport http.RoundTripper
RequestFilter RequestFilter
ResponseFilter ResponseFilter
Logger *log.Logger
DisableRecording bool
CassettePath string
}

const trackNotFound = -1
Expand Down Expand Up @@ -86,13 +84,12 @@ func (pcbr *pcb) trackMatches(cassette *cassette, trackNumber int, req Request)
func (pcbr *pcb) headerResembles(header1 http.Header, header2 http.Header) bool {
for k := range header1 {
// TODO: a given header may have several values (and in any order)
if GetFirstValue(header1, k) != GetFirstValue(header2, k) && !pcbr.ExcludeHeaderFunc(k) {
if GetFirstValue(header1, k) != GetFirstValue(header2, k) {
return false
}
}

// finally assert the number of headers match
// TODO: perhaps should count how many pcb.ExcludeHeaderFunc() returned true and remove that count from the len to compare?
return len(header1) == len(header2)
}

Expand Down Expand Up @@ -166,13 +163,6 @@ func NewVCR(cassetteName string, vcrConfig *VCRConfig) *VCRControlPanel {
vcrConfig.Client.Transport = http.DefaultTransport
}

// use a default set of FilterFunc's
if vcrConfig.ExcludeHeaderFunc == nil {
vcrConfig.ExcludeHeaderFunc = func(key string) bool {
return false
}
}

// load cassette
cassette, err := loadCassette(cassetteName, vcrConfig.CassettePath)
if err != nil {
Expand All @@ -182,13 +172,12 @@ func NewVCR(cassetteName string, vcrConfig *VCRConfig) *VCRControlPanel {
// create PCB
pcbr := &pcb{
// TODO: create appropriate test!
DisableRecording: vcrConfig.DisableRecording,
Transport: vcrConfig.Client.Transport,
ExcludeHeaderFunc: vcrConfig.ExcludeHeaderFunc,
RequestFilter: vcrConfig.RequestFilters.combined(),
ResponseFilter: vcrConfig.ResponseFilters.combined(),
Logger: logger,
CassettePath: vcrConfig.CassettePath,
DisableRecording: vcrConfig.DisableRecording,
Transport: vcrConfig.Client.Transport,
RequestFilter: vcrConfig.RequestFilters.combined(),
ResponseFilter: vcrConfig.ResponseFilters.combined(),
Logger: logger,
CassettePath: vcrConfig.CassettePath,
}

// create VCR's HTTP client
Expand All @@ -210,22 +199,6 @@ func NewVCR(cassetteName string, vcrConfig *VCRConfig) *VCRControlPanel {
}
}

// ExcludeHeaderFunc 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
type ExcludeHeaderFunc func(key string) bool

// vcrTransport is the heart of VCR. It provides
// an http.RoundTripper that wraps over the default
// one provided by Go's http package or a custom one
Expand Down
5 changes: 2 additions & 3 deletions govcr_example3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ func runTestEx4() {
// Create vcr
vcr := govcr.NewVCR(example3CassetteName,
&govcr.VCRConfig{
ExcludeHeaderFunc: func(key string) bool {
// HTTP headers are case-insensitive
return strings.ToLower(key) == "x-custom-my-date"
RequestFilters: govcr.RequestFilters{
govcr.RequestDeleteHeaderKeys("X-Custom-My-Date"),
},
})

Expand Down
20 changes: 0 additions & 20 deletions issues/README.md

This file was deleted.

26 changes: 0 additions & 26 deletions issues/issue27_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ResponseAddHeaderValue(key, value string) ResponseFilter {
}
}

// ResponseDeleteHeader will delete one or more headers on the response when returned from vcr playback.
// ResponseDeleteHeaderKeys will delete one or more headers on the response when returned from vcr playback.
func ResponseDeleteHeaderKeys(keys ...string) ResponseFilter {
return func(resp Response) Response {
for _, key := range keys {
Expand Down

0 comments on commit 6c9a1f1

Please sign in to comment.