Skip to content

Commit 1f21325

Browse files
committed
Docs: Clean up reqtest.Recorder docs a little
1 parent 64a20b1 commit 1f21325

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

reqtest/recorder.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package reqtest
22

33
import (
44
"net/http"
5-
6-
"github.com/carlmjohnson/requests"
75
)
86

97
// RecorderMode is an argument type controlling [Recorder].
@@ -18,17 +16,17 @@ const (
1816
// Replay responses from pre-recorded text files.
1917
ModeReplay
2018
// Replay responses from pre-recorded files if present,
21-
// otherwise record a new request response pair.
19+
// otherwise record a new request/response pair.
2220
ModeCache
2321
)
2422

2523
// Recorder returns an HTTP transport that operates in the specified mode.
2624
// Requests and responses are read from or written to
2725
// text files in basepath according to a hash of their contents.
2826
// File names may optionally be prefixed with comments for better human organization.
29-
// The http.RoundTripper is only used in ModeRecord and ModeCache
30-
// and if nil defaults to http.DefaultTransport.
31-
func Recorder(mode RecorderMode, rt http.RoundTripper, basepath string) requests.Transport {
27+
// The http.RoundTripper is only used in [ModeRecord] and [ModeCache]
28+
// and if nil defaults to [http.DefaultTransport].
29+
func Recorder(mode RecorderMode, rt http.RoundTripper, basepath string) http.RoundTripper {
3230
switch mode {
3331
case ModeReplay:
3432
return Replay(basepath)

reqtest/recorder_example_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,49 @@ An example response.`
2929
// true
3030
}
3131

32-
func ExampleRecorder() {
32+
func copyToTempDir(m map[string]string) string {
3333
dir, err := os.MkdirTemp("", "")
3434
if err != nil {
3535
panic(err)
3636
}
37-
err = os.CopyFS(dir, fstest.MapFS{
38-
"fsys.example - MKIYDwjs.res.txt": &fstest.MapFile{
39-
Data: []byte(`HTTP/1.1 200 OK
37+
fsys := make(fstest.MapFS, len(m))
38+
for path, content := range m {
39+
fsys[path] = &fstest.MapFile{
40+
Data: []byte(content),
41+
}
42+
}
43+
if err = os.CopyFS(dir, fsys); err != nil {
44+
panic(err)
45+
}
46+
return dir
47+
}
48+
49+
func ExampleRecorder() {
50+
// Given a directory with the following file
51+
dir := copyToTempDir(map[string]string{
52+
"fsys.example - MKIYDwjs.res.txt": `HTTP/1.1 200 OK
4053
Content-Type: text/plain; charset=UTF-8
4154
Date: Mon, 24 May 2021 18:48:50 GMT
4255
43-
An example response.`),
44-
},
56+
An example response.`,
4557
})
46-
if err != nil {
47-
panic(err)
48-
}
58+
defer os.RemoveAll(dir)
59+
60+
// Make a test transport that reads the directory
61+
tr := reqtest.Recorder(reqtest.ModeReplay, nil, dir)
4962

63+
// And test that it produces the correct response
5064
var s string
5165
const expected = `An example response.`
5266
if err := requests.
5367
URL("http://fsys.example").
54-
Transport(reqtest.Recorder(reqtest.ModeReplay, nil, dir)).
68+
Transport(tr).
5569
ToString(&s).
5670
Fetch(context.Background()); err != nil {
5771
panic(err)
5872
}
5973
fmt.Println(s == expected)
74+
6075
// Output:
6176
// true
6277
}

0 commit comments

Comments
 (0)