-
Notifications
You must be signed in to change notification settings - Fork 2
/
util_test.go
175 lines (156 loc) · 3.76 KB
/
util_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package rediscloud_api
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func clientFromTestServer(s *httptest.Server, apiKey string, secretKey string) (*Client, error) {
return NewClient(LogRequests(true), BaseURL(s.URL), Auth(apiKey, secretKey), Transporter(s.Client().Transport))
}
func testServer(apiKey, secretKey string, mockedResponses ...endpointRequest) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(strings.ToLower(r.Header.Get("User-Agent")), "go-http-client") {
w.WriteHeader(504)
return
}
if r.Header.Get("X-Api-Key") != apiKey {
w.WriteHeader(502)
return
}
if r.Header.Get("X-Api-Secret-Key") != secretKey {
w.WriteHeader(503)
return
}
mockedResponse := mockedResponses[0]
if !mockedResponse.matches(r) {
w.WriteHeader(501)
return
}
response := mockedResponse.response()
mockedResponses = mockedResponses[1:]
w.WriteHeader(mockedResponse.status)
_, _ = w.Write([]byte(response))
}
}
type endpointRequest struct {
method string
path string
query url.Values
requestBody *string
body string
status int
t *testing.T
}
func (e endpointRequest) matches(r *http.Request) bool {
if e.requestBody != nil {
request, err := ioutil.ReadAll(r.Body)
require.NoError(e.t, err)
if !assert.JSONEq(e.t, *e.requestBody, string(request)) {
return false
}
} else {
if !assert.Empty(e.t, r.Body) {
return false
}
}
if e.query != nil {
if !assert.Equal(e.t, e.query, r.URL.Query()) {
return false
}
} else {
if !assert.Empty(e.t, r.URL.RawQuery) {
return false
}
}
if !assert.Equal(e.t, e.method, r.Method) {
return false
}
if !assert.Equal(e.t, e.path, r.URL.Path) {
return false
}
return true
}
func (e endpointRequest) response() string {
return e.body
}
func getRequest(t *testing.T, path string, body string) endpointRequest {
return endpointRequest{
method: http.MethodGet,
path: path,
body: body,
status: http.StatusOK,
t: t,
}
}
func getRequestWithQuery(t *testing.T, path string, query url.Values, body string) endpointRequest {
return endpointRequest{
method: http.MethodGet,
path: path,
body: body,
query: query,
status: http.StatusOK,
t: t,
}
}
func getRequestWithQueryAndStatus(t *testing.T, path string, query url.Values, status int, body string) endpointRequest {
return endpointRequest{
method: http.MethodGet,
path: path,
body: body,
query: query,
status: status,
t: t,
}
}
func getRequestWithStatus(t *testing.T, path string, status int, body string) endpointRequest {
return endpointRequest{
method: http.MethodGet,
path: path,
body: body,
status: status,
t: t,
}
}
func deleteRequest(t *testing.T, path string, body string) endpointRequest {
return endpointRequest{
method: http.MethodDelete,
path: path,
body: body,
status: http.StatusOK,
t: t,
}
}
func postRequest(t *testing.T, path string, request string, body string) endpointRequest {
return endpointRequest{
method: http.MethodPost,
path: path,
body: body,
requestBody: &request,
status: http.StatusOK,
t: t,
}
}
func postRequestWithNoRequest(t *testing.T, path string, body string) endpointRequest {
return endpointRequest{
method: http.MethodPost,
path: path,
body: body,
status: http.StatusOK,
t: t,
}
}
func putRequest(t *testing.T, path string, request string, body string) endpointRequest {
return endpointRequest{
method: http.MethodPut,
path: path,
body: body,
requestBody: &request,
status: http.StatusOK,
t: t,
}
}