-
Notifications
You must be signed in to change notification settings - Fork 9
/
http.go
197 lines (171 loc) · 6.89 KB
/
http.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package testutil
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"github.com/stretchr/testify/require"
)
const contentTypeAppJSON = "application/json"
type errorRespData struct {
Domain string `json:"domain"`
Code string `json:"code"`
}
type wrappedErrorRespData struct {
Error errorRespData `json:"error"`
}
var (
requireWrappedErrorInResponse = makeRequireWrappedErrorInResponse(true)
requireNoWrappedErrorInResponse = makeRequireWrappedErrorInResponse(false)
requireErrorInResponse = requireWrappedErrorInResponse
)
// DisableWrappingErrorInResponse disables expecting wrapped error ({"error": {"domain": "{domain}", ...} -> {"domain": "{domain}", ...})
// in response body.
func DisableWrappingErrorInResponse() {
requireErrorInResponse = requireNoWrappedErrorInResponse
}
// EnableWrappingErrorInResponse enabled expecting wrapped error ({"domain": "{domain}", ...} -> {"error": {"domain": "{domain}", ...})
// in response body.
func EnableWrappingErrorInResponse() {
requireErrorInResponse = requireWrappedErrorInResponse
}
// RequireErrorInRecorder asserts that passing httptest.ResponseRecorder contains error.
func RequireErrorInRecorder(t require.TestingT, resp *httptest.ResponseRecorder, wantHTTPCode int, wantErrDomain, wantErrCode string) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireErrorInResponse(t, resp.Code, resp.Header(), resp.Body, wantHTTPCode, wantErrDomain, wantErrCode)
}
// RequireErrorInResponse asserts that passing http.Response contains the error.
func RequireErrorInResponse(t require.TestingT, resp *http.Response, wantHTTPCode int, wantErrDomain, wantErrCode string) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireErrorInResponse(t, resp.StatusCode, resp.Header, resp.Body, wantHTTPCode, wantErrDomain, wantErrCode)
}
// RequireWrappedErrorInRecorder asserts that passing httptest.ResponseRecorder contains wrapped error.
func RequireWrappedErrorInRecorder(
t require.TestingT, resp *httptest.ResponseRecorder, wantHTTPCode int, wantErrDomain, wantErrCode string,
) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireWrappedErrorInResponse(t, resp.Code, resp.Header(), resp.Body, wantHTTPCode, wantErrDomain, wantErrCode)
}
// RequireWrappedErrorInResponse asserts that passing http.Response contains the wrapped error.
func RequireWrappedErrorInResponse(t require.TestingT, resp *http.Response, wantHTTPCode int, wantErrDomain, wantErrCode string) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireWrappedErrorInResponse(t, resp.StatusCode, resp.Header, resp.Body, wantHTTPCode, wantErrDomain, wantErrCode)
}
// RequireNoWrappedErrorInRecorder asserts that passing httptest.ResponseRecorder contains no wrapped error.
func RequireNoWrappedErrorInRecorder(
t require.TestingT, resp *httptest.ResponseRecorder, wantHTTPCode int, wantErrDomain, wantErrCode string,
) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireNoWrappedErrorInResponse(t, resp.Code, resp.Header(), resp.Body, wantHTTPCode, wantErrDomain, wantErrCode)
}
// RequireNoWrappedErrorInResponse asserts that passing http.Response contains the no wrapped error.
func RequireNoWrappedErrorInResponse(t require.TestingT, resp *http.Response, wantHTTPCode int, wantErrDomain, wantErrCode string) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireNoWrappedErrorInResponse(t, resp.StatusCode, resp.Header, resp.Body, wantHTTPCode, wantErrDomain, wantErrCode)
}
func makeRequireWrappedErrorInResponse(wrap bool) func(
t require.TestingT, code int, header http.Header, body io.Reader, wantHTTPCode int, wantErrDomain, wantErrCode string,
) {
return func(t require.TestingT, code int, header http.Header, body io.Reader, wantHTTPCode int, wantErrDomain, wantErrCode string) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
require.Equal(t, wantHTTPCode, code)
require.Equal(t, contentTypeAppJSON, header.Get("Content-Type"))
var errResp errorRespData
if wrap {
var wrappedErrResp wrappedErrorRespData
require.NoError(t, json.NewDecoder(body).Decode(&wrappedErrResp))
errResp = wrappedErrResp.Error
} else {
require.NoError(t, json.NewDecoder(body).Decode(&errResp))
}
require.Equal(t, wantErrDomain, errResp.Domain)
require.Equal(t, wantErrCode, errResp.Code)
}
}
// RequireEmptyBodyInRecorder asserts that passing httptest.ResponseRecorder contains empty body.
func RequireEmptyBodyInRecorder(t require.TestingT, resp *httptest.ResponseRecorder) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireEmptyBodyInResponse(t, resp.Body)
}
// RequireEmptyBodyInResponse asserts that passing http.Responsecontains empty body.
func RequireEmptyBodyInResponse(t require.TestingT, resp *http.Response) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireEmptyBodyInResponse(t, resp.Body)
}
func requireEmptyBodyInResponse(t require.TestingT, body io.Reader) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
bodyBytes, err := io.ReadAll(body)
require.NoError(t, err)
require.Equal(t, 0, len(bodyBytes))
}
// RequireJSONInRecorder asserts that passing httptest.ResponseRecorder contains the data in json format.
func RequireJSONInRecorder(t require.TestingT, resp *httptest.ResponseRecorder, want, dest interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireJSONInResponse(t, resp.Header(), resp.Body, want, dest)
}
// RequireJSONInResponse asserts that passing http.Response contains the data in json format.
func RequireJSONInResponse(t require.TestingT, resp *http.Response, want, dest interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireJSONInResponse(t, resp.Header, resp.Body, want, dest)
}
func requireJSONInResponse(t require.TestingT, header http.Header, body io.Reader, want, dest interface{}) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
require.Equal(t, contentTypeAppJSON, header.Get("Content-Type"))
bodyBytes, err := io.ReadAll(body)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(bodyBytes, dest))
require.Equal(t, want, dest)
}
// RequireStringJSONInRecorder asserts that passing httptest.ResponseRecorder contains the json string.
func RequireStringJSONInRecorder(t require.TestingT, resp *httptest.ResponseRecorder, want string) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireStringJSONInResponse(t, resp.Header(), resp.Body, want)
}
// RequireStringJSONInResponse asserts that passing http.Response contains the json string.
func RequireStringJSONInResponse(t require.TestingT, resp *http.Response, want string) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
requireStringJSONInResponse(t, resp.Header, resp.Body, want)
}
func requireStringJSONInResponse(t require.TestingT, header http.Header, body io.Reader, want string) {
if h, ok := t.(tHelper); ok {
h.Helper()
}
require.Equal(t, contentTypeAppJSON, header.Get("Content-Type"))
bodyBytes, err := io.ReadAll(body)
require.NoError(t, err)
require.Equal(t, want, string(bodyBytes))
}