Skip to content

Commit

Permalink
chore(tests): ensure all cases in the code base are covered
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorAvelar committed Mar 20, 2024
1 parent 1543714 commit 1f41133
Showing 1 changed file with 148 additions and 12 deletions.
160 changes: 148 additions & 12 deletions mollie/mollie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ func TestNewClientWithEnvVars(t *testing.T) {

func TestClient_NewAPIRequest(t *testing.T) {
type args struct {
ctx context.Context
method string
uri string
body interface{}
ctx context.Context
method string
uri string
body interface{}
options interface{}
}

type testCtxKey string
Expand All @@ -102,10 +103,11 @@ func TestClient_NewAPIRequest(t *testing.T) {
{
"request with empty context works as expected",
args{
ctx: nil,
method: http.MethodGet,
uri: "test",
body: []string{"hello", "world"},
ctx: nil,
method: http.MethodGet,
uri: "test",
body: []string{"hello", "world"},
options: nil,
},
`["hello","world"]` + "\n",
"/test",
Expand All @@ -114,10 +116,11 @@ func TestClient_NewAPIRequest(t *testing.T) {
{
"request with context works and the same as without context",
args{
ctx: context.WithValue(context.Background(), testCtxKey("test-key"), "I will make it to the other side"),
method: http.MethodGet,
uri: "test",
body: "some simple string",
ctx: context.WithValue(context.Background(), testCtxKey("test-key"), "I will make it to the other side"),
method: http.MethodGet,
uri: "test",
body: "some simple string",
options: nil,
},
"\"some simple string\"\n",
"/test",
Expand Down Expand Up @@ -432,6 +435,42 @@ func TestClient_Do(t *testing.T) {
r.URL = nil
},
},
{
"error when request response is non successful",
args{
context.Background(),
http.MethodGet,
"/test",
nil,
},
true,
fmt.Errorf("500 500 Internal Server Error: "),
http.StatusOK,
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
w.WriteHeader(http.StatusInternalServerError)
},
func(r *http.Request) {},
},
{
"execute returns an error during the response creation request",
args{
context.Background(),
http.MethodGet,
"/test",
nil,
},
true,
fmt.Errorf("unexpected EOF"),
http.StatusOK,
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, AuthHeader, "Bearer token_X12b31ggg23")
w.Header().Set("Content-Length", "1")
},
func(r *http.Request) {},
},
}

for _, c := range cases {
Expand Down Expand Up @@ -506,6 +545,89 @@ func TestCheckResponse(t *testing.T) {
}
}

func TestClient_newResponse(t *testing.T) {
type args struct {
res *http.Response
}

cases := []struct {
name string
args args
want *Response
wantErr bool
err error
}{
{
"new response fails on read operation",
args{
&http.Response{
Body: closedReader(),
},
},
&Response{},
true,
fmt.Errorf("unexpected EOF"),
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := newResponse(c.args.res)
if c.wantErr {
assert.EqualError(t, err, c.err.Error())
} else {
assert.Nil(t, err)
assert.Equal(t, c.want, got)
}
})
}
}

func TestClient_newError(t *testing.T) {
type args struct {
res *Response
}

cases := []struct {
name string
args args
want *BaseError
wantErr bool
err error
}{
{
"new error fails on read operation",
args{
&Response{Response: &http.Response{Body: closedReader(), ContentLength: 1}},
},
&BaseError{},
true,
fmt.Errorf("unexpected end of JSON input"),
},
{
"new error is constructed based on response",
args{
&Response{Response: &http.Response{Body: closedReader()}},
},
&BaseError{},
false,
nil,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
err := newError(c.args.res)
if c.wantErr {
assert.EqualError(t, err, c.err.Error())
} else {
assert.IsType(t, &BaseError{}, err)
assert.EqualValues(t, c.want, err)
}
})
}
}

// <----- Testing helpers ----->

var (
Expand Down Expand Up @@ -579,4 +701,18 @@ func encodingHandler(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{hello: [{},]}`))
}

type readCloserErr struct{}

func (r *readCloserErr) Read(p []byte) (n int, err error) {
return 0, io.ErrUnexpectedEOF
}

func (r *readCloserErr) Close() error {
return nil
}

func closedReader() io.ReadCloser {
return &readCloserErr{}
}

// <----- .Testing helpers ----->

0 comments on commit 1f41133

Please sign in to comment.