|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace tests\MediaMonks\RestApi\Request; |
|
4
|
|
|
|
|
5
|
|
|
use MediaMonks\RestApi\Request\RequestMatcher; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
8
|
|
|
|
|
9
|
|
|
class RequestMatcherTest extends \PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
public function testMatchesEmptyWhitelist() |
|
13
|
|
|
{ |
|
14
|
|
|
$matcher = new RequestMatcher([]); |
|
15
|
|
|
foreach ([ |
|
16
|
|
|
['path' => '/foo', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => false], |
|
17
|
|
|
['path' => '/bar', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => false], |
|
18
|
|
|
['path' => '/foo', 'type' => HttpKernelInterface::SUB_REQUEST, 'result' => false], |
|
19
|
|
|
['path' => '/bar', 'type' => HttpKernelInterface::SUB_REQUEST, 'result' => false], |
|
20
|
|
|
] as $test |
|
21
|
|
|
) { |
|
22
|
|
|
$this->assertEquals($test['result'], |
|
23
|
|
|
$matcher->matches($this->getRequestFromPath($test['path']), $test['type'])); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testMatchesWhitelist() |
|
28
|
|
|
{ |
|
29
|
|
|
$matcher = new RequestMatcher([ |
|
30
|
|
|
'~^/api$~', |
|
31
|
|
|
'~^/api/~' |
|
32
|
|
|
]); |
|
33
|
|
|
foreach ([ |
|
34
|
|
|
['path' => '/foo', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => false], |
|
35
|
|
|
['path' => '/foo', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => false], |
|
36
|
|
|
['path' => '/fapi', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => false], |
|
37
|
|
|
['path' => '/api', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => true], |
|
38
|
|
|
['path' => '/api', 'type' => HttpKernelInterface::SUB_REQUEST, 'result' => false], |
|
39
|
|
|
['path' => '/api/', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => true], |
|
40
|
|
|
['path' => '/api/', 'type' => HttpKernelInterface::SUB_REQUEST, 'result' => false], |
|
41
|
|
|
['path' => '/api/foo', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => true], |
|
42
|
|
|
['path' => '/api/doc', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => true], |
|
43
|
|
|
] as $test |
|
44
|
|
|
) { |
|
45
|
|
|
$this->assertEquals($test['result'], |
|
46
|
|
|
$matcher->matches($this->getRequestFromPath($test['path']), $test['type'])); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testMatchesWhitelistBlacklist() |
|
51
|
|
|
{ |
|
52
|
|
|
$matcher = new RequestMatcher([ |
|
53
|
|
|
'~^/api$~', |
|
54
|
|
|
'~^/api/~' |
|
55
|
|
|
], [ |
|
56
|
|
|
'~^/api/doc~' |
|
57
|
|
|
]); |
|
58
|
|
|
foreach ([ |
|
59
|
|
|
['path' => '/foo', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => false], |
|
60
|
|
|
['path' => '/foo', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => false], |
|
61
|
|
|
['path' => '/fapi', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => false], |
|
62
|
|
|
['path' => '/api', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => true], |
|
63
|
|
|
['path' => '/api', 'type' => HttpKernelInterface::SUB_REQUEST, 'result' => false], |
|
64
|
|
|
['path' => '/api/', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => true], |
|
65
|
|
|
['path' => '/api/', 'type' => HttpKernelInterface::SUB_REQUEST, 'result' => false], |
|
66
|
|
|
['path' => '/api/foo', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => true], |
|
67
|
|
|
['path' => '/api/doc', 'type' => HttpKernelInterface::MASTER_REQUEST, 'result' => false], |
|
68
|
|
|
['path' => '/api/doc', 'type' => HttpKernelInterface::SUB_REQUEST, 'result' => false], |
|
69
|
|
|
] as $test |
|
70
|
|
|
) { |
|
71
|
|
|
$this->assertEquals($test['result'], |
|
72
|
|
|
$matcher->matches($this->getRequestFromPath($test['path']), $test['type'])); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testMatchedRequestIsMarkedAsMatched() |
|
77
|
|
|
{ |
|
78
|
|
|
$matcher = new RequestMatcher(['~^/api$~']); |
|
79
|
|
|
$request = $this->getRequestFromPath('/api'); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertEquals(true, $matcher->matches($request)); |
|
82
|
|
|
$this->assertTrue($request->attributes->has(RequestMatcher::ATTRIBUTE_MATCHED)); |
|
83
|
|
|
$this->assertEquals(true, $matcher->matches($request)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testNonMatchedRequestIsNotMarkedAsMatched() |
|
87
|
|
|
{ |
|
88
|
|
|
$matcher = new RequestMatcher(['~^/api$~']); |
|
89
|
|
|
$request = $this->getRequestFromPath('/'); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertEquals(false, $matcher->matches($request)); |
|
92
|
|
|
$this->assertFalse($request->attributes->has(RequestMatcher::ATTRIBUTE_MATCHED)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function testMatchedRequestIsNotMatchedTwice() |
|
96
|
|
|
{ |
|
97
|
|
|
$matcher = new RequestMatcher(['~^/api$~']); |
|
98
|
|
|
$request = $this->getRequestFromPath('/'); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertEquals(false, $matcher->matches($request)); |
|
101
|
|
|
$this->assertFalse($request->attributes->has(RequestMatcher::ATTRIBUTE_MATCHED)); |
|
102
|
|
|
$this->assertEquals(false, $matcher->matches($request)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testMatchesAlreadyMatched() |
|
106
|
|
|
{ |
|
107
|
|
|
$subject = new RequestMatcher(['~^/api$~']); |
|
108
|
|
|
$request = $this->getRequestFromPath('/api'); |
|
109
|
|
|
|
|
110
|
|
|
// First match, path 1 |
|
111
|
|
|
$this->assertTrue($subject->matches($request)); |
|
112
|
|
|
// Second match, shortcut path 2 |
|
113
|
|
|
$this->assertTrue($subject->matches($request)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $path |
|
118
|
|
|
* @return Request |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getRequestFromPath($path) |
|
121
|
|
|
{ |
|
122
|
|
|
return Request::create($path); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|