forked from seborama/govcr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgovcr_example7_test.go
144 lines (122 loc) · 3.68 KB
/
govcr_example7_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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package govcr_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/http/httptest"
"regexp"
"github.com/seborama/govcr"
)
const example7CassetteName = "MyCassette7"
// runTestEx7 is an example use of govcr.
// This will show how bodies can be rewritten.
// We will take a varying ID from the request URL, neutralize it and also change the ID in the body of the response.
func runTestEx7(rng *rand.Rand) {
cfg := govcr.VCRConfig{
Logging: true,
}
// Order is out example body we want to modify.
type Order struct {
ID string `json:"id"`
Name string `json:"name"`
}
// Regex to extract the ID from the URL.
reOrderID := regexp.MustCompile(`/order/([^/]+)`)
// Create a local test server that serves out responses.
handler := func(w http.ResponseWriter, r *http.Request) {
id := reOrderID.FindStringSubmatch(r.URL.String())
if len(id) < 2 {
w.WriteHeader(404)
return
}
w.WriteHeader(200)
b, err := json.Marshal(Order{
ID: id[1],
Name: "Test Order",
})
if err != nil {
w.WriteHeader(500)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(b)
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
// The filter will neutralize a value in the URL.
// In this case we rewrite /order/{random} to /order/1234
// and replacing the host so it doesn't depend on the random port number.
replacePath := govcr.RequestFilter(func(req govcr.Request) govcr.Request {
req.URL.Path = "/order/1234"
req.URL.Host = "127.0.0.1"
return req
})
// Only execute when we match path.
cfg.RequestFilters.Add(replacePath.OnPath(`/order/`))
cfg.ResponseFilters.Add(
govcr.ResponseFilter(func(resp govcr.Response) govcr.Response {
req := resp.Request()
// Find the requested ID:
orderID := reOrderID.FindStringSubmatch(req.URL.String())
// Unmarshal body.
var o Order
err := json.Unmarshal(resp.Body, &o)
if err != nil {
panic(err)
}
// Change the ID
o.ID = orderID[1]
// Replace the body.
resp.Body, err = json.Marshal(o)
if err != nil {
panic(err)
}
return resp
}).OnStatus(200),
)
orderID := fmt.Sprint(rng.Uint64())
vcr := govcr.NewVCR(example7CassetteName, &cfg)
// create a request with our custom header and a random url part.
req, err := http.NewRequest("GET", server.URL+"/order/"+orderID, nil)
if err != nil {
fmt.Println(err)
}
// run the request
resp, err := vcr.Client.Do(req)
if err != nil {
fmt.Println("Error:", err)
return
}
// print outcome.
// Remove host name for consistent output
req.URL.Host = "127.0.0.1"
fmt.Println("GET", req.URL.String())
fmt.Println("Status code:", resp.StatusCode)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("Returned Body:", string(body))
fmt.Printf("%+v\n", vcr.Stats())
}
// Example_number7BodyInjection will show how bodies can be rewritten.
// We will take a varying ID from the request URL, neutralize it and also change the ID in the body of the response.
func Example_number7BodyInjection() {
// Delete cassette to enable live HTTP call
govcr.DeleteCassette(example7CassetteName, "")
// We need a predictable RNG
rng := rand.New(rand.NewSource(7))
// 1st run of the test - will use live HTTP calls
runTestEx7(rng)
// 2nd run of the test - will use playback
runTestEx7(rng)
// Output:
//GET http://127.0.0.1/order/8475284246537043955
//Status code: 200
//Returned Body: {"id":"8475284246537043955","name":"Test Order"}
//{TracksLoaded:0 TracksRecorded:1 TracksPlayed:0}
//GET http://127.0.0.1/order/2135276795452531224
//Status code: 200
//Returned Body: {"id":"2135276795452531224","name":"Test Order"}
//{TracksLoaded:1 TracksRecorded:0 TracksPlayed:1}
}