appwrite/tests/e2e/General/HTTPTest.php

181 lines
6.1 KiB
PHP
Raw Normal View History

2020-07-07 21:14:40 +00:00
<?php
2020-12-26 12:10:14 +00:00
namespace Tests\E2E\General;
2020-07-07 21:14:40 +00:00
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectNone;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideNone;
2026-02-26 05:50:29 +00:00
use Utopia\Config\Config;
2020-07-07 21:14:40 +00:00
class HTTPTest extends Scope
{
use ProjectNone;
use SideNone;
public function setUp(): void
{
parent::setUp();
2025-12-07 20:29:45 +00:00
$this->client->setEndpoint('http://appwrite.test');
}
2020-07-07 21:14:40 +00:00
public function testOptions()
{
/**
* Test for SUCCESS
*/
2022-01-02 16:08:27 +00:00
$response = $this->client->call(Client::METHOD_OPTIONS, '/', \array_merge([
2020-07-07 21:14:40 +00:00
'origin' => 'http://localhost',
'content-type' => 'application/json',
]), []);
2026-02-26 05:50:29 +00:00
$corsConfig = Config::getParam('cors');
$allowedMethods = \implode(', ', $corsConfig['allowedMethods']);
$allowedHeaders = \implode(', ', $corsConfig['allowedHeaders']);
$exposedHeaders = \implode(', ', $corsConfig['exposedHeaders']);
2021-02-02 11:27:12 +00:00
$this->assertEquals(204, $response['headers']['status-code']);
2020-07-07 21:14:40 +00:00
$this->assertEquals('Appwrite', $response['headers']['server']);
2026-02-26 05:50:29 +00:00
$this->assertEquals($allowedMethods, $response['headers']['access-control-allow-methods']);
$this->assertEquals($allowedHeaders, $response['headers']['access-control-allow-headers']);
$this->assertEquals($exposedHeaders, $response['headers']['access-control-expose-headers']);
2020-07-07 21:14:40 +00:00
$this->assertEquals('http://localhost', $response['headers']['access-control-allow-origin']);
$this->assertEquals('true', $response['headers']['access-control-allow-credentials']);
$this->assertEmpty($response['body']);
}
2020-10-29 21:15:45 +00:00
public function testHumans()
{
/**
* Test for SUCCESS
*/
2022-01-02 16:08:27 +00:00
$response = $this->client->call(Client::METHOD_GET, '/humans.txt', \array_merge([
2020-10-29 21:15:45 +00:00
'origin' => 'http://localhost',
]));
2020-10-29 21:15:45 +00:00
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString('# humanstxt.org/', $response['body']);
}
public function testRobots()
{
/**
* Test for SUCCESS
*/
2022-01-02 16:08:27 +00:00
$response = $this->client->call(Client::METHOD_GET, '/robots.txt', \array_merge([
2020-10-29 21:15:45 +00:00
'origin' => 'http://localhost',
]));
2020-10-29 21:15:45 +00:00
2025-06-26 16:09:07 +00:00
$this->assertEquals(200, $response['headers']['status-code'], "Simple GET /robots.txt HTTP request failed: " . \json_encode($response));
2020-10-29 21:15:45 +00:00
$this->assertStringContainsString('# robotstxt.org/', $response['body']);
}
2020-11-11 22:03:27 +00:00
2022-01-31 15:04:30 +00:00
public function testAcmeChallenge()
{
/**
* Test for SUCCESS
*/
2025-12-07 20:29:45 +00:00
$response = $this->client->call(Client::METHOD_GET, '/.well-known/acme-challenge/8DdIKX257k6Dih5s_saeVMpTnjPJdKO5Ase0OCiJrIg');
// 'Unknown path', but validation passed
2023-06-02 10:58:31 +00:00
$this->assertEquals(404, $response['headers']['status-code']);
2022-01-31 15:04:30 +00:00
/**
* Test for FAILURE
*/
2025-12-07 20:29:45 +00:00
$response = $this->client->call(Client::METHOD_GET, '/.well-known/acme-challenge/../../../../../../../etc/passwd');
2022-01-31 15:04:30 +00:00
2025-12-07 20:29:45 +00:00
// 'Unknown path', but validation passed
$this->assertEquals(404, $response['headers']['status-code']);
2022-01-31 15:04:30 +00:00
}
2022-05-23 14:54:50 +00:00
public function testVersions()
{
2021-02-24 18:31:43 +00:00
/**
* Test without header
*/
2022-01-02 16:08:27 +00:00
$response = $this->client->call(Client::METHOD_GET, '/versions', \array_merge([
2021-02-24 18:31:43 +00:00
'content-type' => 'application/json',
], $this->getHeaders()));
$body = $response['body'];
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertIsString($body['server']);
$this->assertIsString($body['client-web']);
$this->assertIsString($body['client-flutter']);
$this->assertIsString($body['console-web']);
$this->assertIsString($body['server-nodejs']);
$this->assertIsString($body['server-php']);
$this->assertIsString($body['server-python']);
$this->assertIsString($body['server-ruby']);
2022-02-26 02:01:02 +00:00
$this->assertIsString($body['console-cli']);
2021-02-24 18:31:43 +00:00
}
public function testDefaultOAuth2()
{
2024-07-17 20:47:33 +00:00
$response = $this->client->call(Client::METHOD_GET, '/console/auth/oauth2/success', $this->getHeaders());
$this->assertEquals(200, $response['headers']['status-code']);
2024-07-17 20:47:33 +00:00
$response = $this->client->call(Client::METHOD_GET, '/console/auth/oauth2/failure', $this->getHeaders());
$this->assertEquals(200, $response['headers']['status-code']);
}
public function testCors()
{
$endpoint = '/v1/projects'; // Can be any non-404 route
2025-12-07 20:29:45 +00:00
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_GET, $endpoint, [
'origin' => 'http://localhost',
]);
$this->assertEquals('http://localhost', $response['headers']['access-control-allow-origin']);
/**
* Test for FAILURE
*/
2025-12-07 20:29:45 +00:00
// you should not return a fallback origin for a no host
$response = $this->client->call(Client::METHOD_GET, $endpoint);
$this->assertNull($response['headers']['access-control-allow-origin'] ?? null);
// you should not return a fallback origin for a no host
$response = $this->client->call(Client::METHOD_GET, $endpoint, [
'origin' => 'http://google.com',
]);
2025-12-07 20:29:45 +00:00
$this->assertNull($response['headers']['access-control-allow-origin'] ?? null);
2025-12-12 09:21:57 +00:00
}
public function testPreflight()
{
$endpoint = '/v1/projects'; // Can be any non-404 route
2025-12-12 09:21:57 +00:00
/**
* Test for SUCCESS
*/
$response = $this->client->call(Client::METHOD_OPTIONS, $endpoint, [
'origin' => 'http://random.com',
'access-control-request-headers' => 'X-Appwrite-Project',
'access-control-request-method' => 'GET'
]);
$this->assertEquals('http://random.com', $response['headers']['access-control-allow-origin']);
}
public function testConsoleRedirect()
{
/**
* Test for SUCCESS
*/
$endpoint = '/invite?membershipId=123&userId=asdf';
$response = $this->client->call(Client::METHOD_GET, $endpoint);
$this->assertEquals('/console' . $endpoint, $response['headers']['location']);
}
}