-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
recorder_test.go
68 lines (60 loc) · 1.47 KB
/
recorder_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
package requests_test
import (
"context"
"io"
"net/http"
"os"
"strings"
"testing"
"github.com/carlmjohnson/requests"
"github.com/carlmjohnson/requests/internal/be"
)
func TestRecordReplay(t *testing.T) {
baseTrans := requests.ReplayString(`HTTP/1.1 200 OK
Test Document 1`)
dir := t.TempDir()
var s1, s2 string
err := requests.URL("http://example.com").
Transport(requests.Record(baseTrans, dir)).
ToString(&s1).
Fetch(context.Background())
be.NilErr(t, err)
err = requests.URL("http://example.com").
Transport(requests.Replay(dir)).
ToString(&s2).
Fetch(context.Background())
be.NilErr(t, err)
be.Equal(t, s1, s2)
be.Equal(t, "Test Document 1", s1)
}
func TestCaching(t *testing.T) {
dir := t.TempDir()
hasRun := false
content := "some content"
var onceTrans requests.RoundTripFunc = func(req *http.Request) (res *http.Response, err error) {
be.False(t, hasRun)
hasRun = true
res = &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader(content)),
}
return
}
trans := requests.Caching(onceTrans, dir)
var s1, s2 string
err := requests.URL("http://example.com").
Transport(trans).
ToString(&s1).
Fetch(context.Background())
be.NilErr(t, err)
err = requests.URL("http://example.com").
Transport(trans).
ToString(&s2).
Fetch(context.Background())
be.NilErr(t, err)
be.Equal(t, content, s1)
be.Equal(t, s1, s2)
entries, err := os.ReadDir(dir)
be.NilErr(t, err)
be.Equal(t, 2, len(entries))
}