5
5
package aah
6
6
7
7
import (
8
+ "io/ioutil"
8
9
"net/http"
9
10
"net/http/httptest"
10
11
"net/url"
@@ -16,173 +17,46 @@ import (
16
17
"aahframework.org/ahttp.v0"
17
18
"aahframework.org/config.v0"
18
19
"aahframework.org/essentials.v0"
19
- "aahframework.org/i18n.v0"
20
- "aahframework.org/router.v0"
21
- "aahframework.org/security.v0"
20
+ "aahframework.org/log.v0"
22
21
"aahframework.org/test.v0/assert"
23
22
)
24
23
25
- func TestBindParamTemplateFuncs (t * testing.T ) {
26
- form := url.Values {}
27
- form .Add ("names" , "Test1" )
28
- form .Add ("names" , "Test 2 value" )
29
- form .Add ("username" , "welcome" )
30
- form .
Add (
"email" ,
"[email protected] " )
31
- req1 , _ := http .NewRequest ("POST" , "http://localhost:8080/user/registration?_ref=true&locale=en-CA" , strings .NewReader (form .Encode ()))
32
- req1 .Header .Add (ahttp .HeaderContentType , ahttp .ContentTypeForm .Raw ())
33
- _ = req1 .ParseForm ()
34
-
35
- aahReq1 := ahttp .ParseRequest (req1 , & ahttp.Request {})
36
- aahReq1 .Params .Form = req1 .Form
37
- aahReq1 .Params .Path = make (map [string ]string )
38
- aahReq1 .Params .Path ["userId" ] = "100001"
39
-
40
- viewArgs := map [string ]interface {}{}
41
- viewArgs [KeyViewArgRequestParams ] = aahReq1 .Params
42
-
43
- v1 := tmplQueryParam (viewArgs , "_ref" )
44
- assert .Equal (t , "true" , v1 )
45
-
46
- v2 := tmplFormParam (viewArgs , "email" )
47
- assert .
Equal (
t ,
"[email protected] " ,
v2 )
48
-
49
- v3 := tmplPathParam (viewArgs , "userId" )
50
- assert .Equal (t , "100001" , v3 )
51
- }
52
-
53
- func TestBindParse (t * testing.T ) {
54
- defer ess .DeleteFiles ("testapp.pid" )
55
- requestParsers [ahttp .ContentTypeMultipartForm .Mime ] = multipartFormParser
56
- requestParsers [ahttp .ContentTypeForm .Mime ] = formParser
57
-
58
- // Request Query String
59
- r1 := httptest .NewRequest ("GET" , "http://localhost:8080/index.html?lang=en-CA" , nil )
60
- ctx1 := & Context {
61
- Req : ahttp .AcquireRequest (r1 ),
62
- Res : ahttp .AcquireResponseWriter (httptest .NewRecorder ()),
63
- subject : security .AcquireSubject (),
64
- route : & router.Route {MaxBodySize : 5 << 20 },
65
- values : make (map [string ]interface {}),
66
- viewArgs : make (map [string ]interface {}),
67
- }
68
-
69
- appI18n = i18n .New ()
70
-
71
- assert .Nil (t , ctx1 .Req .Locale )
72
- BindMiddleware (ctx1 , & Middleware {})
73
- assert .NotNil (t , ctx1 .Req .Locale )
74
- assert .Equal (t , "en" , ctx1 .Req .Locale .Language )
75
- assert .Equal (t , "CA" , ctx1 .Req .Locale .Region )
76
- assert .Equal (t , "en-CA" , ctx1 .Req .Locale .String ())
77
-
78
- // Request Form Values
79
- form := url.Values {}
80
- form .Add ("names" , "Test1" )
81
- form .Add ("names" , "Test 2 value" )
82
- form .Add ("username" , "welcome" )
83
- form .
Add (
"email" ,
"[email protected] " )
84
- r2 , _ := http .NewRequest ("POST" , "http://localhost:8080/user/registration" , strings .NewReader (form .Encode ()))
85
- r2 .Header .Set (ahttp .HeaderContentType , ahttp .ContentTypeForm .String ())
86
- ctx2 := & Context {
87
- Req : ahttp .AcquireRequest (r2 ),
88
- Res : ahttp .AcquireResponseWriter (httptest .NewRecorder ()),
89
- subject : security .AcquireSubject (),
90
- values : make (map [string ]interface {}),
91
- viewArgs : make (map [string ]interface {}),
92
- route : & router.Route {MaxBodySize : 5 << 20 },
93
- }
94
-
95
- BindMiddleware (ctx2 , & Middleware {})
96
- assert .NotNil (t , ctx2 .Req .Params .Form )
97
- assert .True (t , len (ctx2 .Req .Params .Form ) == 3 )
98
-
99
- // Request Form Multipart
100
- r3 , _ := http .NewRequest ("POST" , "http://localhost:8080/user/registration" , strings .NewReader (form .Encode ()))
101
- r3 .Header .Set (ahttp .HeaderContentType , ahttp .ContentTypeMultipartForm .String ())
102
- ctx3 := & Context {
103
- Req : ahttp .AcquireRequest (r3 ),
104
- subject : security .AcquireSubject (),
105
- values : make (map [string ]interface {}),
106
- viewArgs : make (map [string ]interface {}),
107
- route : & router.Route {MaxBodySize : 5 << 20 },
108
- }
109
- BindMiddleware (ctx3 , & Middleware {})
110
- assert .Nil (t , ctx3 .Req .Params .Form )
111
- assert .False (t , len (ctx3 .Req .Params .Form ) == 3 )
112
- }
113
-
114
- func TestBindParamParseLocaleFromAppConfiguration (t * testing.T ) {
24
+ func TestBindParamContentNegotiation (t * testing.T ) {
115
25
defer ess .DeleteFiles ("testapp.pid" )
116
26
117
- cfg , err := config . ParseString ( `
118
- i18n {
119
- param_name {
120
- query = "language"
121
- }
122
- }
123
- ` )
124
- appConfig = cfg
125
- bindInitialize ( & Event {})
126
-
27
+ a := newApp ()
28
+ cfg , _ := config . ParseString ( `request {
29
+ content_negotiation {
30
+ enable = true
31
+ accepted = ["application/json"]
32
+ offered = ["application/json"]
33
+ }
34
+ }` )
35
+ a . cfg = cfg
36
+ err := a . initLog ()
127
37
assert .Nil (t , err )
128
38
129
- r := httptest .NewRequest ("GET" , "http://localhost:8080/index.html?language=en-CA" , nil )
130
- ctx1 := & Context {
131
- Req : ahttp .AcquireRequest (r ),
132
- viewArgs : make (map [string ]interface {}),
133
- values : make (map [string ]interface {}),
134
- }
135
-
136
- assert .Nil (t , ctx1 .Req .Locale )
137
- BindMiddleware (ctx1 , & Middleware {})
138
- assert .NotNil (t , ctx1 .Req .Locale )
139
- assert .Equal (t , "en" , ctx1 .Req .Locale .Language )
140
- assert .Equal (t , "CA" , ctx1 .Req .Locale .Region )
141
- assert .Equal (t , "en-CA" , ctx1 .Req .Locale .String ())
142
- }
143
-
144
- func TestBindParamContentNegotiation (t * testing.T ) {
145
- defer ess .DeleteFiles ("testapp.pid" )
39
+ err = a .initBind ()
40
+ assert .Nil (t , err )
146
41
147
- errorHandlerFunc = defaultErrorHandlerFunc
148
- isContentNegotiationEnabled = true
42
+ a .Log ().(* log.Logger ).SetWriter (ioutil .Discard )
149
43
150
44
// Accepted
151
- acceptedContentTypes = []string {"application/json" }
152
45
r1 := httptest .NewRequest ("POST" , "http://localhost:8080/v1/userinfo" , nil )
153
46
r1 .Header .Set (ahttp .HeaderContentType , "application/xml" )
154
- ctx1 := & Context {
155
- Req : ahttp .AcquireRequest (r1 ),
156
- reply : acquireReply (),
157
- subject : security .AcquireSubject (),
158
- }
47
+ ctx1 := newContext (nil , r1 )
48
+ ctx1 .a = a
159
49
BindMiddleware (ctx1 , & Middleware {})
160
50
assert .Equal (t , http .StatusUnsupportedMediaType , ctx1 .Reply ().err .Code )
161
51
162
52
// Offered
163
- offeredContentTypes = []string {"application/json" }
164
53
r2 := httptest .NewRequest ("POST" , "http://localhost:8080/v1/userinfo" , nil )
165
54
r2 .Header .Set (ahttp .HeaderContentType , "application/json" )
166
55
r2 .Header .Set (ahttp .HeaderAccept , "application/xml" )
167
- ctx2 := & Context {
168
- Req : ahttp .AcquireRequest (r2 ),
169
- reply : acquireReply (),
170
- subject : security .AcquireSubject (),
171
- }
56
+ ctx2 := newContext (nil , r2 )
57
+ ctx2 .a = a
172
58
BindMiddleware (ctx2 , & Middleware {})
173
59
assert .Equal (t , http .StatusNotAcceptable , ctx2 .Reply ().err .Code )
174
-
175
- isContentNegotiationEnabled = false
176
-
177
- appConfig , _ = config .ParseString (`
178
- request {
179
- content_negotiation {
180
- accepted = ["*/*"]
181
- offered = ["*/*"]
182
- }
183
- }` )
184
- bindInitialize (& Event {})
185
- appConfig = nil
186
60
}
187
61
188
62
func TestBindAddValueParser (t * testing.T ) {
@@ -193,14 +67,6 @@ func TestBindAddValueParser(t *testing.T) {
193
67
assert .Equal (t , "valpar: value parser is already exists" , err .Error ())
194
68
}
195
69
196
- func TestBindFormBodyNil (t * testing.T ) {
197
- // Request Body is nil
198
- r1 , _ := http .NewRequest ("POST" , "http://localhost:8080/user/registration" , nil )
199
- ctx1 := & Context {Req : ahttp .AcquireRequest (r1 ), subject : security .AcquireSubject ()}
200
- result := formParser (ctx1 )
201
- assert .Equal (t , flowCont , result )
202
- }
203
-
204
70
func TestBindValidatorWithValue (t * testing.T ) {
205
71
assert .NotNil (t , Validator ())
206
72
@@ -230,3 +96,34 @@ func TestBindValidatorWithValue(t *testing.T) {
230
96
result = ValidateValue (numbers , "unique" )
231
97
assert .True (t , result )
232
98
}
99
+
100
+ func TestBindParamTemplateFuncs (t * testing.T ) {
101
+ a := newApp ()
102
+ a .viewMgr = & viewManager {a : a }
103
+
104
+ form := url.Values {}
105
+ form .Add ("names" , "Test1" )
106
+ form .Add ("names" , "Test 2 value" )
107
+ form .Add ("username" , "welcome" )
108
+ form .
Add (
"email" ,
"[email protected] " )
109
+ req1 , _ := http .NewRequest ("POST" , "http://localhost:8080/user/registration?_ref=true&locale=en-CA" , strings .NewReader (form .Encode ()))
110
+ req1 .Header .Add (ahttp .HeaderContentType , ahttp .ContentTypeForm .Raw ())
111
+ _ = req1 .ParseForm ()
112
+
113
+ aahReq1 := ahttp .ParseRequest (req1 , & ahttp.Request {})
114
+ aahReq1 .Params .Form = req1 .Form
115
+ aahReq1 .Params .Path = make (map [string ]string )
116
+ aahReq1 .Params .Path ["userId" ] = "100001"
117
+
118
+ viewArgs := map [string ]interface {}{}
119
+ viewArgs [KeyViewArgRequestParams ] = aahReq1 .Params
120
+
121
+ v1 := a .viewMgr .tmplQueryParam (viewArgs , "_ref" )
122
+ assert .Equal (t , "true" , v1 )
123
+
124
+ v2 := a .viewMgr .tmplFormParam (viewArgs , "email" )
125
+ assert .
Equal (
t ,
"[email protected] " ,
v2 )
126
+
127
+ v3 := a .viewMgr .tmplPathParam (viewArgs , "userId" )
128
+ assert .Equal (t , "100001" , v3 )
129
+ }
0 commit comments