Skip to content

Commit

Permalink
Update RequestLogger interface
Browse files Browse the repository at this point in the history
To improve ergonomics of the request logger, make the interface use a
Logf method that is also already fulfilled by the testing.T struct, so
that this can be passed into the Option without needing adaptation.
  • Loading branch information
callebjorkell committed Feb 1, 2024
1 parent 386ee0b commit af9821f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ type logStore struct {
logs []string
}

func (l *logStore) Print(s string) {
func (l *logStore) Logf(format string, args ...any) {
l.mu.Lock()
defer l.mu.Unlock()
l.logs = append(l.logs, s)
l.logs = append(l.logs, fmt.Sprintf(format, args...))
}

func TestRequestLogging(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ func WithRequestValidation() Option {
}
}

// RequestLogger is a minimal interface that can fit for example a testing.T, allowing tests to easily print logs where
// needed.
type RequestLogger interface {
Print(string)
Logf(format string, args ...any)
}

// WithRequestLogging is a functional Option that provides a logger that copper will use to log out requests and
Expand Down
4 changes: 2 additions & 2 deletions verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ func (v *Verifier) Record(res *http.Response) {
count := v.reqCounter.Add(1)
reqDump, err := httputil.DumpRequestOut(req, true)
if err == nil {
v.conf.requestLogger.Print(fmt.Sprintf("REQUEST %04d ====\n%s", count, string(reqDump)))
v.conf.requestLogger.Logf("REQUEST %04d ====\n%s", count, string(reqDump))
}

resDump, err := httputil.DumpResponse(res, true)
if err == nil {
v.conf.requestLogger.Print(fmt.Sprintf("RESPONSE %04d ====\n%s", count, string(resDump)))
v.conf.requestLogger.Logf("RESPONSE %04d ====\n%s", count, string(resDump))
}
}

Expand Down

0 comments on commit af9821f

Please sign in to comment.