Skip to content

Commit

Permalink
improve & add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongdalu-trechina committed Sep 25, 2023
1 parent 7f99be0 commit 732c199
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
29 changes: 13 additions & 16 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (

const debugRequestLogKey = "__restyDebugRequestLog"

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Request Middleware(s)
//_______________________________________________________________________
// _______________________________________________________________________

func parseRequestURL(c *Client, r *Request) error {
// GitHub #103 Path Params
Expand Down Expand Up @@ -183,6 +183,7 @@ CL:
}

func createHTTPRequest(c *Client, r *Request) (err error) {
var bodyCopy = acquireBuffer()
if r.bodyBuf == nil {
if reader, ok := r.Body.(io.Reader); ok && isPayloadSupported(r.Method, c.AllowGetMethodPayload) {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, reader)
Expand All @@ -191,9 +192,12 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
} else {
r.RawRequest, err = http.NewRequest(r.Method, r.URL, nil)
}
bodyCopy, err = getRawRequestBodyCopy(r)
if err != nil {
return err
}
} else {
// deep copy
bodyCopy := acquireBuffer()
_, err := io.Copy(bodyCopy, bytes.NewReader(r.bodyBuf.Bytes()))
if err != nil {
return err
Expand Down Expand Up @@ -232,17 +236,10 @@ func createHTTPRequest(c *Client, r *Request) (err error) {
r.RawRequest = r.RawRequest.WithContext(r.ctx)
}

if r.bodyBuf == nil {
bodyCopy, err := getRawRequestBodyCopy(r)
if err != nil {
return err
}

if bodyCopy != nil {
// assign get body func for the underlying raw request instance
r.RawRequest.GetBody = func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(bodyCopy.Bytes())), nil
}
if bodyCopy != nil && bodyCopy.Len() > 0 {
// assign get body func for the underlying raw request instance
r.RawRequest.GetBody = func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(bodyCopy.Bytes())), nil
}
}

Expand Down Expand Up @@ -312,9 +309,9 @@ func requestLogger(c *Client, r *Request) error {
return nil
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Response Middleware(s)
//_______________________________________________________________________
// _______________________________________________________________________

func responseLogger(c *Client, res *Response) error {
if res.Request.Debug {
Expand Down
45 changes: 45 additions & 0 deletions middleware_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resty

import (
"bytes"
"net/url"
"testing"
)
Expand Down Expand Up @@ -227,3 +228,47 @@ func Test_parseRequestURL(t *testing.T) {
})
}
}

func Test_createHTTPRequest(t *testing.T) {
type args struct {
c *Client
r *Request
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "bodyBuf is not nil, deep copy",
args: args{
c: &Client{},
r: func() *Request {
req := &Request{}
req.bodyBuf = bytes.NewBufferString("test")
return req
}(),
},
wantErr: false,
},
{
name: "bodyBuf is nil, deep copy",
args: args{
c: &Client{},
r: func() *Request {
req := &Request{}
req.bodyBuf = nil
return req
}(),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := createHTTPRequest(tt.args.c, tt.args.r); (err != nil) != tt.wantErr {
t.Errorf("createHTTPRequest() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 732c199

Please sign in to comment.