|
10 | 10 | use Zend\EventManager\EventManagerInterface; |
11 | 11 | use Zend\Http\PhpEnvironment\Request; |
12 | 12 | use Zend\Http\PhpEnvironment\Response; |
13 | | -use Zend\Mvc\Application; |
14 | 13 | use Zend\Mvc\MvcEvent; |
15 | 14 | use Zend\Router\RouteMatch; |
16 | 15 | use Zend\Uri\Uri; |
|
35 | 34 | $listener->attach($this->eventManager); |
36 | 35 |
|
37 | 36 | expect($this->eventManager)->not->toReceive('attach')->with(MvcEvent::EVENT_ROUTE, [$listener, 'forceHttpsScheme']); |
| 37 | + expect($this->eventManager)->not->toReceive('attach')->with(MvcEvent::EVENT_DISPATCH_ERROR, [$listener, 'forceHttpsScheme'], 1000); |
38 | 38 |
|
39 | 39 | }); |
40 | 40 |
|
|
50 | 50 | $listener->attach($this->eventManager); |
51 | 51 |
|
52 | 52 | expect($this->eventManager)->not->toReceive('attach')->with(MvcEvent::EVENT_ROUTE, [$listener, 'forceHttpsScheme']); |
| 53 | + expect($this->eventManager)->not->toReceive('attach')->with(MvcEvent::EVENT_DISPATCH_ERROR, [$listener, 'forceHttpsScheme'], 1000); |
53 | 54 |
|
54 | 55 | }); |
55 | 56 |
|
|
63 | 64 | ]); |
64 | 65 |
|
65 | 66 | allow($this->eventManager)->toReceive('attach')->with(MvcEvent::EVENT_ROUTE, [$listener, 'forceHttpsScheme']); |
| 67 | + allow($this->eventManager)->toReceive('attach')->with(MvcEvent::EVENT_DISPATCH_ERROR, [$listener, 'forceHttpsScheme'], 1000); |
66 | 68 | $listener->attach($this->eventManager); |
67 | 69 |
|
68 | 70 | expect($this->eventManager)->toReceive('attach')->with(MvcEvent::EVENT_ROUTE, [$listener, 'forceHttpsScheme']); |
| 71 | + expect($this->eventManager)->toReceive('attach')->with(MvcEvent::EVENT_DISPATCH_ERROR, [$listener, 'forceHttpsScheme'], 1000); |
69 | 72 |
|
70 | 73 | }); |
71 | 74 |
|
|
106 | 109 |
|
107 | 110 | }); |
108 | 111 |
|
| 112 | + it('not redirect if router not match', function () { |
| 113 | + |
| 114 | + $listener = new ForceHttps([ |
| 115 | + 'enable' => true, |
| 116 | + 'force_all_routes' => true, |
| 117 | + 'force_specific_routes' => [], |
| 118 | + ]); |
| 119 | + |
| 120 | + allow($this->mvcEvent)->toReceive('getRouteMatch')->andReturn(null); |
| 121 | + allow($this->routeMatch)->toReceive('getMatchedRouteName')->andReturn('about'); |
| 122 | + |
| 123 | + allow($this->mvcEvent)->toReceive('getRequest', 'getUri', 'getScheme')->andReturn('https'); |
| 124 | + allow($this->mvcEvent)->toReceive('getRequest', 'getUri', 'toString')->andReturn('https://www.example.com/about'); |
| 125 | + allow($this->mvcEvent)->toReceive('getResponse')->andReturn($this->response); |
| 126 | + expect($this->mvcEvent)->toReceive('getResponse'); |
| 127 | + |
| 128 | + $listener->forceHttpsScheme($this->mvcEvent); |
| 129 | + expect($this->response)->not->toReceive('getHeaders'); |
| 130 | + |
| 131 | + }); |
| 132 | + |
109 | 133 | }); |
110 | 134 |
|
111 | 135 | context('on current scheme is http', function () { |
|
129 | 153 | $listener->forceHttpsScheme($this->mvcEvent); |
130 | 154 |
|
131 | 155 | expect($this->mvcEvent)->toReceive('getResponse'); |
| 156 | + expect($this->response)->not->toReceive('send'); |
| 157 | + |
| 158 | + }); |
| 159 | + |
| 160 | + it('not redirect on router not match', function () { |
| 161 | + |
| 162 | + $listener = new ForceHttps([ |
| 163 | + 'enable' => true, |
| 164 | + ]); |
| 165 | + |
| 166 | + allow($this->mvcEvent)->toReceive('getRequest', 'getUri', 'getScheme')->andReturn('http'); |
| 167 | + allow($this->mvcEvent)->toReceive('getRouteMatch')->andReturn(null); |
| 168 | + allow($this->mvcEvent)->toReceive('getResponse')->andReturn($this->response); |
| 169 | + |
| 170 | + $listener->forceHttpsScheme($this->mvcEvent); |
| 171 | + |
| 172 | + expect($this->mvcEvent)->toReceive('getResponse'); |
| 173 | + expect($this->response)->not->toReceive('send'); |
132 | 174 |
|
133 | 175 | }); |
134 | 176 |
|
|
232 | 274 |
|
233 | 275 | }); |
234 | 276 |
|
| 277 | + |
| 278 | + it('redirect no router not match, but allow_404 is true', function () { |
| 279 | + |
| 280 | + $listener = new ForceHttps([ |
| 281 | + 'enable' => true, |
| 282 | + 'allow_404' => true, |
| 283 | + ]); |
| 284 | + |
| 285 | + allow($this->mvcEvent)->toReceive('getRequest')->andReturn($this->request); |
| 286 | + allow($this->request)->toReceive('getUri')->andReturn($this->uri); |
| 287 | + allow($this->uri)->toReceive('getScheme')->andReturn('http'); |
| 288 | + allow($this->mvcEvent)->toReceive('getRouteMatch')->andReturn(null); |
| 289 | + allow($this->uri)->toReceive('setScheme')->with('https')->andReturn($this->uri); |
| 290 | + allow($this->uri)->toReceive('toString')->andReturn('https://example.com/404'); |
| 291 | + allow($this->mvcEvent)->toReceive('getResponse')->andReturn($this->response); |
| 292 | + allow($this->response)->toReceive('setStatusCode')->with(308)->andReturn($this->response); |
| 293 | + allow($this->response)->toReceive('getHeaders', 'addHeaderLine')->with('Location', 'https://example.com/404'); |
| 294 | + allow($this->response)->toReceive('send'); |
| 295 | + |
| 296 | + $closure = function () use ($listener) { |
| 297 | + $listener->forceHttpsScheme($this->mvcEvent); |
| 298 | + }; |
| 299 | + expect($closure)->toThrow(new QuitException('Exit statement occurred', 0)); |
| 300 | + |
| 301 | + expect($this->mvcEvent)->toReceive('getResponse'); |
| 302 | + expect($this->response)->toReceive('getHeaders', 'addHeaderLine')->with('Location', 'https://example.com/404'); |
| 303 | + |
| 304 | + }); |
| 305 | + |
235 | 306 | it('redirect with www prefix with configurable "add_www_prefix" on force_all_routes', function () { |
236 | 307 |
|
237 | 308 | $listener = new ForceHttps([ |
|
0 commit comments