Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4e10b3f

Browse filesBrowse files
committed
Updated test cases so they work with new server and template engines
1 parent 9d4c254 commit 4e10b3f
Copy full SHA for 4e10b3f
Expand file treeCollapse file tree

14 files changed

+196
-90
lines changed

‎binder_test.go

Copy file name to clipboardExpand all lines: binder_test.go
+22-1Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515
"testing"
1616
"time"
17+
"encoding/json"
1718
)
1819

1920
type A struct {
@@ -164,11 +165,31 @@ var fileBindings = []struct{ val, arrval, f interface{} }{
164165
{(*io.Reader)(nil), []io.Reader{}, ioutil.ReadAll},
165166
{(*io.ReadSeeker)(nil), []io.ReadSeeker{}, ioutil.ReadAll},
166167
}
168+
func TestJsonBinder(t *testing.T) {
169+
// Reuse the mvc_test.go multipart request to test the binder.
170+
foo := struct {
171+
A string
172+
}{}
173+
d,_ := json.Marshal(map[string]string{"a":"b"})
174+
params := &Params{JsonRequest:true,Json:d}
175+
c := NewGOContext(nil)
176+
c.Request.SetRequest(getMultipartRequest())
177+
ParseParams(params, NewRequest(c.Request ))
167178

179+
actual := Bind(params, "test", reflect.TypeOf(foo))
180+
if actual.Interface().(struct {
181+
A string
182+
}).A!="b" {
183+
t.Fail()
184+
}
185+
186+
}
168187
func TestBinder(t *testing.T) {
169188
// Reuse the mvc_test.go multipart request to test the binder.
170189
params := &Params{}
171-
ParseParams(params, NewRequest(getMultipartRequest()))
190+
c := NewGOContext(nil)
191+
c.Request.SetRequest(getMultipartRequest())
192+
ParseParams(params, NewRequest(c.Request ))
172193
params.Values = ParamTestValues
173194

174195
// Values

‎compress_test.go

Copy file name to clipboardExpand all lines: compress_test.go
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import (
1414
func TestBenchmarkCompressed(t *testing.T) {
1515
startFakeBookingApp()
1616
resp := httptest.NewRecorder()
17-
c := NewController(NewRequest(showRequest), NewResponse(resp))
17+
context := NewGOContext(nil)
18+
context.Request.SetRequest(showRequest)
19+
context.Response.SetResponse(resp)
20+
c := NewController(context)
1821
if err := c.SetAction("Hotels", "Show"); err != nil {
1922
t.Errorf("SetAction failed: %s", err)
2023
}
@@ -30,7 +33,10 @@ func BenchmarkRenderCompressed(b *testing.B) {
3033
startFakeBookingApp()
3134
resp := httptest.NewRecorder()
3235
resp.Body = nil
33-
c := NewController(NewRequest(showRequest), NewResponse(resp))
36+
context := NewGOContext(nil)
37+
context.Request.SetRequest(showRequest)
38+
context.Response.SetResponse(resp)
39+
c := NewController(context)
3440
if err := c.SetAction("Hotels", "Show"); err != nil {
3541
b.Errorf("SetAction failed: %s", err)
3642
}
@@ -47,7 +53,10 @@ func BenchmarkRenderUnCompressed(b *testing.B) {
4753
startFakeBookingApp()
4854
resp := httptest.NewRecorder()
4955
resp.Body = nil
50-
c := NewController(NewRequest(showRequest), NewResponse(resp))
56+
context := NewGOContext(nil)
57+
context.Request.SetRequest(showRequest)
58+
context.Response.SetResponse(resp)
59+
c := NewController(context)
5160
if err := c.SetAction("Hotels", "Show"); err != nil {
5261
b.Errorf("SetAction failed: %s", err)
5362
}

‎engine_adapter_go.go

Copy file name to clipboardExpand all lines: engine_adapter_go.go
+15-12Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ type GOHttpServer struct {
2323
Server *http.Server
2424
ServerInit *EngineInit
2525
MaxMultipartSize int64
26+
goContextStack *SimpleLockStack
27+
goMultipartFormStack *SimpleLockStack
2628
}
2729

2830
func (g *GOHttpServer) Init(init *EngineInit) {
2931
g.MaxMultipartSize = int64(Config.IntDefault("server.request.max.multipart.filesize", 32)) << 20 /* 32 MB */
30-
goContextStack = NewStackLock(Config.IntDefault("server.context.stack", 100),
32+
g.goContextStack = NewStackLock(Config.IntDefault("server.context.stack", 100),
3133
Config.IntDefault("server.context.maxstack", 200),
3234
func() interface{} {
3335
return NewGOContext(g)
3436
})
35-
goMultipartFormStack = NewStackLock(Config.IntDefault("server.form.stack", 100),
37+
g.goMultipartFormStack = NewStackLock(Config.IntDefault("server.form.stack", 100),
3638
Config.IntDefault("server.form.maxstack", 200),
3739
func() interface{} { return &GOMultipartForm{} })
3840
g.ServerInit = init
@@ -78,9 +80,9 @@ func (g *GOHttpServer) Handle(w http.ResponseWriter, r *http.Request) {
7880
}
7981

8082
upgrade := r.Header.Get("Upgrade")
81-
context := goContextStack.Pop().(*GOContext)
83+
context := g.goContextStack.Pop().(*GOContext)
8284
defer func() {
83-
goContextStack.Push(context)
85+
g.goContextStack.Push(context)
8486
}()
8587
context.Request.SetRequest(r)
8688
context.Response.SetResponse(w)
@@ -109,8 +111,8 @@ func (g *GOHttpServer) Name() string {
109111

110112
func (g *GOHttpServer) Stats() map[string]interface{} {
111113
return map[string]interface{}{
112-
"Go Engine Context": goContextStack.String(),
113-
"Go Engine Forms": goMultipartFormStack.String(),
114+
"Go Engine Context": g.goContextStack.String(),
115+
"Go Engine Forms": g.goMultipartFormStack.String(),
114116
}
115117
}
116118

@@ -159,14 +161,15 @@ type (
159161
GoCookie http.Cookie
160162
)
161163

162-
var (
163-
goContextStack *SimpleLockStack
164-
goMultipartFormStack *SimpleLockStack
165-
)
166-
167164
func NewGOContext(instance *GOHttpServer) *GOContext {
168165
if instance==nil {
169166
instance = &GOHttpServer{MaxMultipartSize:32 << 20}
167+
instance.goContextStack = NewStackLock(100, 200,
168+
func() interface{} {
169+
return NewGOContext(instance)
170+
})
171+
instance.goMultipartFormStack = NewStackLock(100, 200,
172+
func() interface{} { return &GOMultipartForm{} })
170173
}
171174
c:= &GOContext{Request: &GORequest{Goheader: &GOHeader{}, Engine:instance}}
172175
c.Response=&GOResponse{Goheader: &GOHeader{},Request:c.Request, Engine:instance}
@@ -234,7 +237,7 @@ func (r *GORequest) GetMultipartForm() (ServerMultipartForm, error) {
234237
if e := r.Original.ParseMultipartForm(r.Engine.MaxMultipartSize); e != nil {
235238
return nil, e
236239
}
237-
r.ParsedForm = goMultipartFormStack.Pop().(*GOMultipartForm)
240+
r.ParsedForm =r.Engine.goMultipartFormStack.Pop().(*GOMultipartForm)
238241
r.ParsedForm.Form = r.Original.MultipartForm
239242
}
240243

‎fakeapp_test.go

Copy file name to clipboardExpand all lines: fakeapp_test.go
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,6 @@ func startFakeBookingApp() {
107107
},
108108
})
109109
InitServerEngine(9000, GO_NATIVE_SERVER_ENGINE)
110+
initControllerStack()
110111
runStartupHooks()
111112
}

‎i18n_test.go

Copy file name to clipboardExpand all lines: i18n_test.go
+30-20Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,50 +89,50 @@ func TestI18nMessageWithDefaultLocale(t *testing.T) {
8989
func TestHasLocaleCookie(t *testing.T) {
9090
loadTestI18nConfig(t)
9191

92-
if found, value := hasLocaleCookie(buildRequestWithCookie("APP_LANG", "en")); !found {
92+
if found, value := hasLocaleCookie(buildRequestWithCookie("APP_LANG", "en").Request); !found {
9393
t.Errorf("Expected %s cookie with value '%s' but found nothing or unexpected value '%s'", "APP_LANG", "en", value)
9494
}
95-
if found, value := hasLocaleCookie(buildRequestWithCookie("APP_LANG", "en-US")); !found {
95+
if found, value := hasLocaleCookie(buildRequestWithCookie("APP_LANG", "en-US").Request); !found {
9696
t.Errorf("Expected %s cookie with value '%s' but found nothing or unexpected value '%s'", "APP_LANG", "en-US", value)
9797
}
98-
if found, _ := hasLocaleCookie(buildRequestWithCookie("DOESNT_EXIST", "en-US")); found {
98+
if found, _ := hasLocaleCookie(buildRequestWithCookie("DOESNT_EXIST", "en-US").Request); found {
9999
t.Errorf("Expected %s cookie to not exist, but apparently it does", "DOESNT_EXIST")
100100
}
101101
}
102102

103103
func TestHasLocaleCookieWithInvalidConfig(t *testing.T) {
104104
loadTestI18nConfigWithoutLanguageCookieOption(t)
105-
if found, _ := hasLocaleCookie(buildRequestWithCookie("APP_LANG", "en-US")); found {
105+
if found, _ := hasLocaleCookie(buildRequestWithCookie("APP_LANG", "en-US").Request); found {
106106
t.Errorf("Expected %s cookie to not exist because the configured name is missing", "APP_LANG")
107107
}
108-
if found, _ := hasLocaleCookie(buildRequestWithCookie("REVEL_LANG", "en-US")); !found {
108+
if found, _ := hasLocaleCookie(buildRequestWithCookie("REVEL_LANG", "en-US").Request); !found {
109109
t.Errorf("Expected %s cookie to exist", "REVEL_LANG")
110110
}
111111
}
112112

113113
func TestHasAcceptLanguageHeader(t *testing.T) {
114-
if found, value := hasAcceptLanguageHeader(buildRequestWithAcceptLanguages("en-US")); !found && value != "en-US" {
114+
if found, value := hasAcceptLanguageHeader(buildRequestWithAcceptLanguages("en-US").Request); !found && value != "en-US" {
115115
t.Errorf("Expected to find Accept-Language header with value '%s', found '%s' instead", "en-US", value)
116116
}
117-
if found, value := hasAcceptLanguageHeader(buildRequestWithAcceptLanguages("en-GB", "en-US", "nl")); !found && value != "en-GB" {
117+
if found, value := hasAcceptLanguageHeader(buildRequestWithAcceptLanguages("en-GB", "en-US", "nl").Request); !found && value != "en-GB" {
118118
t.Errorf("Expected to find Accept-Language header with value '%s', found '%s' instead", "en-GB", value)
119119
}
120120
}
121121

122122
func TestBeforeRequest(t *testing.T) {
123123
loadTestI18nConfig(t)
124124

125-
c := NewController(buildEmptyRequest(), nil)
125+
c := buildEmptyRequest()
126126
if I18nFilter(c, NilChain); c.Request.Locale != "" {
127127
t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "", c.Request.Locale)
128128
}
129129

130-
c = NewController(buildRequestWithCookie("APP_LANG", "en-US"), nil)
130+
c = buildRequestWithCookie("APP_LANG", "en-US")
131131
if I18nFilter(c, NilChain); c.Request.Locale != "en-US" {
132132
t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "en-US", c.Request.Locale)
133133
}
134134

135-
c = NewController(buildRequestWithAcceptLanguages("en-GB", "en-US"), nil)
135+
c = buildRequestWithAcceptLanguages("en-GB", "en-US")
136136
if I18nFilter(c, NilChain); c.Request.Locale != "en-GB" {
137137
t.Errorf("Expected to find current language '%s' in controller, found '%s' instead", "en-GB", c.Request.Locale)
138138
}
@@ -213,10 +213,14 @@ func loadTestI18nConfigWithUnknowFormatOption(t *testing.T) {
213213
Config.Raw().AddOption("DEFAULT", "i18n.unknown_format", "*** %s ***")
214214
}
215215

216-
func buildRequestWithCookie(name, value string) *Request {
216+
func buildRequestWithCookie(name, value string) *Controller {
217217
httpRequest, _ := http.NewRequest("GET", "/", nil)
218-
request := NewRequest(httpRequest)
219-
request.AddCookie(&http.Cookie{
218+
context := NewGOContext(nil)
219+
context.Request.SetRequest(httpRequest)
220+
controller := NewController(context)
221+
222+
223+
httpRequest.AddCookie(&http.Cookie{
220224
Name: name,
221225
Value: value,
222226
Path: "",
@@ -229,20 +233,26 @@ func buildRequestWithCookie(name, value string) *Request {
229233
Raw: "",
230234
Unparsed: nil,
231235
})
232-
return request
236+
return controller
233237
}
234238

235-
func buildRequestWithAcceptLanguages(acceptLanguages ...string) *Request {
239+
func buildRequestWithAcceptLanguages(acceptLanguages ...string) *Controller {
236240
httpRequest, _ := http.NewRequest("GET", "/", nil)
237-
request := NewRequest(httpRequest)
241+
context := NewGOContext(nil)
242+
context.Request.SetRequest(httpRequest)
243+
controller := NewController(context)
244+
245+
request := controller.Request
238246
for _, acceptLanguage := range acceptLanguages {
239247
request.AcceptLanguages = append(request.AcceptLanguages, AcceptLanguage{acceptLanguage, 1})
240248
}
241-
return request
249+
return controller
242250
}
243251

244-
func buildEmptyRequest() *Request {
252+
func buildEmptyRequest() *Controller {
245253
httpRequest, _ := http.NewRequest("GET", "/", nil)
246-
request := NewRequest(httpRequest)
247-
return request
254+
context := NewGOContext(nil)
255+
context.Request.SetRequest(httpRequest)
256+
controller := NewController(context)
257+
return controller
248258
}

‎invoker_test.go

Copy file name to clipboardExpand all lines: invoker_test.go
+6-5Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,20 @@ func BenchmarkSetAction(b *testing.B) {
117117

118118
func BenchmarkInvoker(b *testing.B) {
119119
startFakeBookingApp()
120-
c := Controller{
121-
ViewArgs: make(map[string]interface{}),
122-
}
120+
context := NewGOContext(nil)
121+
context.Request.SetRequest(showRequest)
122+
c := NewController(context)
123+
c.ViewArgs = make(map[string]interface{})
123124
if err := c.SetAction("Hotels", "Show"); err != nil {
124125
b.Errorf("Failed to set action: %s", err)
125126
return
126127
}
127-
c.Request = NewRequest(showRequest)
128+
128129
c.Params = &Params{Values: make(url.Values)}
129130
c.Params.Set("id", "3")
130131

131132
b.ResetTimer()
132133
for i := 0; i < b.N; i++ {
133-
ActionInvoker(&c, nil)
134+
ActionInvoker(c, nil)
134135
}
135136
}

‎params_test.go

Copy file name to clipboardExpand all lines: params_test.go
+18-12Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,23 @@ func getMultipartRequest() *http.Request {
8989
}
9090

9191
func BenchmarkParams(b *testing.B) {
92-
c := Controller{
93-
Request: NewRequest(getMultipartRequest()),
94-
Params: &Params{},
95-
}
92+
context := NewGOContext(nil)
93+
context.Request.SetRequest(showRequest)
94+
c := NewController(context)
95+
c.Params = &Params{}
96+
9697
for i := 0; i < b.N; i++ {
97-
ParamsFilter(&c, NilChain)
98+
ParamsFilter(c, NilChain)
9899
}
99100
}
100101

101102
func TestMultipartForm(t *testing.T) {
102-
c := Controller{
103-
Request: NewRequest(getMultipartRequest()),
104-
Params: &Params{},
105-
}
106-
ParamsFilter(&c, NilChain)
103+
context := NewGOContext(nil)
104+
context.Request.SetRequest(getMultipartRequest())
105+
c := NewController(context)
106+
c.Params = &Params{}
107+
108+
ParamsFilter(c, NilChain)
107109

108110
if !reflect.DeepEqual(expectedValues, map[string][]string(c.Params.Values)) {
109111
t.Errorf("Param values: (expected) %v != %v (actual)",
@@ -175,8 +177,12 @@ func BenchmarkResolveAcceptLanguage(b *testing.B) {
175177
}
176178
}
177179

178-
func buildHTTPRequestWithAcceptLanguage(acceptLanguage string) *http.Request {
180+
func buildHTTPRequestWithAcceptLanguage(acceptLanguage string) *Request {
179181
request, _ := http.NewRequest("POST", "http://localhost/path", nil)
180182
request.Header.Set("Accept-Language", acceptLanguage)
181-
return request
183+
context := NewGOContext(nil)
184+
context.Request.SetRequest(request)
185+
c := NewController(context)
186+
187+
return c.Request
182188
}

‎results_test.go

Copy file name to clipboardExpand all lines: results_test.go
+13-3Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import (
1414
func TestBenchmarkRender(t *testing.T) {
1515
startFakeBookingApp()
1616
resp := httptest.NewRecorder()
17-
c := NewController(NewRequest(showRequest), NewResponse(resp))
17+
18+
context := NewGOContext(nil)
19+
context.Request.SetRequest(showRequest)
20+
context.Response.SetResponse(resp)
21+
c := NewController(context)
1822
if err := c.SetAction("Hotels", "Show"); err != nil {
1923
t.Errorf("SetAction failed: %s", err)
2024
}
@@ -29,7 +33,10 @@ func BenchmarkRenderChunked(b *testing.B) {
2933
startFakeBookingApp()
3034
resp := httptest.NewRecorder()
3135
resp.Body = nil
32-
c := NewController(NewRequest(showRequest), NewResponse(resp))
36+
context := NewGOContext(nil)
37+
context.Request.SetRequest(showRequest)
38+
context.Response.SetResponse(resp)
39+
c := NewController(context)
3340
if err := c.SetAction("Hotels", "Show"); err != nil {
3441
b.Errorf("SetAction failed: %s", err)
3542
}
@@ -46,7 +53,10 @@ func BenchmarkRenderNotChunked(b *testing.B) {
4653
startFakeBookingApp()
4754
resp := httptest.NewRecorder()
4855
resp.Body = nil
49-
c := NewController(NewRequest(showRequest), NewResponse(resp))
56+
context := NewGOContext(nil)
57+
context.Request.SetRequest(showRequest)
58+
context.Response.SetResponse(resp)
59+
c := NewController(context)
5060
if err := c.SetAction("Hotels", "Show"); err != nil {
5161
b.Errorf("SetAction failed: %s", err)
5262
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.