Skip to content

Commit

Permalink
Docs: Clean up reqtest.Recorder docs a little
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Dec 26, 2024
1 parent 64a20b1 commit 1f21325
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
10 changes: 4 additions & 6 deletions reqtest/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package reqtest

import (
"net/http"

"github.com/carlmjohnson/requests"
)

// RecorderMode is an argument type controlling [Recorder].
Expand All @@ -18,17 +16,17 @@ const (
// Replay responses from pre-recorded text files.
ModeReplay
// Replay responses from pre-recorded files if present,
// otherwise record a new request response pair.
// otherwise record a new request/response pair.
ModeCache
)

// Recorder returns an HTTP transport that operates in the specified mode.
// Requests and responses are read from or written to
// text files in basepath according to a hash of their contents.
// File names may optionally be prefixed with comments for better human organization.
// The http.RoundTripper is only used in ModeRecord and ModeCache
// and if nil defaults to http.DefaultTransport.
func Recorder(mode RecorderMode, rt http.RoundTripper, basepath string) requests.Transport {
// The http.RoundTripper is only used in [ModeRecord] and [ModeCache]
// and if nil defaults to [http.DefaultTransport].
func Recorder(mode RecorderMode, rt http.RoundTripper, basepath string) http.RoundTripper {
switch mode {
case ModeReplay:
return Replay(basepath)
Expand Down
35 changes: 25 additions & 10 deletions reqtest/recorder_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,49 @@ An example response.`
// true
}

func ExampleRecorder() {
func copyToTempDir(m map[string]string) string {
dir, err := os.MkdirTemp("", "")
if err != nil {
panic(err)
}
err = os.CopyFS(dir, fstest.MapFS{
"fsys.example - MKIYDwjs.res.txt": &fstest.MapFile{
Data: []byte(`HTTP/1.1 200 OK
fsys := make(fstest.MapFS, len(m))
for path, content := range m {
fsys[path] = &fstest.MapFile{
Data: []byte(content),
}
}
if err = os.CopyFS(dir, fsys); err != nil {
panic(err)
}
return dir
}

func ExampleRecorder() {
// Given a directory with the following file
dir := copyToTempDir(map[string]string{
"fsys.example - MKIYDwjs.res.txt": `HTTP/1.1 200 OK
Content-Type: text/plain; charset=UTF-8
Date: Mon, 24 May 2021 18:48:50 GMT
An example response.`),
},
An example response.`,
})
if err != nil {
panic(err)
}
defer os.RemoveAll(dir)

// Make a test transport that reads the directory
tr := reqtest.Recorder(reqtest.ModeReplay, nil, dir)

// And test that it produces the correct response
var s string
const expected = `An example response.`
if err := requests.
URL("http://fsys.example").
Transport(reqtest.Recorder(reqtest.ModeReplay, nil, dir)).
Transport(tr).
ToString(&s).
Fetch(context.Background()); err != nil {
panic(err)
}
fmt.Println(s == expected)

// Output:
// true
}

0 comments on commit 1f21325

Please sign in to comment.