-
Notifications
You must be signed in to change notification settings - Fork 1
/
RequestDispatcherTest.hx
252 lines (211 loc) · 9.95 KB
/
RequestDispatcherTest.hx
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Harfang - A Web development framework
// Copyright (C) Nicolas Juneau <n.juneau@gmail.com>
// Full copyright notice can be found in the project root's "COPYRIGHT" file
//
// This file is part of Harfang.
//
// Harfang is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Harfang is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Harfang. If not, see <http://www.gnu.org/licenses/>.
package requestdispatcher;
import unit2.TestCase;
import harfang.exception.Exception;
import harfang.exception.NotFoundException;
import harfang.RequestDispatcher;
import harfang.request.RequestInfo;
import requestdispatcher.mock.MockRequestDispatcherUserConfiguration;
import requestdispatcher.mock.MockRequestDispatcherFilterConfiguration;
import requestdispatcher.mock.MockRequestDispatcherController;
/**
* This tests the request dispatcher - make sure it's able to dispatch calls to the
* controllers correctly
*/
class RequestDispatcherTest extends TestCase {
private var dispatcher : RequestDispatcher;
private var configuration : MockRequestDispatcherUserConfiguration;
/**
* This method is called whem the test case is initialised.
*/
@BeforeClass
public function prepare() : Void {
this.configuration = new MockRequestDispatcherUserConfiguration();
this.configuration.init();
this.dispatcher = new RequestDispatcher(this.configuration);
}
/**
* Resets the test controller before each test
*/
@Before
public function before() : Void {
MockRequestDispatcherController.reset();
}
/**
* Dispatch a simple URL, without parameters
*/
@Test
public function testDispatchSimple() {
var rqInfo : RequestInfo = new RequestInfo("/", "GET");
this.dispatcher.dispatch(rqInfo);
assertTrue(MockRequestDispatcherController.getIsInit());
// Make sure correct method is dispatched
assertFalse(MockRequestDispatcherController.getDispatchedParam());
assertFalse(MockRequestDispatcherController.getDispatchedMultipleParam());
assertFalse(MockRequestDispatcherController.getDispatchedDoNotDispatch());
assertTrue(MockRequestDispatcherController.getDispatchedSimple());
assertEquals(MockRequestDispatcherController.getLastMethodName(), "dispatchSimple");
assertTrue(MockRequestDispatcherController.getCalledPostRequest());
assertEquals(MockRequestDispatcherController.getLastPostMethodName(), "dispatchSimple");
}
/**
* Same as disptach a simple URL, makes sure that the slash is appended at
* the end of the URL.
*/
@Test
public function testDispatchSlash() {
var rqInfo : RequestInfo = new RequestInfo("", "GET");
this.dispatcher.dispatch(rqInfo);
assertTrue(MockRequestDispatcherController.getIsInit());
// Make sure correct method is dispatched
assertFalse(MockRequestDispatcherController.getDispatchedParam());
assertFalse(MockRequestDispatcherController.getDispatchedMultipleParam());
assertFalse(MockRequestDispatcherController.getDispatchedDoNotDispatch());
assertTrue(MockRequestDispatcherController.getDispatchedSimple());
assertEquals(MockRequestDispatcherController.getLastMethodName(), "dispatchSimple");
assertTrue(MockRequestDispatcherController.getCalledPostRequest());
assertEquals(MockRequestDispatcherController.getLastPostMethodName(), "dispatchSimple");
}
/**
* Dispatch an URL, with a single parameter
*/
@Test
public function testDispatchParam() {
var rqInfo : RequestInfo = new RequestInfo("/abc/", "GET");
this.dispatcher.dispatch(rqInfo);
assertTrue(MockRequestDispatcherController.getIsInit());
// Make sure correct method is dispatched
assertFalse(MockRequestDispatcherController.getDispatchedSimple());
assertFalse(MockRequestDispatcherController.getDispatchedMultipleParam());
assertFalse(MockRequestDispatcherController.getDispatchedDoNotDispatch());
assertTrue(MockRequestDispatcherController.getDispatchedParam());
assertEquals(MockRequestDispatcherController.getLastMethodName(), "dispatchParam");
assertTrue(MockRequestDispatcherController.getCalledPostRequest());
assertEquals(MockRequestDispatcherController.getLastPostMethodName(), "dispatchParam");
// Make sure correct parameter is sent to the controller
assertEquals(MockRequestDispatcherController.getDispatchParamParam(), "abc");
assertTrue(MockRequestDispatcherController.getCalledPostRequest());
}
/**
* Dispatch a simple URL, with multiple parameters
*/
@Test
public function testDispatchMultipleParam() {
var rqInfo : RequestInfo = new RequestInfo("/cDe/0988/", "GET");
this.dispatcher.dispatch(rqInfo);
assertTrue(MockRequestDispatcherController.getIsInit());
// Make sure correct method is dispatched
assertFalse(MockRequestDispatcherController.getDispatchedSimple());
assertFalse(MockRequestDispatcherController.getDispatchedParam());
assertFalse(MockRequestDispatcherController.getDispatchedDoNotDispatch());
assertTrue(MockRequestDispatcherController.getDispatchedMultipleParam());
assertEquals(MockRequestDispatcherController.getLastMethodName(), "dispatchMultipleParam");
assertTrue(MockRequestDispatcherController.getCalledPostRequest());
assertEquals(MockRequestDispatcherController.getLastPostMethodName(), "dispatchMultipleParam");
// Make sure correct parameters are sent to the controller
assertEquals(MockRequestDispatcherController.getDispatchMutlipleParamParamA(), "cDe");
// Make sure correct parameters are sent to the controller
assertEquals(MockRequestDispatcherController.getDispatchMutlipleParamParamB(), "0988");
}
/**
* Dispatch the URL, but do not call controller method (stopped at
* handleRequest)
*/
@Test
public function testDoNotDispatch() {
var rqInfo : RequestInfo = new RequestInfo("/_doNotDispatch/", "GET");
this.dispatcher.dispatch(rqInfo);
assertTrue(MockRequestDispatcherController.getIsInit());
// Make sure correct method is dispatched
assertFalse(MockRequestDispatcherController.getDispatchedSimple());
assertFalse(MockRequestDispatcherController.getDispatchedParam());
assertFalse(MockRequestDispatcherController.getDispatchedMultipleParam());
assertFalse(MockRequestDispatcherController.getDispatchedDoNotDispatch());
assertEquals(MockRequestDispatcherController.getLastMethodName(), "doNotDispatch");
// The post handle request shouldn't be called when handleRequest
// returns false.
assertTrue(MockRequestDispatcherController.getLastPostMethodName() == null);
assertFalse(MockRequestDispatcherController.getCalledPostRequest());
}
/**
* Dispatch an URL that is not mapped (trigger 404)
*/
@Test
public function testDispatchNotFound() {
var rqInfo : RequestInfo = new RequestInfo("-+-+qwe", "GET");
try {
this.dispatcher.dispatch(rqInfo);
} catch(e : Exception) {
assertEquals(Type.getClass(e), NotFoundException);
}
assertFalse(MockRequestDispatcherController.getIsInit());
// No methods should be called
assertFalse(MockRequestDispatcherController.getDispatchedSimple());
assertFalse(MockRequestDispatcherController.getDispatchedParam());
assertFalse(MockRequestDispatcherController.getDispatchedMultipleParam());
assertFalse(MockRequestDispatcherController.getDispatchedDoNotDispatch());
assertTrue(MockRequestDispatcherController.getLastMethodName() == null);
assertFalse(MockRequestDispatcherController.getCalledPostRequest());
assertTrue(MockRequestDispatcherController.getLastPostMethodName() == null);
}
/**
* Tests the dispatcher "filter" and "resolve" functionnality
*/
@Test
public function testResolveFilter() {
var rqInfo : RequestInfo = new RequestInfo("/", "GET");
var filterAndResolve : MockRequestDispatcherFilterConfiguration =
new MockRequestDispatcherFilterConfiguration(true, true);
filterAndResolve.init();
var filter : MockRequestDispatcherFilterConfiguration =
new MockRequestDispatcherFilterConfiguration(false, true);
filter.init();
var resolve : MockRequestDispatcherFilterConfiguration =
new MockRequestDispatcherFilterConfiguration(true, false);
resolve.init();
// Resolving and filtering returns true
var filterDispatcher : RequestDispatcher = new RequestDispatcher(filterAndResolve);
var dispatched : Bool = true;
try {
filterDispatcher.dispatch(rqInfo);
} catch(e : NotFoundException) {
dispatched = false;
}
assertTrue(dispatched);
// Resolving returns false
filterDispatcher = new RequestDispatcher(filter);
dispatched = true;
try {
filterDispatcher.dispatch(rqInfo);
} catch(e : NotFoundException) {
dispatched = false;
}
assertFalse(dispatched);
// Filtering returns false
filterDispatcher = new RequestDispatcher(resolve);
dispatched = true;
try {
filterDispatcher.dispatch(rqInfo);
} catch(e : NotFoundException) {
dispatched = false;
}
assertFalse(dispatched);
}
}