1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Middlewares\OriginFetchers; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use TheCodingMachine\Middlewares\CsrfHeaderCheckMiddlewareException; |
7
|
|
|
use Zend\Diactoros\ServerRequest; |
8
|
|
|
|
9
|
|
|
class HostHeaderTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testFetchHost() |
12
|
|
|
{ |
13
|
|
|
$request = new ServerRequest([], [], "http://alice.com:8080/hello", "Post"); |
14
|
|
|
|
15
|
|
|
$hostHeader = new HostHeader(); |
16
|
|
|
|
17
|
|
|
$hosts = $hostHeader($request); |
18
|
|
|
|
19
|
|
|
$this->assertSame(['alice.com'], $hosts); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
View Code Duplication |
public function testForwardedHostIgnored() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
$request = new ServerRequest([], [], "http://alice.com:8080/hello", "Post"); |
25
|
|
|
$request = $request->withHeader('X-Forwarded-Host', 'eve.com'); |
26
|
|
|
|
27
|
|
|
$hostHeader = new HostHeader(); |
28
|
|
|
|
29
|
|
|
$hosts = $hostHeader($request); |
30
|
|
|
|
31
|
|
|
$this->assertSame(['alice.com'], $hosts); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testMultipleHostHeaders() |
35
|
|
|
{ |
36
|
|
|
$request = new ServerRequest([], [], "http://alice.com:8080/hello", "Post"); |
37
|
|
|
$request = $request->withAddedHeader('Host', 'eve.com'); |
38
|
|
|
|
39
|
|
|
$hostHeader = new HostHeader(); |
40
|
|
|
|
41
|
|
|
$this->expectException(CsrfHeaderCheckMiddlewareException::class); |
42
|
|
|
$this->expectExceptionMessage("Unexpected request: more than one HOST header sent."); |
43
|
|
|
$hostHeader($request); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.