-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHTTPServicesTest.php
More file actions
142 lines (107 loc) · 3.84 KB
/
HTTPServicesTest.php
File metadata and controls
142 lines (107 loc) · 3.84 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
<?php
namespace Utopia\Tests;
use PHPUnit\Framework\TestCase;
use Utopia\DI\Container;
use Utopia\Http\Adapter\FPM\Request;
use Utopia\Http\Adapter\FPM\Server;
use Utopia\Http\Http;
class HttpServicesTest extends TestCase
{
protected ?string $method;
protected ?string $uri;
protected ?Http $http;
public function setUp(): void
{
Http::reset();
$platform = new TestPlatform();
$platform->init('http');
$server = new Server(new Container());
$this->http = new Http($server, 'UTC');
}
public function tearDown(): void
{
$this->http = null;
}
public function testRootAction()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/';
$request = new Request();
$response = new MockResponse();
\ob_start();
$this->http->run($request, $response);
$result = \ob_get_contents();
\ob_end_clean();
$this->assertEquals('Hello World!', $result);
}
public function testChunkedAction()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/chunked';
$request = new Request();
$response = new MockResponse();
\ob_start();
$this->http->run($request, $response);
$result = \ob_get_contents();
\ob_end_clean();
$this->assertEquals('Hello World!', $result);
}
public function testRedirectAction()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/redirect';
$request = new Request();
$response = new MockResponse();
$this->http->run($request, $response);
$this->assertEquals('/', $response->getHeaders()['Location']);
}
public function testHook()
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/';
$request = new Request();
$response = new MockResponse();
\ob_start();
$this->http->run($request, $response);
$result = \ob_get_contents();
\ob_end_clean();
$this->assertEquals('Hello World!', $result);
$this->assertEquals('init-called', $response->getHeaders()['x-init']);
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/chunked';
$request1 = new Request();
$response1 = new MockResponse();
\ob_start();
$this->http->run($request1, $response1);
$result = \ob_get_contents();
\ob_end_clean();
$this->assertEquals('Hello World!', $result);
$this->assertEquals('', ($response1->getHeaders()['x-init'] ?? ''));
}
public function testActionParamFieldsForwardedToRoute()
{
$routes = Http::getRoutes();
$route = null;
foreach ($routes as $method => $methodRoutes) {
foreach ($methodRoutes as $r) {
if ($r->getPath() === '/with-params') {
$route = $r;
break 2;
}
}
}
$this->assertNotNull($route, 'Route /with-params should be registered');
$params = $route->getParams();
// Verify all Action::param() fields are forwarded to the Route
$actionParamKeys = ['default', 'validator', 'description', 'optional', 'injections', 'skipValidation', 'deprecated', 'example'];
foreach ($params as $name => $param) {
foreach ($actionParamKeys as $key) {
$this->assertArrayHasKey($key, $param, "Param '{$name}' is missing '{$key}' on the Route. Platform must forward all Action param fields.");
}
}
$this->assertEquals('John Doe', $params['name']['example']);
$this->assertFalse($params['name']['deprecated']);
$this->assertEquals('true', $params['active']['example']);
$this->assertTrue($params['active']['deprecated']);
}
}