Skip to content

Commit 3819e92

Browse files
committed
Force uppercase on methods/verbs.
1 parent 5eea90e commit 3819e92

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/Modes/RouterChunkedMode.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class RouterChunkedMode extends Router
4444
/** @inheritdoc */
4545
public function find(string $method, string $path): ?Action
4646
{
47+
$method = strtoupper($method);
4748
$route = "{$method} {$path}";
4849

4950
foreach ($this->patterns as [$pattern, $rules]) {

src/Modes/RouterRegexMode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class RouterRegexMode extends Router
2525
/** @inheritdoc */
2626
public function find(string $method, string $path): ?Action
2727
{
28+
$method = strtoupper($method);
29+
2830
foreach ($this->routes as $rule => $target) {
2931
$pattern = $this->expandRule($rule);
3032

src/Modes/RouterSingleMode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class RouterSingleMode extends Router
4949
/** @inheritdoc */
5050
public function find(string $method, string $path): ?Action
5151
{
52+
$method = strtoupper($method);
53+
5254
foreach ($this->patterns as $pattern => [$rule, $target]) {
5355
$matches = [];
5456
if (!preg_match($pattern, "{$method} {$path}", $matches)) continue;

tests/RouterTestCase.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,47 @@ public function testMultiMethod()
6565
}
6666

6767

68+
public function testCaseInsensitive()
69+
{
70+
$router = Router::create($this->router->config);
71+
$router->config->case_insensitive = false;
72+
$router->load($this->routes);
73+
74+
// Good.
75+
$action = $router->find('GET', '/get/123');
76+
$this->assertNotNull($action);
77+
$this->assertEquals('get route', $action->target);
78+
79+
// Bad.
80+
$action = $router->find('GET', '/GET/123');
81+
$this->assertNull($action);
82+
83+
// Methods are not case sensitive.
84+
$action = $router->find('get', '/get/123');
85+
$this->assertNotNull($action);
86+
$this->assertEquals('get route', $action->target);
87+
88+
$router = Router::create($router->config);
89+
$router->config->case_insensitive = true;
90+
$router->load($this->routes);
91+
92+
// Still good.
93+
$action = $router->find('GET', '/get/123');
94+
$this->assertNotNull($action);
95+
$this->assertEquals('get route', $action->target);
96+
97+
// Now this works.
98+
$action = $router->find('GET', '/GET/123');
99+
$this->assertNotNull($action);
100+
$this->assertEquals('get route', $action->target);
101+
102+
// Also still good.
103+
$action = $router->find('get', '/GeT/123');
104+
$this->assertNotNull($action);
105+
$this->assertEquals('get route', $action->target);
106+
}
107+
108+
68109
public function testVariables()
69110
{
70111
$action = $this->router->find('GET', '/abc/12345');

0 commit comments

Comments
 (0)