Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
104 lines (81 loc) · 2.33 KB
/
Copy pathexample_test.go
File metadata and controls
104 lines (81 loc) · 2.33 KB
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
package recorder_test
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"github.com/mgurevin/recorder"
)
func ExampleNewTransport() {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
_, _ = io.WriteString(writer, "ok")
}))
defer server.Close()
records := recorder.NewMemoryRecorder()
config := recorder.DefaultConfig()
if err := config.Validate(); err != nil {
panic(err)
}
client := &http.Client{
Transport: recorder.NewTransport(http.DefaultTransport, records, config),
}
response, err := client.Get(server.URL)
if err != nil {
panic(err)
}
_, _ = io.Copy(io.Discard, response.Body)
if err := response.Body.Close(); err != nil {
panic(err)
}
entry := records.Entries()[0]
fmt.Println(entry.Response.Status, entry.Recorder.State, entry.Recorder.ResponseBody.TotalBytes)
// Output: 200 completed 2
}
func ExampleConfig_redaction() {
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
writer.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(writer, `{"secret":"value","keep":1}`)
}))
defer server.Close()
records := recorder.NewMemoryRecorder()
config := recorder.DefaultConfig()
config.CaptureResponseBody = true
config.EmbedBodies = true
config.Redaction.Common.JSONFields = []string{"secret"}
if err := config.Validate(); err != nil {
panic(err)
}
client := &http.Client{
Transport: recorder.NewTransport(http.DefaultTransport, records, config),
}
response, err := client.Get(server.URL)
if err != nil {
panic(err)
}
_, _ = io.Copy(io.Discard, response.Body)
_ = response.Body.Close()
fmt.Println(records.Entries()[0].Response.Content.Text)
// Output: {"secret":"[REDACTED]","keep":1}
}
func ExampleConfig_Validate() {
config := recorder.DefaultConfig()
config.CaptureTLS = false
fmt.Println(config.Validate())
// Output: recorder: Config.CaptureCertificates requires CaptureTLS
}
func ExampleNewAsyncRecorder() {
sink := recorder.NewMemoryRecorder()
async, err := recorder.NewAsyncRecorder(sink, recorder.DefaultAsyncRecorderConfig())
if err != nil {
panic(err)
}
if err := async.Record(&recorder.Entry{}); err != nil {
panic(err)
}
if err := async.Close(context.Background()); err != nil {
panic(err)
}
fmt.Println(sink.Len())
// Output: 1
}