Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ func GzipConfig(level int, h func(gw *gzip.Writer) error) Config {
// Deprecated: Use reqtest.Server.
func TestServerConfig(s *httptest.Server) Config {
return func(rb *Builder) {
// In memory servers don't set s.URL until after s.Client is called,
// so call that first.
cl := s.Client()
rb.
BaseURL(s.URL).
Client(s.Client())
Client(cl)
}
}

Expand Down
2 changes: 2 additions & 0 deletions config_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

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

func ExampleNew() {
Expand Down Expand Up @@ -52,6 +53,7 @@ func ExampleGzipConfig() {
_, err := gw.Write([]byte(`hello, world`))
return err
})).
Transport(reqtest.ReplayFile("testdata/postman-echo POST gzip pL_wuhOy.res.txt")).
ToJSON(&echo).
Fetch(context.Background())
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/carlmjohnson/requests

go 1.25.0

require golang.org/x/net v0.55.0
require golang.org/x/net v0.58.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8=
golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww=
golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To=
golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU=
16 changes: 16 additions & 0 deletions reqtest/transport.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package reqtest

import (
"bufio"
"bytes"
"io/fs"
"net/http"
"os"

"github.com/carlmjohnson/requests"
)
Expand All @@ -29,6 +32,19 @@ func Replay(basepath string) requests.Transport {
return requests.Replay(basepath)
}

// ReplayFile returns an http.RoundTripper that reads its
// response from the response file at path.
func ReplayFile(path string) requests.Transport {
return requests.RoundTripFunc(func(req *http.Request) (res *http.Response, err error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
r := bufio.NewReader(bytes.NewReader(b))
return http.ReadResponse(r, req)
})
}

// ReplayFS returns an http.RoundTripper that reads its
// responses from text files in the fs.FS.
// Responses are looked up according to a hash of the request.
Expand Down
Loading