-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathConfigurationTest.php
More file actions
181 lines (152 loc) · 5.15 KB
/
Copy pathConfigurationTest.php
File metadata and controls
181 lines (152 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
namespace Noxlogic\RateLimitBundle\Tests\DependencyInjection;
use Noxlogic\RateLimitBundle\DependencyInjection\Configuration;
use Noxlogic\RateLimitBundle\Tests\WebTestCase;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
class ConfigurationTest extends WebTestCase
{
private Processor $processor;
public function setUp():void
{
$this->processor = new Processor();
}
private function getConfigs(array $configArray): array
{
$configuration = new Configuration();
return $this->processor->processConfiguration($configuration, array($configArray));
}
public function testUnconfiguredConfiguration(): void
{
$configuration = $this->getConfigs([]);
$this->assertSame([
'enabled' => true,
'storage_engine' => 'redis',
'redis_client' => 'default_client',
'redis_service' => null,
'php_redis_service' => null,
'memcache_client' => 'default',
'memcache_service' => null,
'doctrine_provider' => null,
'doctrine_service' => null,
'simple_cache_service' => null,
'cache_service' => null,
'rate_response_code' => 429,
'rate_response_exception' => null,
'rate_response_message' => 'You exceeded the rate limit',
'display_headers' => true,
'headers' => [
'limit' => 'X-RateLimit-Limit',
'remaining' => 'X-RateLimit-Remaining',
'reset' => 'X-RateLimit-Reset',
],
'path_limits' => [],
'fos_oauth_key_listener' => true
], $configuration);
}
public function testDisabledConfiguration(): void
{
$configuration = $this->getConfigs(array('enabled' => false));
$this->assertArrayHasKey('enabled', $configuration);
$this->assertFalse($configuration['enabled']);
}
public function testPathLimitConfiguration(): void
{
$pathLimits = array(
'api' => array(
'path' => 'api/',
'methods' => array('GET'),
'limit' => 100,
'period' => 60
)
);
$configuration = $this->getConfigs(array(
'path_limits' => $pathLimits
));
$this->assertArrayHasKey('path_limits', $configuration);
$this->assertEquals($pathLimits, $configuration['path_limits']);
}
public function testMultiplePathLimitConfiguration(): void
{
$pathLimits = array(
'api' => array(
'path' => 'api/',
'methods' => array('GET', 'POST'),
'limit' => 200,
'period' => 10
),
'api2' => array(
'path' => 'api2/',
'methods' => array('*'),
'limit' => 1000,
'period' => 15
)
);
$configuration = $this->getConfigs(array(
'path_limits' => $pathLimits
));
$this->assertArrayHasKey('path_limits', $configuration);
$this->assertEquals($pathLimits, $configuration['path_limits']);
}
public function testDefaultPathLimitMethods(): void
{
$pathLimits = array(
'api' => array(
'path' => 'api/',
'methods' => array('GET', 'POST'),
'limit' => 200,
'period' => 10
),
'api2' => array(
'path' => 'api2/',
'limit' => 1000,
'period' => 15
)
);
$configuration = $this->getConfigs(array(
'path_limits' => $pathLimits
));
$pathLimits['api2']['methods'] = array('*');
$this->assertArrayHasKey('path_limits', $configuration);
$this->assertEquals($pathLimits, $configuration['path_limits']);
}
public function testMustBeBasedOnExceptionClass(): void
{
$this->expectException(InvalidConfigurationException::class);
$this->getConfigs(array('rate_response_exception' => '\StdClass'));
}
/**
* @testWith [""]
* [null]
*/
public function testEmptyPathIsNotAllowed(mixed $path): void
{
$pathLimits = [
'api' => [
'path' => $path,
'methods' => ['GET'],
'limit' => 200,
'period' => 10
],
];
$this->expectException(InvalidConfigurationException::class);
$this->getConfigs([
'path_limits' => $pathLimits
]);
}
/**
*
*/
public function testMustBeBasedOnExceptionClass2(): void
{
$this->getConfigs(array('rate_response_exception' => '\InvalidArgumentException'));
# no exception triggered is ok.
$this->expectNotToPerformAssertions();
}
public function testMustBeBasedOnExceptionOrNull(): void
{
$this->getConfigs(array('rate_response_exception' => null));
# no exception triggered is ok.
$this->expectNotToPerformAssertions();
}
}