-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
146 lines (122 loc) · 3.86 KB
/
server.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
package mock
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/pkg/jsonq"
"github.com/getfider/fider/app/pkg/jwt"
"github.com/getfider/fider/app/pkg/web"
)
// Server is a HTTP server wrapper for testing purpose
type Server struct {
engine *web.Engine
context web.Context
recorder *httptest.ResponseRecorder
middleware []web.MiddlewareFunc
}
func createServer(services *app.Services) *Server {
settings := &models.SystemSettings{}
engine := web.New(settings)
request, _ := http.NewRequest("GET", "/", nil)
recorder := httptest.NewRecorder()
context := engine.NewContext(recorder, request, make(web.StringMap, 0))
context.SetServices(services)
return &Server{
engine: engine,
recorder: recorder,
context: context,
middleware: []web.MiddlewareFunc{},
}
}
// Engine returns current engine from mocked server
func (s *Server) Engine() *web.Engine {
return s.engine
}
// Use adds a new middleware to pipeline
func (s *Server) Use(middleware web.MiddlewareFunc) *Server {
s.middleware = append(s.middleware, middleware)
return s
}
// OnTenant set current context tenant
func (s *Server) OnTenant(tenant *models.Tenant) *Server {
s.context.SetTenant(tenant)
s.context.Services().SetCurrentTenant(tenant)
return s
}
// AsUser set current context user
func (s *Server) AsUser(user *models.User) *Server {
s.context.SetUser(user)
s.context.Services().SetCurrentUser(user)
return s
}
// AddParam to current context route parameters
func (s *Server) AddParam(name string, value interface{}) *Server {
s.context.AddParam(name, fmt.Sprintf("%v", value))
return s
}
// AddHeader add key-value to current context headers
func (s *Server) AddHeader(name string, value string) *Server {
s.context.Request.SetHeader(name, value)
return s
}
// AddCookie add key-value to current context cookies
func (s *Server) AddCookie(name string, value string) *Server {
s.context.Request.AddCookie(&http.Cookie{Name: name, Value: value})
return s
}
// WithClientIP set current ClientIP address
func (s *Server) WithClientIP(clientIP string) *Server {
s.context.Request.ClientIP = clientIP
return s
}
// WithClaims set current context user claims
func (s *Server) WithClaims(claims *jwt.FiderClaims) *Server {
s.context.SetClaims(claims)
return s
}
// WithURL set current context Request URL
func (s *Server) WithURL(fullURL string) *Server {
u, _ := url.Parse(fullURL)
s.context.Request.URL = u
return s
}
// Execute given handler and return response
func (s *Server) Execute(handler web.HandlerFunc) (int, *httptest.ResponseRecorder) {
next := handler
for i := len(s.middleware) - 1; i >= 0; i-- {
next = s.middleware[i](next)
}
if err := next(s.context); err != nil {
s.context.Failure(err)
}
return s.recorder.Code, s.recorder
}
// ExecuteAsJSON given handler and return json response
func (s *Server) ExecuteAsJSON(handler web.HandlerFunc) (int, *jsonq.Query) {
code, response := s.Execute(handler)
return code, toJSONQuery(response)
}
// ExecutePost executes given handler as POST and return response
func (s *Server) ExecutePost(handler web.HandlerFunc, body string) (int, *httptest.ResponseRecorder) {
s.context.Request.Method = "POST"
s.context.Request.Body = body
s.context.Request.ContentLength = int64(len(body))
s.context.Request.SetHeader("Content-Type", web.UTF8JSONContentType)
return s.Execute(handler)
}
// ExecutePostAsJSON executes given handler as POST and return json response
func (s *Server) ExecutePostAsJSON(handler web.HandlerFunc, body string) (int, *jsonq.Query) {
code, response := s.ExecutePost(handler, body)
return code, toJSONQuery(response)
}
func toJSONQuery(response *httptest.ResponseRecorder) *jsonq.Query {
b, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
return jsonq.New(string(b))
}