|
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; |
|
50 | 49 | $listener->attach($this->eventManager); |
51 | 50 |
|
52 | 51 | expect($this->eventManager)->not->toReceive('attach')->with(MvcEvent::EVENT_ROUTE, [$listener, 'forceHttpsScheme']); |
| 52 | + expect($this->eventManager)->not->toReceive('attach')->with(MvcEvent::EVENT_DISPATCH_ERROR, [$listener, 'forceHttpsScheme'], 1000); |
53 | 53 |
|
54 | 54 | }); |
55 | 55 |
|
|
106 | 106 |
|
107 | 107 | }); |
108 | 108 |
|
| 109 | + it('not redirect if router not match', function () { |
| 110 | + |
| 111 | + $listener = new ForceHttps([ |
| 112 | + 'enable' => true, |
| 113 | + 'force_all_routes' => true, |
| 114 | + 'force_specific_routes' => [], |
| 115 | + ]); |
| 116 | + |
| 117 | + allow($this->mvcEvent)->toReceive('getRouteMatch')->andReturn(null); |
| 118 | + allow($this->routeMatch)->toReceive('getMatchedRouteName')->andReturn('about'); |
| 119 | + |
| 120 | + allow($this->mvcEvent)->toReceive('getRequest', 'getUri', 'getScheme')->andReturn('https'); |
| 121 | + allow($this->mvcEvent)->toReceive('getRequest', 'getUri', 'toString')->andReturn('https://www.example.com/about'); |
| 122 | + allow($this->mvcEvent)->toReceive('getResponse')->andReturn($this->response); |
| 123 | + expect($this->mvcEvent)->toReceive('getResponse'); |
| 124 | + |
| 125 | + $listener->forceHttpsScheme($this->mvcEvent); |
| 126 | + expect($this->response)->not->toReceive('getHeaders'); |
| 127 | + |
| 128 | + }); |
| 129 | + |
109 | 130 | }); |
110 | 131 |
|
111 | 132 | context('on current scheme is http', function () { |
|
129 | 150 | $listener->forceHttpsScheme($this->mvcEvent); |
130 | 151 |
|
131 | 152 | expect($this->mvcEvent)->toReceive('getResponse'); |
| 153 | + expect($this->response)->not->toReceive('send'); |
| 154 | + |
| 155 | + }); |
| 156 | + |
| 157 | + it('not redirect on router not match', function () { |
| 158 | + |
| 159 | + $listener = new ForceHttps([ |
| 160 | + 'enable' => true, |
| 161 | + ]); |
| 162 | + |
| 163 | + allow($this->mvcEvent)->toReceive('getRequest', 'getUri', 'getScheme')->andReturn('http'); |
| 164 | + allow($this->mvcEvent)->toReceive('getRouteMatch')->andReturn(null); |
| 165 | + allow($this->mvcEvent)->toReceive('getResponse')->andReturn($this->response); |
| 166 | + |
| 167 | + $listener->forceHttpsScheme($this->mvcEvent); |
| 168 | + |
| 169 | + expect($this->mvcEvent)->toReceive('getResponse'); |
| 170 | + expect($this->response)->not->toReceive('send'); |
132 | 171 |
|
133 | 172 | }); |
134 | 173 |
|
|
232 | 271 |
|
233 | 272 | }); |
234 | 273 |
|
| 274 | + |
| 275 | + it('redirect no router not match, but allow_404 is true', function () { |
| 276 | + |
| 277 | + $listener = new ForceHttps([ |
| 278 | + 'enable' => true, |
| 279 | + 'allow_404' => true, |
| 280 | + ]); |
| 281 | + |
| 282 | + allow($this->mvcEvent)->toReceive('getRequest')->andReturn($this->request); |
| 283 | + allow($this->request)->toReceive('getUri')->andReturn($this->uri); |
| 284 | + allow($this->uri)->toReceive('getScheme')->andReturn('http'); |
| 285 | + allow($this->mvcEvent)->toReceive('getRouteMatch')->andReturn(null); |
| 286 | + allow($this->uri)->toReceive('setScheme')->with('https')->andReturn($this->uri); |
| 287 | + allow($this->uri)->toReceive('toString')->andReturn('https://example.com/404'); |
| 288 | + allow($this->mvcEvent)->toReceive('getResponse')->andReturn($this->response); |
| 289 | + allow($this->response)->toReceive('setStatusCode')->with(308)->andReturn($this->response); |
| 290 | + allow($this->response)->toReceive('getHeaders', 'addHeaderLine')->with('Location', 'https://example.com/404'); |
| 291 | + allow($this->response)->toReceive('send'); |
| 292 | + |
| 293 | + $closure = function () use ($listener) { |
| 294 | + $listener->forceHttpsScheme($this->mvcEvent); |
| 295 | + }; |
| 296 | + expect($closure)->toThrow(new QuitException('Exit statement occurred', 0)); |
| 297 | + |
| 298 | + expect($this->mvcEvent)->toReceive('getResponse'); |
| 299 | + expect($this->response)->toReceive('getHeaders', 'addHeaderLine')->with('Location', 'https://example.com/404'); |
| 300 | + |
| 301 | + }); |
| 302 | + |
235 | 303 | it('redirect with www prefix with configurable "add_www_prefix" on force_all_routes', function () { |
236 | 304 |
|
237 | 305 | $listener = new ForceHttps([ |
|
0 commit comments