forked from seborama/govcr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgovcr_example2_test.go
96 lines (78 loc) · 2.46 KB
/
govcr_example2_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package govcr_test
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/seborama/govcr"
)
const example2CassetteName = "MyCassette2"
// myApp is an application container.
type myApp struct {
httpClient *http.Client
}
func (app *myApp) Get(url string) (*http.Response, error) {
return app.httpClient.Get(url)
}
func (app *myApp) Post(url string) (*http.Response, error) {
// beware: don't use a ReadCloser, only a Reader!
body := strings.NewReader(`{"Msg": "This is an example request"}`)
return app.httpClient.Post(url, "application/json", body)
}
func runTestEx2(app *myApp) {
var samples = []struct {
f func(string) (*http.Response, error)
body string
}{
{app.Get, "domain in examples without prior coordination or asking for permission."},
{app.Post, "404 - Not Found"},
}
// Instantiate VCR.
vcr := govcr.NewVCR(example2CassetteName,
&govcr.VCRConfig{
Client: app.httpClient,
})
// Inject VCR's http.Client wrapper.
// The original transport has been preserved, only just wrapped into VCR's.
app.httpClient = vcr.Client
for _, td := range samples {
// Run HTTP call
resp, _ := td.f("https://www.example.com/foo")
// 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())
}
// Example2 is an example use of govcr.
// It shows the use of a VCR with a custom Client.
// Here, the app executes a GET request.
func Example_number2CustomClientVCR1() {
// Create a custom http.Transport.
tr := http.DefaultTransport.(*http.Transport)
tr.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true, // just an example, not recommended
}
// Create an instance of myApp.
// It uses the custom Transport created above and a custom Timeout.
app := &myApp{
httpClient: &http.Client{
Transport: tr,
Timeout: 15 * time.Second,
},
}
// Delete cassette to enable live HTTP call
govcr.DeleteCassette(example2CassetteName, "")
// 1st run of the test - will use live HTTP calls
runTestEx2(app)
// 2nd run of the test - will use playback
runTestEx2(app)
// Output:
// 404 text/html; charset=UTF-8 true - 404 text/html; charset=UTF-8 true - {TracksLoaded:0 TracksRecorded:2 TracksPlayed:0}
// 404 text/html; charset=UTF-8 true - 404 text/html; charset=UTF-8 true - {TracksLoaded:2 TracksRecorded:0 TracksPlayed:2}
}