-
Notifications
You must be signed in to change notification settings - Fork 5
/
quicktest.go
142 lines (113 loc) · 2.68 KB
/
quicktest.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
package quick
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
)
type QuickTestReturn interface {
Body() []byte
BodyStr() string
StatusCode() int
Response() *http.Response
}
type (
qTest struct {
body []byte
bodyStr string
statusCode int
response *http.Response
}
QuickMockTestServer struct {
Client *http.Client
Port int
URI string
Method string
Headers map[string]string
Body []byte
}
)
// QuickTest: This Method is a helper function to make tests with quick more quickly
// Required Params: Method (GET, POST, PUT, DELETE...), URI (only the path. Example: /test/:myParam)
// Optional Param: Body (If you don't want to define one, just ignore)
func (q Quick) QuickTest(method, URI string, headers map[string]string, body ...[]byte) (QuickTestReturn, error) {
var buffBody []byte
if len(body) > 0 {
buffBody = body[0]
}
println("")
println("")
println("method:", method, " URI:", URI, " Body:", len(buffBody))
println("")
println("")
req, err := http.NewRequest(method, URI, io.NopCloser(bytes.NewBuffer(buffBody)))
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Set(k, v)
}
rec := httptest.NewRecorder()
q.ServeHTTP(rec, req)
resp := rec.Result()
var b []byte
if resp.Body != nil {
b, err = io.ReadAll(resp.Body)
println("sssssb>", string(b))
if err != nil {
return nil, err
}
}
return &qTest{
body: b,
bodyStr: string(b),
statusCode: resp.StatusCode,
response: resp,
}, nil
}
// commented just for now
// func (q Quick) QuickTestListen(qs QuickMockTestServer) (QuickTestReturn, error) {
// port := strconv.Itoa(qs.Port)
// port = concat.String(":", port)
// URI := concat.String("http://0.0.0.0", port, qs.URI)
// req, err := http.NewRequest(qs.Method, URI, io.NopCloser(bytes.NewBuffer(qs.Body)))
// if err != nil {
// return nil, err
// }
// for k, v := range qs.Headers {
// req.Header.Set(k, v)
// }
// go q.Listen(port)
// // This is a wait time to start the server in go routine
// time.Sleep(time.Millisecond * 100)
// if qs.Client == nil {
// qs.Client = http.DefaultClient
// }
// resp, err := qs.Client.Do(req)
// if err != nil {
// return nil, err
// }
// defer resp.Body.Close()
// b, err := io.ReadAll(resp.Body)
// if err != nil {
// return nil, err
// }
// return &qTest{
// body: b,
// bodyStr: string(b),
// statusCode: resp.StatusCode,
// response: resp,
// }, nil
// }
func (qt *qTest) Body() []byte {
return qt.body
}
func (qt *qTest) BodyStr() string {
return qt.bodyStr
}
func (qt *qTest) StatusCode() int {
return qt.statusCode
}
func (qt *qTest) Response() *http.Response {
return qt.response
}