Skip to content

Commit 31a5aa5

Browse files
committed
Merge branch 'tworzenieweb-2.x.x'
2 parents e2ae327 + 4e99681 commit 31a5aa5

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Features
2323

2424
- [x] Enable/disable force https.
2525
- [x] Force Https to All routes.
26+
- [x] Force Https to All routes except exclusion list.
2627
- [x] Force Https to specific routes only.
2728
- [x] Keep headers, request method, and request body.
2829
- [x] Enable/disable HTTP Strict Transport Security Header and set its value.
@@ -79,6 +80,11 @@ return [
7980
'checkout',
8081
'payment'
8182
],
83+
'exclude_specific_routes' => [
84+
// a lists of specific routes to not be https
85+
// only works if previous config 'force_all_routes' => true
86+
'non-https-route',
87+
],
8288
// set HTTP Strict Transport Security Header
8389
'strict_transport_security' => [
8490
// set to false to disable it

config/force-https-module.local.php.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ return [
88
// a lists of specific routes to be https
99
// only works if previous config 'force_all_routes' => false
1010
],
11+
'exclude_specific_routes' => [
12+
// a lists of specific routes to not be https
13+
// only works if previous config 'force_all_routes' => true
14+
],
1115
// set HTTP Strict Transport Security Header
1216
'strict_transport_security' => [
1317
'enable' => true, // set to false to disable it

config/mezzio-force-https-module.local.php.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ return [
1010
// a lists of specific routes to be https
1111
// only works if previous config 'force_all_routes' => false
1212
],
13+
'exclude_specific_routes' => [
14+
// a lists of specific routes to not be https
15+
// only works if previous config 'force_all_routes' => true
16+
],
1317
// set HTTP Strict Transport Security Header
1418
'strict_transport_security' => [
1519
'enable' => true, // set to false to disable it

spec/Listener/ForceHttpsSpec.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,62 @@
205205

206206
});
207207

208+
it('not redirect if force_all_routes is true and route name in exclude_specific_routes config', function () {
209+
210+
$listener = new ForceHttps([
211+
'enable' => true,
212+
'force_all_routes' => true,
213+
'exclude_specific_routes' => [
214+
'checkout'
215+
],
216+
'force_specific_routes' => [],
217+
]);
218+
219+
allow($this->mvcEvent)->toReceive('getRequest')->andReturn($this->request);
220+
allow($this->request)->toReceive('getUri')->andReturn($this->uri);
221+
allow($this->uri)->toReceive('getScheme')->andReturn('http');
222+
allow($this->mvcEvent)->toReceive('getRouteMatch')->andReturn($this->routeMatch);
223+
allow($this->routeMatch)->toReceive('getMatchedRouteName')->andReturn('checkout');
224+
allow($this->uri)->toReceive('toString')->andReturn('http://example.com/about');
225+
allow($this->mvcEvent)->toReceive('getResponse')->andReturn($this->response);
226+
allow($this->response)->toReceive('send');
227+
228+
$listener->forceHttpsScheme($this->mvcEvent);
229+
expect($this->mvcEvent)->toReceive('getResponse');
230+
});
231+
232+
233+
it('redirect if force_all_routes is true and route name not in exclude_specific_routes config', function () {
234+
235+
$listener = new ForceHttps([
236+
'enable' => true,
237+
'force_all_routes' => true,
238+
'exclude_specific_routes' => [
239+
'sale'
240+
],
241+
'force_specific_routes' => [],
242+
]);
243+
244+
allow($this->mvcEvent)->toReceive('getRequest')->andReturn($this->request);
245+
allow($this->request)->toReceive('getUri')->andReturn($this->uri);
246+
allow($this->uri)->toReceive('getScheme')->andReturn('http');
247+
allow($this->mvcEvent)->toReceive('getRouteMatch')->andReturn($this->routeMatch);
248+
allow($this->routeMatch)->toReceive('getMatchedRouteName')->andReturn('checkout');
249+
allow($this->uri)->toReceive('setScheme')->with('https')->andReturn($this->uri);
250+
allow($this->uri)->toReceive('toString')->andReturn('https://example.com/about');
251+
allow($this->mvcEvent)->toReceive('getResponse')->andReturn($this->response);
252+
allow($this->response)->toReceive('setStatusCode')->with(308)->andReturn($this->response);
253+
allow($this->response)->toReceive('getHeaders', 'addHeaderLine')->with('Location', 'https://example.com/about');
254+
allow($this->response)->toReceive('send');
255+
256+
$closure = function () use ($listener) {
257+
$listener->forceHttpsScheme($this->mvcEvent);
258+
};
259+
expect($closure)->toThrow(new QuitException('Exit statement occurred', 0));
260+
261+
expect($this->mvcEvent)->toReceive('getResponse');
262+
});
263+
208264
it('redirect if force_all_routes is true', function () {
209265

210266
$listener = new ForceHttps([

src/HttpsTrait.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@ private function isGoingToBeForcedToHttps($match = null): bool
3636
return $this->config['allow_404'] ?? false;
3737
}
3838

39+
Assert::notNull($match);
40+
$matchedRouteName = $match->getMatchedRouteName();
41+
3942
if ($this->config['force_all_routes']) {
43+
if (! empty($this->config['exclude_specific_routes'])
44+
&& \in_array($matchedRouteName, $this->config['exclude_specific_routes'])) {
45+
return false;
46+
}
47+
4048
return true;
4149
}
4250

43-
Assert::notNull($match);
44-
if (! in_array($match->getMatchedRouteName(), $this->config['force_specific_routes'])) {
51+
if (! \in_array($matchedRouteName, $this->config['force_specific_routes'])) {
4552
return false;
4653
}
4754

0 commit comments

Comments
 (0)