-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_main_test.go
254 lines (211 loc) · 4.6 KB
/
t_main_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package gr_test
import (
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net/http"
ht "net/http/httptest"
"net/url"
"os"
r "reflect"
"runtime"
"strings"
"testing"
u "unsafe"
)
type (
U = url.URL
V = url.Values
H = http.Header
Q = http.Request
S = http.Response
W = http.ResponseWriter
Pair = [2]interface{}
)
var testServer *ht.Server
func TestMain(m *testing.M) {
testServer = ht.NewServer(http.HandlerFunc(testHandler))
defer testServer.Close()
os.Exit(m.Run())
}
func testHandler(rew W, req *Q) {
switch req.URL.Path {
case `/json`:
testHandlerJson(rew, req)
default:
testHandlerDefault(rew, req)
}
}
func testHandlerJson(rew W, req *Q) {
try(json.NewEncoder(rew).Encode(struct {
ReqMethod string `json:"reqMethod"`
ReqUrl string `json:"reqUrl"`
ReqBody string `json:"reqBody"`
}{
ReqMethod: req.Method,
ReqUrl: req.URL.String(),
ReqBody: readStr(req.Body),
}))
}
func testHandlerDefault(rew W, req *Q) {
fmt.Fprintf(rew, `
request method: %v
request URL: %v
request body: %s
`, req.Method, req.URL, readStr(req.Body))
}
func eq(t testing.TB, exp, act interface{}) {
t.Helper()
if !r.DeepEqual(exp, act) {
t.Fatalf(`
expected (detailed):
%#[1]v
actual (detailed):
%#[2]v
expected (simple):
%[1]v
actual (simple):
%[2]v
`, exp, act)
}
}
func is(t testing.TB, exp, act interface{}) {
t.Helper()
// nolint:structcheck
type iface struct {
typ u.Pointer
dat u.Pointer
}
expIface := *(*iface)(u.Pointer(&exp))
actIface := *(*iface)(u.Pointer(&act))
if expIface != actIface {
t.Fatalf(`
expected (interface):
%#[1]v
actual (interface):
%#[2]v
expected (detailed):
%#[3]v
actual (detailed):
%#[4]v
expected (simple):
%[3]v
actual (simple):
%[4]v
`, expIface, actIface, exp, act)
}
}
func errs(t testing.TB, msg string, err error) {
if err == nil {
t.Fatalf(`expected an error with %q, got none`, msg)
}
str := err.Error()
if !strings.Contains(str, msg) {
t.Fatalf(`expected an error with a message containing %q, got %q`, msg, str)
}
}
func panics(t testing.TB, msg string, fun func()) {
t.Helper()
val := catchAny(fun)
if val == nil {
t.Fatalf(`expected %v to panic, found no panic`, funcName(fun))
}
str := fmt.Sprint(val)
if !strings.Contains(str, msg) {
t.Fatalf(`
expected %v to panic with a message containing:
%v
found the following message:
%v
`, funcName(fun), msg, str)
}
}
func eqTest(t testing.TB) func(interface{}, interface{}) {
return func(exp, act interface{}) {
t.Helper()
eq(t, exp, act)
}
}
func codeTest(t testing.TB, fun func(int) bool) func(bool, int) {
return func(exp bool, code int) {
t.Helper()
eq(t, exp, fun(code))
}
}
func funcName(val interface{}) string {
return runtime.FuncForPC(r.ValueOf(val).Pointer()).Name()
}
func catchAny(fun func()) (val interface{}) {
defer recAny(&val)
fun()
return
}
func recAny(ptr *interface{}) { *ptr = recover() }
func try(err error) {
if err != nil {
panic(err)
}
}
type Trans struct {
Req *http.Request
Res *http.Response
Err error
}
func (self *Trans) RoundTrip(req *http.Request) (*http.Response, error) {
self.Req = req
return self.Res, self.Err
}
var (
errRead = fmt.Errorf(`unexpected read`)
errClose = fmt.Errorf(`unexpected close`)
errDecode = fmt.Errorf(`failing to decode`)
)
type FailReadCloser struct{}
func (FailReadCloser) Read([]byte) (int, error) { return 0, errRead }
func (FailReadCloser) Close() error { return errClose }
type ReaderFailCloser struct{}
func (ReaderFailCloser) Read([]byte) (int, error) { return 0, io.EOF }
func (ReaderFailCloser) Close() error { return errClose }
type ReadCloseFlag struct {
DidRead bool
DidClose bool
}
func (self *ReadCloseFlag) Read([]byte) (int, error) {
self.DidRead = true
return 0, io.EOF
}
func (self *ReadCloseFlag) Close() error {
if self.DidClose {
return errClose
}
self.DidClose = true
return nil
}
func NewReaderCloseFlag(val string) *ReaderCloseFlag {
return &ReaderCloseFlag{Reader: strings.NewReader(val)}
}
type ReaderCloseFlag struct {
io.Reader
DidClose bool
}
func (self *ReaderCloseFlag) Close() error {
if self.DidClose {
return errClose
}
self.DidClose = true
return nil
}
type DecodeFail struct{}
func (self *DecodeFail) UnmarshalJSON([]byte) error { return errDecode }
func (self *DecodeFail) UnmarshalXML(*xml.Decoder, xml.StartElement) error { return errDecode }
func spr(val string) *string { return &val }
func iter(count int) []struct{} { return make([]struct{}, count) }
func readStr(src io.Reader) string {
if src == nil {
return ``
}
val, err := io.ReadAll(src)
try(err)
return string(val)
}