forked from seborama/govcr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgovcr_example3_test.go
69 lines (56 loc) · 1.86 KB
/
govcr_example3_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package govcr_test
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/seborama/govcr"
)
const example3CassetteName = "MyCassette3"
func runTestEx3() {
var samples = []struct {
method string
body string
}{
{"GET", "domain in examples without prior coordination or asking for permission."},
{"POST", "404 - Not Found"},
{"PUT", ""},
{"DELETE", ""},
}
// Create vcr
vcr := govcr.NewVCR(example3CassetteName,
&govcr.VCRConfig{
RequestFilters: govcr.RequestFilters{
govcr.RequestDeleteHeaderKeys("X-Custom-My-Date"),
},
})
for _, td := range samples {
// Create a request with our custom header
req, _ := http.NewRequest(td.method, "http://www.example.com/foo", nil)
req.Header.Add("X-Custom-My-Date", time.Now().String())
// Make http call
resp, _ := vcr.Client.Do(req)
// Show results
fmt.Printf("%d ", resp.StatusCode)
fmt.Printf("%s ", resp.Header.Get("Content-Type"))
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
fmt.Printf("%v ", strings.Contains(string(body), td.body))
}
fmt.Printf("%+v\n", vcr.Stats())
}
// Example_simpleVCR is an example use of govcr.
// It shows how to use govcr in the simplest case when the default
// http.Client suffices.
func Example_number3HeaderExclusionVCR() {
// Delete cassette to enable live HTTP call
govcr.DeleteCassette(example3CassetteName, "")
// 1st run of the test - will use live HTTP calls
runTestEx3()
// 2nd run of the test - will use playback
runTestEx3()
// Output:
// 404 text/html; charset=UTF-8 true 404 text/html; charset=UTF-8 true 404 text/html; charset=UTF-8 true 404 text/html; charset=UTF-8 true {TracksLoaded:0 TracksRecorded:4 TracksPlayed:0}
// 404 text/html; charset=UTF-8 true 404 text/html; charset=UTF-8 true 404 text/html; charset=UTF-8 true 404 text/html; charset=UTF-8 true {TracksLoaded:4 TracksRecorded:0 TracksPlayed:4}
}