-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRouterTest.php
More file actions
160 lines (136 loc) · 4.92 KB
/
RouterTest.php
File metadata and controls
160 lines (136 loc) · 4.92 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
<?php declare(strict_types=1);
namespace Bref\DevServer\Test;
use Bref\DevServer\Router;
use Nyholm\Psr7\ServerRequest;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\Yaml\Yaml;
class RouterTest extends TestCase
{
public function test wildcard(): void
{
$router = new Router([
'GET /' => 'home',
'*' => 'wildcard',
]);
self::assertEquals('home', $router->match($this->request('GET', '/'))[0]);
self::assertEquals('wildcard', $router->match($this->request('GET', '/abc'))[0]);
}
public function test routing simple paths(): void
{
$router = new Router([
'GET /' => 'home',
'POST /' => 'post home',
'GET /abc' => 'abc',
]);
self::assertEquals('home', $router->match($this->request('GET', '/'))[0]);
self::assertEquals('post home', $router->match($this->request('POST', '/'))[0]);
self::assertEquals('abc', $router->match($this->request('GET', '/abc'))[0]);
}
public function test routing path parameters(): void
{
$router = new Router([
'GET /' => 'home',
'GET /{root}' => 'home with param',
'GET /{root}/abc' => 'abc',
'GET /{root}/{sub}' => 'def',
]);
self::assertEquals('home', $router->match($this->request('GET', '/'))[0]);
self::assertEquals('home with param', $router->match($this->request('GET', '/abc'))[0]);
self::assertEquals('abc', $router->match($this->request('GET', '/abc/abc'))[0]);
self::assertEquals('def', $router->match($this->request('GET', '/abc/def'))[0]);
}
/**
* @test
*/
public function test_routing_expanded_config(): void
{
$config = <<<YAML
functions:
api:
handler: api.php
events:
- httpApi:
method: '*'
path: '*'
YAML;
$config = Yaml::parse($config);
$router = Router::fromServerlessConfig($config);
self::assertSame('api.php', $router->match($this->request('GET', '/'))[0]);
self::assertSame('api.php', $router->match($this->request('POST', '/'))[0]);
self::assertSame('api.php', $router->match($this->request('GET', '/tests/1'))[0]);
self::assertSame('api.php', $router->match($this->request('PUT', '/tests/1'))[0]);
}
/**
* @test
*/
public function test_routing_with_api_gateway_v1_wildcard_config(): void
{
$config = <<<YAML
functions:
api:
handler: api.php
events:
- http:
method: '*'
path: '*'
YAML;
$config = Yaml::parse($config);
$router = Router::fromServerlessConfig($config);
self::assertSame('api.php', $router->match($this->request('GET', '/'))[0]);
self::assertSame('api.php', $router->match($this->request('POST', '/'))[0]);
self::assertSame('api.php', $router->match($this->request('GET', '/tests/1'))[0]);
self::assertSame('api.php', $router->match($this->request('PUT', '/tests/1'))[0]);
}
/**
* @test
*/
public function test_routing_with_api_gateway_v1_specific_method_config(): void
{
$config = <<<YAML
functions:
hello:
handler: hello.php
events:
- http:
method: 'GET'
path: '/hello'
YAML;
$config = Yaml::parse($config);
$router = Router::fromServerlessConfig($config);
self::assertSame('hello.php', $router->match($this->request('GET', '/hello'))[0]);
}
/**
* @test
*/
public function test_routing_with_api_gateway_v1_file_config(): void
{
$config = <<<YAML
functions:
- \${file(./tests/function/hello/v1serverless.yml)}
YAML;
$config = Yaml::parse($config);
$router = Router::fromServerlessConfig($config);
self::assertSame('hello.php', $router->match($this->request('GET', '/hello'))[0]);
}
/**
* @test
*/
public function test_routing_with_api_gateway_v2_file_config(): void
{
$config = <<<YAML
functions:
- \${file(./tests/function/hello/v2serverless.yml)}
YAML;
$config = Yaml::parse($config);
$router = Router::fromServerlessConfig($config);
self::assertSame('hello.php', $router->match($this->request('GET', '/'))[0]);
self::assertSame('hello.php', $router->match($this->request('POST', '/'))[0]);
self::assertSame('hello.php', $router->match($this->request('GET', '/tests/1'))[0]);
self::assertSame('hello.php', $router->match($this->request('PUT', '/tests/1'))[0]);
}
private function request(string $method, string $path): ServerRequestInterface
{
return new ServerRequest($method, $path);
}
}