-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
146 lines (127 loc) · 4.13 KB
/
client_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package rest
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Struct struct {
Status string `json:"status"`
}
func TestClientDo(t *testing.T) {
tests := map[string]struct {
method string
urlPostfix string
statusCode int
expectedStatusCodes []int
respBody []byte
v interface{}
want interface{}
wantErr error
wantWrappedErr error
}{
"positive_get": {
method: http.MethodGet,
urlPostfix: "/health",
statusCode: http.StatusOK,
expectedStatusCodes: []int{http.StatusOK},
respBody: []byte(`{"status":"ok"}`),
v: &Struct{},
want: &Struct{Status: "ok"},
},
"positive_multiple_statuses": {
method: http.MethodPost,
urlPostfix: "/health",
statusCode: http.StatusAccepted,
expectedStatusCodes: []int{http.StatusOK, http.StatusAccepted},
respBody: []byte(`{"status":"ok"}`),
v: &Struct{},
want: &Struct{Status: "ok"},
},
"positive_but_wrong_payload": {
method: http.MethodGet,
urlPostfix: "/health",
statusCode: http.StatusOK,
expectedStatusCodes: []int{http.StatusOK},
respBody: []byte(`{"error":"some error"}`),
v: &Struct{},
want: &Struct{}, // zero values
},
"positive_get_empty_body": {
method: http.MethodGet,
urlPostfix: "/health",
statusCode: http.StatusNoContent,
expectedStatusCodes: []int{http.StatusNoContent},
respBody: nil,
v: nil,
want: nil,
},
"negative_wrong_status_code": {
method: http.MethodGet,
urlPostfix: "/health",
statusCode: http.StatusInternalServerError,
expectedStatusCodes: []int{http.StatusOK},
respBody: []byte(`{"error":"some error"}`),
wantWrappedErr: errors.New("wrong status code (500 not in [200]): {\"error\":\"some error\"}"),
},
"negative_wrong_status_codes": {
method: http.MethodGet,
urlPostfix: "/health",
statusCode: http.StatusInternalServerError,
expectedStatusCodes: []int{http.StatusOK, http.StatusAccepted},
respBody: []byte(`{"error":"some error"}`),
wantWrappedErr: errors.New("wrong status code (500 not in [200, 202]): {\"error\":\"some error\"}"),
},
"negative_not_a_pointer": {
method: http.MethodGet,
urlPostfix: "/health",
statusCode: http.StatusOK,
expectedStatusCodes: []int{http.StatusOK},
respBody: []byte(`{"status":"ok"}`),
v: Struct{},
wantErr: errors.New("json: Unmarshal(non-pointer rest.Struct)"),
},
// TODO: add more test cases
}
for k, tt := range tests {
tt := tt
t.Run(k, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, tt.method, r.Method)
hasSuffix(t, r.URL.String(), tt.urlPostfix)
// Send response to be tested
w.WriteHeader(tt.statusCode)
if tt.respBody != nil {
_, err := w.Write(tt.respBody)
require.NoError(t, err, "failed to write the body")
}
}))
defer server.Close()
c := Client{
httpClient: server.Client(),
}
req, err := http.NewRequest(tt.method, server.URL+tt.urlPostfix, nil)
require.NoError(t, err)
// test:
err = c.Do(req, tt.v, tt.expectedStatusCodes...)
if tt.wantErr != nil {
require.EqualError(t, err, tt.wantErr.Error())
return
}
if tt.wantWrappedErr != nil {
require.EqualError(t, errors.Unwrap(err), tt.wantWrappedErr.Error())
return
}
require.NoError(t, err)
if tt.v != nil {
assert.EqualValues(t, tt.want, tt.v)
}
})
}
}
func hasSuffix(t *testing.T, s, suffix string) {
assert.Truef(t, strings.HasSuffix(s, suffix), "expected '%s' to ends with suffix '%s'", s, suffix)
}