-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.go
232 lines (207 loc) · 4.71 KB
/
tester.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
package tester
import (
"encoding/json"
"errors"
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/session"
"io"
"net/http"
"net/http/httptest"
"runtime"
"strconv"
"strings"
"sync"
)
type tmanager struct{}
type tester struct {
err error
req *http.Request
control beego.ControllerInterface
params map[string]interface{}
beforeCallback func() error
afterCallback func() error
beec *beego.Controller
sync.RWMutex
}
// 创建 tester 管理器
func NewTester() *tmanager {
return new(tmanager)
}
func (t *tmanager) Controller(ctrl *beego.Controller) *tester {
return new(tester).Controller(ctrl)
}
func (t *tester) Reset() *tester {
t.req = nil
t.beec = nil
return t
}
func (t *tester) BeforeCallback(callback func() error) *tester {
t.Lock()
defer t.Unlock()
t.beforeCallback = callback
return t
}
func (t *tester) AfterCallback(callback func() error) *tester {
t.Lock()
defer t.Unlock()
t.afterCallback = callback
return t
}
func (t *tester) SetParams(p map[string]interface{}) *tester {
t.Lock()
defer t.Unlock()
t.params = p
return t
}
func (t *tester) Params(p map[string]interface{}) *tester {
t.Lock()
defer t.Unlock()
t.params = p
return t
}
func (t *tester) Controller(bc *beego.Controller) *tester {
t.Lock()
if bc == nil {
panic("beego.Controller can not nil")
}
bc.Ctx = &context.Context{
Request: &http.Request{},
}
globalSessions, _ := session.NewManager("memory", &session.ManagerConfig{CookieName: ""})
bc.Ctx.Request.Header = http.Header{}
bc.CruSession = globalSessions.SessionRegenerateID(bc.Ctx.ResponseWriter, bc.Ctx.Request)
t.control = bc
t.beec = bc
t.Unlock()
return t
}
func (t *tester) SetSession(name interface{}, value interface{}) *tester {
t.Lock()
defer t.Unlock()
if t.beec == nil {
panic("beego.Controller can not nil")
}
t.beec.SetSession(name, value)
return t
}
func (t *tester) AddCookie(name string, value string) *tester {
t.Lock()
defer t.Unlock()
if t.beec == nil {
panic("beego.Controller can not nil")
}
t.beec.Ctx.Request.AddCookie(&http.Cookie{Name: name, Value: value})
return t
}
func (t *tester) Request(r *http.Request) *tester {
t.Lock()
defer t.Unlock()
t.req = r
return t
}
func (t *tester) request(method string, path string, reader io.Reader, contentType string) *tester {
r, err := http.NewRequest(method, path, reader)
if err != nil {
panic("Can't create Request:%s" + err.Error())
}
r.Header.Set("Content-Type", contentType)
t.req = r
return t
}
func (t *tester) Run(h func()) (interface{}, error) {
t.Lock()
defer t.Unlock()
beego.BConfig.CopyRequestBody = true
t.err = nil
t.request("GET", "", nil, "application/json")
if t.beforeCallback != nil {
err := t.beforeCallback()
if err != nil {
return nil, err
}
}
recover := httptest.NewRecorder()
t.initContext(t.req, recover)
from := t.req.Form
if from == nil {
from = make(map[string][]string)
}
if t.params != nil {
for k, v := range t.params {
from.Set(k, fmt.Sprint(v))
}
}
t.params = nil
t.req.Form = from
t.run(h)
t.control.Finish()
if t.afterCallback != nil {
err := t.afterCallback()
if err != nil {
return nil, err
}
}
return t.beec.Data["json"], t.err
}
func (t *tester) run(h func()) {
defer func() {
err := recover()
if err == nil || err == beego.ErrAbort {
return
}
printStack := func() []byte {
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, false)
if n < len(buf) {
return buf[:n]
}
buf = make([]byte, 2*len(buf))
}
}
t.err = errors.New(string(printStack()))
}()
h()
return
}
func (t *tester) initContext(r *http.Request, rw http.ResponseWriter) {
ctx := context.NewContext()
ctx.Request = r
ctx.Reset(rw, r)
if beego.BConfig.RecoverFunc != nil {
defer beego.BConfig.RecoverFunc(ctx)
}
var urlPath = r.URL.Path
if !beego.BConfig.RouterCaseSensitive {
urlPath = strings.ToLower(urlPath)
}
if _, ok := beego.HTTPMETHOD[r.Method]; !ok {
http.Error(rw, "Method Not Allowed", 405)
return
}
if r.Method != http.MethodGet && r.Method != http.MethodHead {
if beego.BConfig.CopyRequestBody && !ctx.Input.IsUpload() {
ctx.Input.CopyBody(beego.BConfig.MaxMemory)
}
ctx.Input.ParseFormOrMulitForm(beego.BConfig.MaxMemory)
}
if splat := ctx.Input.Param(":splat"); splat != "" {
for k, v := range strings.Split(splat, "/") {
ctx.Input.SetParam(strconv.Itoa(k), v)
}
}
if t.params != nil {
for k, v := range t.params {
ctx.Input.SetParam(k, fmt.Sprint(v))
}
}
t.control.Init(ctx, "", "", t.control)
t.control.Prepare()
}
func Receive(sources, target interface{}) {
if result, err := json.Marshal(sources); err == nil {
json.Unmarshal(result, &target)
}
}