Skip to content

Commit 91c4d35

Browse files
committed
Refactore new methods blacklisters and add documentation
1 parent 8e87c8f commit 91c4d35

13 files changed

+141
-158
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [BC BREAK] Remove SeoManagerInterface
66
* Add blacklist behaviour to avoid useless fetcher runs
7+
* Add blacklisters checking for XHR or HTTP methods
78

89
## 0.4.0 (2017-09-12)
910

doc/symfony/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Note:
111111
### Blacklist
112112

113113
You can blacklist some request/response to avoid fetcher to run (f.e. on non
114-
2xx response, on your admin, etc).
114+
2xx response, on your admin, on XHR calls, etc).
115115

116116
There is some built-in blacklister that you can enable/configure via the config
117117
`blacklist`:

doc/symfony/available-blacklisters.md

+30
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
|---|---|---|
55
| not_2xx | Blacklist if the response status code is not 2xx | None |
66
| path | Blacklist if the request path matches the pattern | - "pattern": mandatory, the regex pattern to match |
7+
| not_method | Blacklist if the request HTTP method is in the accepted ones | - "method": mandatory, the HTTP method(s) to accept |
8+
| xml_http | Blacklist if the request is made through XmlHttpRequest | None |
79

810
## not_2xx
911

@@ -36,3 +38,31 @@ seo_override:
3638
type: path
3739
pattern: '^/(admin|account)'
3840
```
41+
42+
## not_method
43+
44+
The `not_method` blacklister refuses request whose method is not in the
45+
configured `method` option. This option can be a single HTTP method or an array
46+
of methods:
47+
48+
```yaml
49+
seo_override:
50+
blacklist:
51+
-
52+
type: not_method
53+
method: [GET, POST]
54+
# if only one method should be accepted
55+
# method: GET
56+
```
57+
58+
## xml_http
59+
60+
The `xml_http` blacklister refuses request which have been made through Ajax
61+
(XmlHttpRequest). This blacklister does not have any option so the short YAML
62+
syntax can be used:
63+
64+
```yaml
65+
seo_override:
66+
blacklist:
67+
- xml_http
68+
```

src/Bridge/Symfony/Blacklister/GetMethodBlacklister.php src/Bridge/Symfony/Blacklister/NotMethodBlacklister.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,27 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717

18-
/**
19-
* Class GetBlacklister.
20-
*/
21-
class GetMethodBlacklister implements Blacklister
18+
class NotMethodBlacklister implements Blacklister
2219
{
20+
/** @var string[] */
21+
private $methods;
22+
2323
/**
24-
* @param Request $request
25-
* @param Response $response
26-
*
27-
* @return bool
24+
* @param string|string[] $method
2825
*/
26+
public function __construct($method)
27+
{
28+
$this->methods = (array) $method;
29+
}
30+
2931
public function isBlacklisted(Request $request, Response $response): bool
3032
{
31-
return $request->isMethod('get');
33+
foreach ($this->methods as $method) {
34+
if ($request->isMethod($method)) {
35+
return false;
36+
}
37+
}
38+
39+
return true;
3240
}
3341
}

src/Bridge/Symfony/Blacklister/PostMethodBlacklister.php

-33
This file was deleted.

src/Bridge/Symfony/Blacklister/XmlHttpBlacklister.php

-9
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,8 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717

18-
/**
19-
* Class XmlHttpBlacklister.
20-
*/
2118
class XmlHttpBlacklister implements Blacklister
2219
{
23-
/**
24-
* @param Request $request
25-
* @param Response $response
26-
*
27-
* @return bool
28-
*/
2920
public function isBlacklisted(Request $request, Response $response): bool
3021
{
3122
return $request->isXmlHttpRequest();

src/Bridge/Symfony/Resources/config/services.yml

+5-9
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,13 @@ services:
5252
tags:
5353
- { name: seo_override.blacklister, alias: not_2xx }
5454

55-
seo_override.blacklister.get_method:
56-
class: Joli\SeoOverride\Bridge\Symfony\Blacklister\GetMethodBlacklister
57-
public: false
58-
tags:
59-
- { name: seo_override.blacklister, alias: get_method }
60-
61-
seo_override.blacklister.post_method:
62-
class: Joli\SeoOverride\Bridge\Symfony\Blacklister\PostMethodBlacklister
55+
seo_override.blacklister.not_method:
56+
class: Joli\SeoOverride\Bridge\Symfony\Blacklister\NotMethodBlacklister
6357
public: false
58+
arguments:
59+
- '' # Argument configured in the DIC extension
6460
tags:
65-
- { name: seo_override.blacklister, alias: post_method }
61+
- { name: seo_override.blacklister, alias: not_method, required_options: 'method' }
6662

6763
seo_override.blacklister.xml_http:
6864
class: Joli\SeoOverride\Bridge\Symfony\Blacklister\XmlHttpBlacklister

tests/Functional/Fixtures/symfony/app/config/config.yml

+2
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ seo_override:
5252
blacklist:
5353
- not_2xx
5454
- { type: path, pattern: '^/admin' }
55+
- { type: not_method, method: ['GET', 'POST'] }
56+
- xml_http

tests/Functional/SymfonyTest.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,34 @@ public function test_it_does_not_override_seo_when_request_path_does_not_match()
186186
$this->assertSame($expected, $response->getContent());
187187
}
188188

189+
public function test_it_does_not_override_seo_when_request_use_not_allowed_action()
190+
{
191+
$response = $this->call('/', 'localhost', 'PUT');
192+
193+
$this->assertSame(200, $response->getStatusCode());
194+
$this->assertSame(self::NOT_OVERRIDDEN_HOMEPAGE_CONTENT, $response->getContent());
195+
}
196+
197+
public function test_it_does_not_override_seo_when_request_is_xhr()
198+
{
199+
$response = $this->call('/', 'localhost', 'GET', [
200+
'X-Requested-With' => 'XMLHttpRequest',
201+
]);
202+
203+
$this->assertSame(200, $response->getStatusCode());
204+
$this->assertSame(self::NOT_OVERRIDDEN_HOMEPAGE_CONTENT, $response->getContent());
205+
}
206+
189207
protected static function getKernelClass()
190208
{
191209
return AppKernel::class;
192210
}
193211

194-
private function call(string $uri, string $host)
212+
private function call(string $uri, string $host, string $method = 'GET', array $server = [])
195213
{
196-
$request = Request::create($uri, 'GET', [], [], [], [
197-
'HTTP_HOST' => $host,
198-
]);
214+
$server['HTTP_HOST'] = $host;
215+
216+
$request = Request::create($uri, $method, [], [], [], $server);
199217

200218
return self::$kernel->handle($request);
201219
}

tests/Unit/Bridge/Symfony/Blacklister/GetMethodBlackListerTest.php

-46
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SeoOverride project.
5+
*
6+
* (c) JoliCode <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Joli\SeoOverride\Tests\Unit\Bridge\Symfony\Blacklister;
13+
14+
use Joli\SeoOverride\Bridge\Symfony\Blacklister\NotMethodBlacklister;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\Response;
18+
19+
class NotMethodBlacklisterTest extends TestCase
20+
{
21+
/** @var NotMethodBlacklisterTest */
22+
private $blacklister;
23+
24+
public function setUp()
25+
{
26+
parent::setUp();
27+
}
28+
29+
public function test_it_blacklists_not_accepted_method_requests()
30+
{
31+
$request = new Request();
32+
$request->setMethod('put');
33+
34+
$blacklister = new NotMethodBlacklister(['GET', 'POST']);
35+
36+
self::assertTrue($blacklister->isBlacklisted($request, new Response()));
37+
}
38+
39+
public function test_it_does_not_blacklist_accepted_method_requests()
40+
{
41+
$request = new Request();
42+
$request->setMethod('get');
43+
44+
$blacklister = new NotMethodBlacklister('GET');
45+
46+
self::assertFalse($blacklister->isBlacklisted($request, new Response()));
47+
48+
$request = new Request();
49+
$request->setMethod('get');
50+
51+
$blacklister = new NotMethodBlacklister(['GET', 'POST']);
52+
53+
self::assertFalse($blacklister->isBlacklisted($request, new Response()));
54+
55+
$request = new Request();
56+
$request->setMethod('POST');
57+
58+
$blacklister = new NotMethodBlacklister(['GET', 'POST']);
59+
60+
self::assertFalse($blacklister->isBlacklisted($request, new Response()));
61+
}
62+
}

tests/Unit/Bridge/Symfony/Blacklister/PostMethodBlacklisterTest.php

-46
This file was deleted.

tests/Unit/Bridge/Symfony/Blacklister/XmlHttpBlacklisterTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function test_it_blacklists_xml_http_requests()
3636
self::assertTrue($this->blacklister->isBlacklisted($request, new Response()));
3737
}
3838

39-
public function test_it_does_not_blacklist_xml_http_requests()
39+
public function test_it_does_not_blacklist_not_xml_http_requests()
4040
{
4141
$request = new Request();
4242

0 commit comments

Comments
 (0)