1
0
Fork 0
mirror of synced 2024-07-02 05:00:33 +12:00

Fix tests

This commit is contained in:
Jake Barnby 2023-05-30 18:26:17 +12:00
parent b092405b04
commit 0661f1f889
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C
5 changed files with 51 additions and 69 deletions

View file

@ -160,35 +160,21 @@ class Client
* @param array $params * @param array $params
* @param array $headers * @param array $headers
* @param bool $decode * @param bool $decode
* @return array|string * @return array
* @throws Exception * @throws Exception
*/ */
public function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true) public function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true): array
{ {
$headers = array_merge($this->headers, $headers); $headers = array_merge($this->headers, $headers);
$ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : '')); $ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : ''));
$responseHeaders = []; $responseHeaders = [];
$responseStatus = -1;
$responseType = '';
$responseBody = '';
switch ($headers['content-type']) { $query = match ($headers['content-type']) {
case 'application/json': 'application/json' => json_encode($params),
$query = json_encode($params); 'multipart/form-data' => $this->flatten($params),
break; 'application/graphql' => $params[0],
default => http_build_query($params),
case 'multipart/form-data': };
$query = $this->flatten($params);
break;
case 'application/graphql':
$query = $params[0];
break;
default:
$query = http_build_query($params);
break;
}
foreach ($headers as $i => $header) { foreach ($headers as $i => $header) {
$headers[] = $i . ':' . $header; $headers[] = $i . ':' . $header;
@ -220,7 +206,7 @@ class Client
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
} }
// Allow self signed certificates // Allow self-signed certificates
if ($this->selfSigned) { if ($this->selfSigned) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
@ -230,22 +216,18 @@ class Client
$responseType = $responseHeaders['content-type'] ?? ''; $responseType = $responseHeaders['content-type'] ?? '';
$responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($decode) { if ($decode && substr($responseType, 0, strpos($responseType, ';')) == 'application/json') {
switch (substr($responseType, 0, strpos($responseType, ';'))) { $json = json_decode($responseBody, true);
case 'application/json':
$json = json_decode($responseBody, true);
if ($json === null) { if ($json === null) {
throw new Exception('Failed to parse response: ' . $responseBody); throw new Exception('Failed to parse response: ' . $responseBody);
}
$responseBody = $json;
$json = null;
break;
} }
$responseBody = $json;
$json = null;
} }
if ((curl_errno($ch)/* || 200 != $responseStatus*/)) { if ((curl_errno($ch))) {
throw new Exception(curl_error($ch) . ' with status code ' . $responseStatus, $responseStatus); throw new Exception(curl_error($ch) . ' with status code ' . $responseStatus, $responseStatus);
} }
@ -273,7 +255,7 @@ class Client
{ {
$cookies = []; $cookies = [];
parse_str(strtr($cookie, array('&' => '%26', '+' => '%2B', ';' => '&')), $cookies); parse_str(strtr($cookie, ['&' => '%26', '+' => '%2B', ';' => '&']), $cookies);
return $cookies; return $cookies;
} }

View file

@ -12,6 +12,12 @@ class HTTPTest extends Scope
use ProjectNone; use ProjectNone;
use SideNone; use SideNone;
public function setUp(): void
{
parent::setUp();
$this->client->setEndpoint('http://localhost');
}
public function testOptions() public function testOptions()
{ {
/** /**
@ -32,24 +38,6 @@ class HTTPTest extends Scope
$this->assertEmpty($response['body']); $this->assertEmpty($response['body']);
} }
public function testError()
{
/**
* Test for SUCCESS
*/
$this->markTestIncomplete('This test needs to be updated for the new console.');
// $response = $this->client->call(Client::METHOD_GET, '/error', \array_merge([
// 'origin' => 'http://localhost',
// 'content-type' => 'application/json',
// ]), []);
// $this->assertEquals(404, $response['headers']['status-code']);
// $this->assertEquals('Not Found', $response['body']['message']);
// $this->assertEquals(Exception::GENERAL_ROUTE_NOT_FOUND, $response['body']['type']);
// $this->assertEquals(404, $response['body']['code']);
// $this->assertEquals('dev', $response['body']['version']);
}
public function testHumans() public function testHumans()
{ {
/** /**
@ -57,7 +45,7 @@ class HTTPTest extends Scope
*/ */
$response = $this->client->call(Client::METHOD_GET, '/humans.txt', \array_merge([ $response = $this->client->call(Client::METHOD_GET, '/humans.txt', \array_merge([
'origin' => 'http://localhost', 'origin' => 'http://localhost',
]), []); ]));
$this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString('# humanstxt.org/', $response['body']); $this->assertStringContainsString('# humanstxt.org/', $response['body']);
@ -70,7 +58,7 @@ class HTTPTest extends Scope
*/ */
$response = $this->client->call(Client::METHOD_GET, '/robots.txt', \array_merge([ $response = $this->client->call(Client::METHOD_GET, '/robots.txt', \array_merge([
'origin' => 'http://localhost', 'origin' => 'http://localhost',
]), []); ]));
$this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(200, $response['headers']['status-code']);
$this->assertStringContainsString('# robotstxt.org/', $response['body']); $this->assertStringContainsString('# robotstxt.org/', $response['body']);
@ -87,7 +75,7 @@ class HTTPTest extends Scope
*/ */
$response = $this->client->call(Client::METHOD_GET, '/.well-known/acme-challenge/8DdIKX257k6Dih5s_saeVMpTnjPJdKO5Ase0OCiJrIg', \array_merge([ $response = $this->client->call(Client::METHOD_GET, '/.well-known/acme-challenge/8DdIKX257k6Dih5s_saeVMpTnjPJdKO5Ase0OCiJrIg', \array_merge([
'origin' => 'http://localhost', 'origin' => 'http://localhost',
]), []); ]));
$this->assertEquals(404, $response['headers']['status-code']); $this->assertEquals(404, $response['headers']['status-code']);
// 'Unknown path', but validation passed // 'Unknown path', but validation passed
@ -97,9 +85,9 @@ class HTTPTest extends Scope
*/ */
$response = $this->client->call(Client::METHOD_GET, '/.well-known/acme-challenge/../../../../../../../etc/passwd', \array_merge([ $response = $this->client->call(Client::METHOD_GET, '/.well-known/acme-challenge/../../../../../../../etc/passwd', \array_merge([
'origin' => 'http://localhost', 'origin' => 'http://localhost',
]), []); ]));
$this->assertEquals(400, $response['headers']['status-code']); $this->assertEquals(404, $response['headers']['status-code']);
// Cleanup // Cleanup
$this->client->setEndpoint($previousEndpoint); $this->client->setEndpoint($previousEndpoint);

View file

@ -17,10 +17,7 @@ abstract class Scope extends TestCase
protected function setUp(): void protected function setUp(): void
{ {
$this->client = new Client(); $this->client = new Client();
$this->client->setEndpoint($this->endpoint);
$this->client
->setEndpoint($this->endpoint)
;
} }
protected function tearDown(): void protected function tearDown(): void
@ -45,10 +42,10 @@ abstract class Scope extends TestCase
{ {
sleep(2); sleep(2);
$resquest = json_decode(file_get_contents('http://request-catcher:5000/__last_request__'), true); $request = json_decode(file_get_contents('http://request-catcher:5000/__last_request__'), true);
$resquest['data'] = json_decode($resquest['data'], true); $request['data'] = json_decode($request['data'], true);
return $resquest; return $request;
} }
/** /**

View file

@ -3053,7 +3053,7 @@ trait DatabasesBase
$databaseId = $database['body']['$id']; $databaseId = $database['body']['$id'];
// Create collection // Create collection
$movies = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/', array_merge([ $movies = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'] 'x-appwrite-key' => $this->getProject()['apiKey']

View file

@ -814,11 +814,11 @@ class ProjectsConsoleClientTest extends Scope
$this->assertEquals($response['headers']['status-code'], 501); $this->assertEquals($response['headers']['status-code'], 501);
$response = $this->client->call(Client::METHOD_POST, '/account/anonymous', array_merge([ $response = $this->client->call(Client::METHOD_POST, '/account/sessions/anonymous', array_merge([
'origin' => 'http://localhost', 'origin' => 'http://localhost',
'content-type' => 'application/json', 'content-type' => 'application/json',
'x-appwrite-project' => $id, 'x-appwrite-project' => $id,
]), []); ]));
$this->assertEquals($response['headers']['status-code'], 501); $this->assertEquals($response['headers']['status-code'], 501);
@ -874,6 +874,19 @@ class ProjectsConsoleClientTest extends Scope
'name' => $name, 'name' => $name,
]); ]);
$email = uniqid() . 'user@localhost.test';
$response = $this->client->call(Client::METHOD_POST, '/account', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $id,
]), [
'userId' => ID::unique(),
'email' => $email,
'password' => $password,
'name' => $name,
]);
$this->assertEquals($response['headers']['status-code'], 501); $this->assertEquals($response['headers']['status-code'], 501);
/** /**
@ -889,6 +902,8 @@ class ProjectsConsoleClientTest extends Scope
$this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals(200, $response['headers']['status-code']);
$this->assertNotEmpty($response['body']['$id']); $this->assertNotEmpty($response['body']['$id']);
$email = uniqid() . 'user@localhost.test';
$response = $this->client->call(Client::METHOD_POST, '/account', array_merge([ $response = $this->client->call(Client::METHOD_POST, '/account', array_merge([
'origin' => 'http://localhost', 'origin' => 'http://localhost',
'content-type' => 'application/json', 'content-type' => 'application/json',