Skip to content

Commit

Permalink
chore(refactor): align method on response and use json method
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jan 31, 2025
1 parent 5aa6eda commit d1149e4
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func debugLogger(c *Client, res *Response) {
Status: res.Status(),
Proto: res.Proto(),
ReceivedAt: res.ReceivedAt(),
Duration: res.Time(),
Duration: res.Duration(),
Size: res.Size(),
Header: sanitizeHeaders(res.Header().Clone()),
Body: res.fmtBodyString(res.Request.DebugBodyLimit),
Expand Down
10 changes: 5 additions & 5 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,7 @@ func TestOutputFileWithBaseDirAndRelativePath(t *testing.T) {

assertError(t, err)
assertEqual(t, true, resp.Size() != 0)
assertEqual(t, true, resp.Time() > 0)
assertEqual(t, true, resp.Duration() > 0)

f, err1 := os.Open(filepath.Join(baseOutputDir, outputFilePath))
defer closeq(f)
Expand Down Expand Up @@ -1391,7 +1391,7 @@ func TestOutputPathDirNotExists(t *testing.T) {

assertError(t, err)
assertEqual(t, true, resp.Size() != 0)
assertEqual(t, true, resp.Time() > 0)
assertEqual(t, true, resp.Duration() > 0)
}

func TestOutputFileAbsPath(t *testing.T) {
Expand Down Expand Up @@ -1802,7 +1802,7 @@ func TestTraceInfo(t *testing.T) {
assertEqual(t, true, tr.ResponseTime >= 0)
assertEqual(t, true, tr.TotalTime >= 0)
assertEqual(t, true, tr.TotalTime < time.Hour)
assertEqual(t, true, tr.TotalTime == resp.Time())
assertEqual(t, true, tr.TotalTime == resp.Duration())
assertEqual(t, tr.RemoteAddr, serverAddr)

assertNotNil(t, tr.Clone())
Expand All @@ -1824,7 +1824,7 @@ func TestTraceInfo(t *testing.T) {
assertEqual(t, true, tr.ServerTime >= 0)
assertEqual(t, true, tr.ResponseTime >= 0)
assertEqual(t, true, tr.TotalTime >= 0)
assertEqual(t, true, tr.TotalTime == resp.Time())
assertEqual(t, true, tr.TotalTime == resp.Duration())
assertEqual(t, tr.RemoteAddr, serverAddr)
}

Expand Down Expand Up @@ -1912,7 +1912,7 @@ func TestTraceInfoOnTimeout(t *testing.T) {
assertEqual(t, true, tr.ServerTime == 0)
assertEqual(t, true, tr.ResponseTime == 0)
assertEqual(t, true, tr.TotalTime > 0)
assertEqual(t, true, tr.TotalTime == resp.Time())
assertEqual(t, true, tr.TotalTime == resp.Duration())
}

func TestDebugLoggerRequestBodyTooLarge(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ func (r *Response) Bytes() []byte {
return r.bodyBytes
}

// Time method returns the duration of HTTP response time from the request we sent
// Duration method returns the duration of HTTP response time from the request we sent
// and received a request.
//
// See [Response.ReceivedAt] to know when the client received a response and see
// `Response.Request.Time` to know when the client sent a request.
func (r *Response) Time() time.Duration {
func (r *Response) Duration() time.Duration {
if r.Request.trace != nil {
return r.Request.TraceInfo().TotalTime
}
Expand Down
2 changes: 1 addition & 1 deletion resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ func isNil(v any) bool {
func logResponse(t *testing.T, resp *Response) {
t.Helper()
t.Logf("Response Status: %v", resp.Status())
t.Logf("Response Time: %v", resp.Time())
t.Logf("Response Duration: %v", resp.Duration())
t.Logf("Response Headers: %v", resp.Header())
t.Logf("Response Cookies: %v", resp.Cookies())
t.Logf("Response Body: %v", resp)
Expand Down
5 changes: 1 addition & 4 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ func (ti TraceInfo) String() string {

// JSON method returns the JSON string of request trace information
func (ti TraceInfo) JSON() string {
buf := acquireBuffer()
defer releaseBuffer(buf)
_ = encodeJSON(buf, ti)
return buf.String()
return toJSON(ti)
}

// Clone method returns the clone copy of [TraceInfo]
Expand Down

0 comments on commit d1149e4

Please sign in to comment.