Add Request::Filter and ::FilterArray.
[hoplite.git] / testing / tests / http / root_controller_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use hoplite\http as http;
19
20 require_once HOPLITE_ROOT . '/http/action.php';
21 require_once HOPLITE_ROOT . '/http/output_filter.php';
22 require_once HOPLITE_ROOT . '/http/root_controller.php';
23 require_once HOPLITE_ROOT . '/http/url_map.php';
24
25 class ActionReporter extends http\Action
26 {
27 public $did_filter_request = FALSE;
28 public $did_invoke = FALSE;
29 public $did_filter_response = FALSE;
30
31 public function FilterRequest(http\Request $q, http\Response $s)
32 {
33 $this->did_filter_request = TRUE;
34 }
35
36 public function Invoke(http\Request $q, http\Response $s)
37 {
38 $this->did_invoke = TRUE;
39 }
40
41 public function FilterResponse(http\Request $q, http\Response $s)
42 {
43 $this->did_filter_response = TRUE;
44 }
45 }
46
47 class RootControllerTest extends \PHPUnit_Framework_TestCase
48 {
49 /*!
50 Configures a mock RootControler.
51 @param array|NULL Array of methods to mock
52 @param varargs Constructor parameters.
53 @return Mock RootControler
54 */
55 public function ConfigureMock()
56 {
57 $args = func_get_args();
58 return $this->getMock('hoplite\http\RootController', $args[0], array_slice($args, 1));
59 }
60
61 public function testRun()
62 {
63 $globals = array('_SERVER' => array(
64 'REQUEST_METHOD' => 'GET',
65 'PATH_INFO' => '/some/action/42'
66 ));
67 $mock = $this->ConfigureMock(array('RouteRequest', 'Stop'), $globals);
68
69 $mock->request()->url = 'some/action/42';
70
71 $mock->expects($this->once())
72 ->method('RouteRequest')
73 ->with($this->equalTo($mock->request()));
74
75 $mock->expects($this->once())
76 ->method('Stop');
77
78 $mock->Run();
79 }
80
81 public function testInvokeAction()
82 {
83 $globals = array();
84 $fixture = new http\RootController($globals);
85
86 $action = new ActionReporter($fixture);
87
88 $this->assertFalse($action->did_filter_request);
89 $this->assertFalse($action->did_invoke);
90 $this->assertFalse($action->did_filter_response);
91
92 $fixture->InvokeAction($action);
93
94 $this->assertTrue($action->did_filter_request);
95 $this->assertTrue($action->did_invoke);
96 $this->assertTrue($action->did_filter_response);
97 }
98
99 public function testRouteRequest()
100 {
101 $globals = array();
102 $mock = $this->ConfigureMock(array('Stop', 'InvokeAction'), $globals);
103
104 $mock->request()->url = 'some/action/42';
105 $map_value = 'ActionReporter';
106 $action = new ActionReporter($mock);
107
108 $mock->expects($this->once())
109 ->method('InvokeAction')
110 ->with($this->isInstanceOf('hoplite\test\ActionReporter'));
111
112 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
113 $url_map->expects($this->once())
114 ->method('Evaluate')
115 ->with($this->equalTo($mock->request()))
116 ->will($this->returnValue($map_value));
117 $url_map->expects($this->once())
118 ->method('LookupAction')
119 ->with($this->equalTo($map_value))
120 ->will($this->returnValue($action));
121
122 $mock->set_url_map($url_map);
123 $mock->RouteRequest($mock->request());
124 }
125
126 public function testRouteRequestInvalid()
127 {
128 $globals = array();
129 $mock = $this->ConfigureMock(array('Stop'), $globals);
130
131 $mock->request()->url = 'another/action';
132
133 $mock->expects($this->once())
134 ->method('Stop');
135
136 $url_map = $this->getMock('hoplite\http\UrlMap', array(), array($mock));
137 $url_map->expects($this->once())
138 ->method('Evaluate')
139 ->with($this->equalTo($mock->request()));
140
141 $mock->set_url_map($url_map);
142 $mock->RouteRequest($mock->request());
143 $this->assertEquals(http\ResponseCode::NOT_FOUND, $mock->response()->response_code);
144 }
145
146 public function testStop()
147 {
148 $globals = array();
149 $mock = $this->ConfigureMock(array('_Exit'), $globals);
150
151 $mock->expects($this->once())
152 ->method('_Exit');
153
154 $output_filter = $this->getMock('hoplite\http\OutputFilter', array(), array($mock));
155 $output_filter->expects($this->once())
156 ->method('FilterOutput')
157 ->with($this->isInstanceOf('hoplite\http\Request'),
158 $this->isInstanceOf('hoplite\http\Response'));
159
160 $mock->set_output_filter($output_filter);
161 $mock->Stop();
162 }
163
164 public function testStopWithRedirect()
165 {
166 $globals = array();
167 $mock = $this->ConfigureMock(array('_Exit'), $globals);
168 $mock->expects($this->once())
169 ->method('_Exit');
170
171 $output_filter = $this->getMock('hoplite\http\OutputFilter', array(), array($mock));
172 $output_filter->expects($this->once())
173 ->method('FilterOutput')
174 ->with($this->isInstanceOf('hoplite\http\Request'),
175 $this->isInstanceOf('hoplite\http\Response'));
176
177 $mock->set_output_filter($output_filter);
178
179 $mock->StopWithRedirect('/foo/bar');
180
181 $this->assertEquals('/foo/bar', $mock->response()->headers['Location']);
182 $this->assertEquals(http\ResponseCode::FOUND, $mock->response()->response_code);
183 }
184
185 public function testAbsolutify()
186 {
187 $globals = array(
188 '_SERVER' => array(
189 'HTTP_HOST' => 'www.bluestatic.org',
190 'REQUEST_URI' => '/hoplite/webapp/test/path',
191 'PATH_INFO' => '/test/path',
192 'SERVER_PORT' => 80,
193 ),
194 );
195 $mock = new \hoplite\http\RootController($globals);
196
197 $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
198 $this->assertEquals($mock->MakeURL('/', TRUE), 'http://www.bluestatic.org/hoplite/webapp/');
199
200 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
201 $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'http://www.bluestatic.org/hoplite/webapp/path/3');
202
203 $globals['_SERVER']['HTTPS'] = 'on';
204 $globals['_SERVER']['SERVER_PORT'] = 443;
205 $mock = new \hoplite\http\RootController($globals);
206 $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
207 $this->assertEquals($mock->MakeURL('/', TRUE), 'https://www.bluestatic.org/hoplite/webapp/');
208
209 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
210 $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'https://www.bluestatic.org/hoplite/webapp/path/3');
211
212 $globals['_SERVER']['SERVER_PORT'] = 8080;
213 $mock = new \hoplite\http\RootController($globals);
214 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
215 $this->assertEquals($mock->MakeURL('/', TRUE), 'https://www.bluestatic.org:8080/hoplite/webapp/');
216 $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'https://www.bluestatic.org:8080/hoplite/webapp/path/3');
217 }
218
219 public function testAbsolutifyRoot()
220 {
221 $globals = array(
222 '_SERVER' => array(
223 'HTTP_HOST' => 'www.bluestatic.org',
224 'REQUEST_URI' => '/hoplite/webapp/',
225 'PATH_INFO' => NULL,
226 'SERVER_PORT' => 80,
227 ),
228 );
229 $mock = new \hoplite\http\RootController($globals);
230
231 $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
232 $this->assertEquals($mock->MakeURL('/', TRUE), 'http://www.bluestatic.org/hoplite/webapp/');
233
234 $globals['_SERVER']['HTTPS'] = 'on';
235 $globals['_SERVER']['SERVER_PORT'] = 443;
236 $mock = new \hoplite\http\RootController($globals);
237 $this->assertEquals($mock->MakeURL('/'), '/hoplite/webapp/');
238 $this->assertEquals($mock->MakeURL('/', TRUE), 'https://www.bluestatic.org/hoplite/webapp/');
239
240 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
241 $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'https://www.bluestatic.org/hoplite/webapp/path/3');
242
243 $globals['_SERVER']['SERVER_PORT'] = 8080;
244 $mock = new \hoplite\http\RootController($globals);
245 $this->assertEquals($mock->MakeURL('/path/2'), '/hoplite/webapp/path/2');
246 $this->assertEquals($mock->MakeURL('/', TRUE), 'https://www.bluestatic.org:8080/hoplite/webapp/');
247 $this->assertEquals($mock->MakeURL('/path/3', TRUE), 'https://www.bluestatic.org:8080/hoplite/webapp/path/3');
248 }
249 }