Skip to content

Commit

Permalink
add error test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SVilgelm committed Sep 29, 2023
1 parent d702df2 commit 3ac1d58
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resty
import (
"bytes"
"encoding/json"
"errors"
"io"
"mime"
"mime/multipart"
Expand Down Expand Up @@ -350,13 +351,20 @@ func Benchmark_parseRequestHeader(b *testing.B) {
}
}

type errorReader struct{}

func (errorReader) Read(p []byte) (n int, err error) {
return 0, errors.New("fake")
}

func Test_parseRequestBody(t *testing.T) {
for _, tt := range []struct {
name string
init func(c *Client, r *Request)
expectedBodyBuf []byte
expectedContentLength string
expectedContentType string
wantErr bool
}{
{
name: "empty body",
Expand Down Expand Up @@ -691,13 +699,73 @@ func Test_parseRequestBody(t *testing.T) {
expectedContentType: "multipart/form-data; boundary=",
expectedContentLength: "412",
},
{
name: "body with errorReader",
init: func(c *Client, r *Request) {
r.SetBody(&errorReader{}).SetContentLength(true)
},
wantErr: true,
},
{
name: "unsupported type",
init: func(c *Client, r *Request) {
r.SetBody(1)
},
wantErr: true,
},
{
name: "unsupported xml",
init: func(c *Client, r *Request) {
r.SetBody(struct {
Foo string `xml:"foo"`
Bar string `xml:"bar"`
}{
Foo: "1",
Bar: "2",
}).Header.Set(hdrContentTypeKey, "text/xml")
},
wantErr: true,
},
{
name: "multipart fields with errorReader",
init: func(c *Client, r *Request) {
r.SetMultipartFields(&MultipartField{
Param: "foo",
ContentType: "text/plain",
Reader: &errorReader{},
})
},
wantErr: true,
},
{
name: "multipart files with errorReader",
init: func(c *Client, r *Request) {
r.SetFileReader("foo", "foo.txt", &errorReader{})
},
wantErr: true,
},
{
name: "multipart with file not found",
init: func(c *Client, r *Request) {
r.SetFormData(map[string]string{
"@foo": "foo.txt",
})
r.isMultiPart = true
},
wantErr: true,
},
} {
t.Run(tt.name, func(t *testing.T) {
c := New()
r := c.R()
tt.init(c, r)
if err := parseRequestBody(c, r); err != nil {
if tt.wantErr {
return
}
t.Errorf("parseRequestBody() error = %v", err)
} else if tt.wantErr {
t.Errorf("wanted error, but got nil")
}
switch {
case r.bodyBuf == nil && tt.expectedBodyBuf != nil:
Expand Down

0 comments on commit 3ac1d58

Please sign in to comment.