Skip to content

Commit

Permalink
Add reqtest.Recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
earthboundkid committed Dec 26, 2024
1 parent 02ba493 commit 64a20b1
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 11 deletions.
3 changes: 2 additions & 1 deletion builder_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import (
"strings"

"github.com/carlmjohnson/requests"
"github.com/carlmjohnson/requests/reqtest"
)

func init() {
http.DefaultClient.Transport = requests.Replay("testdata")
http.DefaultClient.Transport = reqtest.Recorder(reqtest.ModeReplay, nil, "testdata")
}

func Example() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/carlmjohnson/requests

go 1.22
go 1.23

require golang.org/x/net v0.33.0
42 changes: 42 additions & 0 deletions reqtest/recorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package reqtest

import (
"net/http"

"github.com/carlmjohnson/requests"
)

// RecorderMode is an argument type controlling [Recorder].
type RecorderMode int8

//go:generate stringer -type=RecorderMode

// Enum values for type RecorderMode
const (
// Record HTTP requests and responses to text files.
ModeRecord RecorderMode = iota
// Replay responses from pre-recorded text files.
ModeReplay
// Replay responses from pre-recorded files if present,
// 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 {
switch mode {
case ModeReplay:
return Replay(basepath)
case ModeRecord:
return Record(rt, basepath)
case ModeCache:
return Caching(rt, basepath)
default:
panic("invalid reqtest.RecorderMode")
}
}
15 changes: 12 additions & 3 deletions reqtest/recorder_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reqtest_test
import (
"context"
"fmt"
"os"
"testing/fstest"

"github.com/carlmjohnson/requests"
Expand All @@ -28,21 +29,29 @@ An example response.`
// true
}

func ExampleReplayFS() {
fsys := fstest.MapFS{
func ExampleRecorder() {
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
Content-Type: text/plain; charset=UTF-8
Date: Mon, 24 May 2021 18:48:50 GMT
An example response.`),
},
})
if err != nil {
panic(err)
}

var s string
const expected = `An example response.`
if err := requests.
URL("http://fsys.example").
Transport(reqtest.ReplayFS(fsys)).
Transport(reqtest.Recorder(reqtest.ModeReplay, nil, dir)).
ToString(&s).
Fetch(context.Background()); err != nil {
panic(err)
Expand Down
8 changes: 4 additions & 4 deletions reqtest/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import (
"github.com/carlmjohnson/requests/reqtest"
)

func TestRecordReplay(t *testing.T) {
func TestRecorder(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(reqtest.Record(baseTrans, dir)).
Transport(reqtest.Recorder(reqtest.ModeRecord, baseTrans, dir)).
ToString(&s1).
Fetch(context.Background())
be.NilErr(t, err)

err = requests.URL("http://example.com").
Transport(reqtest.Replay(dir)).
Transport(reqtest.Recorder(reqtest.ModeReplay, nil, dir)).
ToString(&s2).
Fetch(context.Background())
be.NilErr(t, err)
Expand All @@ -48,7 +48,7 @@ func TestCaching(t *testing.T) {
}
return
}
trans := reqtest.Caching(onceTrans, dir)
trans := reqtest.Recorder(reqtest.ModeCache, onceTrans, dir)
var s1, s2 string
err := requests.URL("http://example.com").
Transport(trans).
Expand Down
25 changes: 25 additions & 0 deletions reqtest/recordermode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions reqtest/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func ReplayString(rawResponse string) requests.Transport {
// requests and their responses to text files in basepath.
// Requests are named according to a hash of their contents.
// Responses are named according to the request that made them.
//
// Deprecated: Use [Recorder].
func Record(rt http.RoundTripper, basepath string) requests.Transport {
return requests.Record(rt, basepath)
}
Expand All @@ -25,6 +27,8 @@ func Record(rt http.RoundTripper, basepath string) requests.Transport {
// responses from text files in basepath.
// Responses are looked up according to a hash of the request.
// Response file names may optionally be prefixed with comments for better human organization.
//
// Deprecated: Use [Recorder].
func Replay(basepath string) requests.Transport {
return requests.Replay(basepath)
}
Expand All @@ -33,6 +37,8 @@ func Replay(basepath string) requests.Transport {
// responses from text files in the fs.FS.
// Responses are looked up according to a hash of the request.
// Response file names may optionally be prefixed with comments for better human organization.
//
// Deprecated: Use os.CopyFS and [Recorder].
func ReplayFS(fsys fs.FS) requests.Transport {
return requests.ReplayFS(fsys)
}
Expand All @@ -42,6 +48,8 @@ func ReplayFS(fsys fs.FS) requests.Transport {
// it caches the result of issuing the request with rt in basepath.
// Requests are named according to a hash of their contents.
// Responses are named according to the request that made them.
//
// Deprecated: Use [Recorder].
func Caching(rt http.RoundTripper, basepath string) requests.Transport {
return requests.Caching(rt, basepath)
}
3 changes: 1 addition & 2 deletions reqxml/to_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
)

func init() {
http.DefaultClient.Transport = reqtest.Replay("testdata")
// http.DefaultClient.Transport = reqtest.Caching(nil, "testdata")
http.DefaultClient.Transport = reqtest.Recorder(reqtest.ModeReplay, nil, "testdata")
}

func ExampleTo() {
Expand Down

0 comments on commit 64a20b1

Please sign in to comment.