-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathResponseTest.php
More file actions
171 lines (141 loc) · 6.85 KB
/
Copy pathResponseTest.php
File metadata and controls
171 lines (141 loc) · 6.85 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
<?php
namespace Utopia\Tests;
use PHPUnit\Framework\TestCase;
use Tests\E2E\Client;
class ResponseTest extends TestCase
{
protected Client $client;
public function setUp(): void
{
$this->client = new Client();
}
public function testResponse()
{
$response = $this->client->call(Client::METHOD_GET, '/');
$this->assertEquals('Hello World!', $response['body']);
}
public function testResponseValue()
{
$response = $this->client->call(Client::METHOD_GET, '/value/123');
$this->assertEquals('123', $response['body']);
}
public function testChunkResponse()
{
$response = $this->client->call(Client::METHOD_GET, '/chunked');
$this->assertEquals('Hello World!', $response['body']);
}
public function testRedirect()
{
$response = $this->client->call(Client::METHOD_GET, '/redirect');
$this->assertEquals('Hello World!', $response['body']);
}
public function testFile()
{
$response = $this->client->call(Client::METHOD_GET, '/humans.txt');
$this->assertEquals(204, $response['headers']['status-code']);
}
public function testEarlyResponse()
{
// Ensure response from action is not recieved
$response = $this->client->call(Client::METHOD_GET, '/early-response');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEquals('Action response', $response['body']);
// 2nd request would catch if action from first ran
$response = $this->client->call(Client::METHOD_GET, '/early-response');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Init response. Actioned before: no', $response['body']);
}
public function testNullPathHandling()
{
// Test that malformed URLs default to root path
$response = $this->client->call(Client::METHOD_GET, '/');
$this->assertEquals('Hello World!', $response['body']);
$this->assertEquals(200, $response['headers']['status-code']);
}
public function testRootPathFallback()
{
// Test that when path parsing fails, it falls back to root
// This is tested by ensuring the root route works correctly
$response = $this->client->call(Client::METHOD_GET, '/');
$this->assertEquals('Hello World!', $response['body']);
$this->assertEquals(200, $response['headers']['status-code']);
}
public function testAliasWithParameter(): void
{
$response = $this->client->call(Client::METHOD_POST, '/functions/deployment', [
'content-type' => 'application/json'
], [
'deploymentId' => 'deployment1'
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('ID:deployment1', $response['body']);
$response = $this->client->call(Client::METHOD_POST, '/functions/deployment');
$this->assertEquals(204, $response['headers']['status-code']);
$response = $this->client->call(Client::METHOD_POST, '/functions/deployment/deployment2');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('ID:deployment2', $response['body']);
$response = $this->client->call(Client::METHOD_POST, '/database/collections/col1');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(';col1', $response['body']);
$response = $this->client->call(Client::METHOD_POST, '/databases/db2/collections/col2');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('db2;col2', $response['body']);
}
public function testDoubleSlash()
{
$response = $this->client->call(Client::METHOD_GET, '//');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Hello World!', $response['body']);
$response = $this->client->call(Client::METHOD_GET, '//path-404');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Hello World!', $response['body']);
$response = $this->client->call(Client::METHOD_GET, '//value/123');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEmpty($response['body']);
$response = $this->client->call(Client::METHOD_GET, '/value//123');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEmpty($response['body']);
$response = $this->client->call(Client::METHOD_GET, '//value//123');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEmpty($response['body']);
}
public function testCookie()
{
// One cookie
$cookie = 'cookie1=value1';
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie' => $cookie ]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($cookie, $response['body']);
// Two cookiees
$cookie = 'cookie1=value1; cookie2=value2';
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie' => $cookie ]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($cookie, $response['body']);
// Two cookies without optional space
$cookie = 'cookie1=value1;cookie2=value2';
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie' => $cookie ]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($cookie, $response['body']);
// Cookie with "=" in value
$cookie = 'cookie1=value1=value2';
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie' => $cookie ]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($cookie, $response['body']);
// Case sensitivity for cookie names
$cookie = 'cookie1=v1; Cookie1=v2';
$response = $this->client->call(Client::METHOD_GET, '/cookies', [ 'Cookie' => $cookie ]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($cookie, $response['body']);
}
public function testSetCookie()
{
$response = $this->client->call(Client::METHOD_GET, '/set-cookie');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('value1', $response['cookies']['key1']);
$this->assertEquals('value2', $response['cookies']['key2']);
$response = $this->client->call(Client::METHOD_GET, '/set-cookie-override');
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertArrayNotHasKey('key1', $response['cookies']);
$this->assertEquals('value2', $response['cookies']['key2']);
}
}