From bf284f4fde05a358bbc4db956d6b78a405c5d181 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 13 Jul 2022 15:50:25 +1200 Subject: [PATCH] Add content-type tests --- tests/e2e/Client.php | 4 + .../Services/GraphQL/GraphQLBodyTypeTest.php | 121 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 tests/e2e/Services/GraphQL/GraphQLBodyTypeTest.php diff --git a/tests/e2e/Client.php b/tests/e2e/Client.php index 226556b78..e98472b2b 100644 --- a/tests/e2e/Client.php +++ b/tests/e2e/Client.php @@ -181,6 +181,10 @@ class Client $query = $this->flatten($params); break; + case 'application/graphql': + $query = $params[0]; + break; + default: $query = http_build_query($params); break; diff --git a/tests/e2e/Services/GraphQL/GraphQLBodyTypeTest.php b/tests/e2e/Services/GraphQL/GraphQLBodyTypeTest.php new file mode 100644 index 000000000..72fba0056 --- /dev/null +++ b/tests/e2e/Services/GraphQL/GraphQLBodyTypeTest.php @@ -0,0 +1,121 @@ +getProject()['$id']; + $query = 'query { localeGetCountries { total countries { code } } }'; + $graphQLPayload = [$query]; // Needs to be an array because the test client expects it + $response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ + 'content-type' => 'application/graphql', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $graphQLPayload); + + $this->assertIsArray($response['body']['data']); + $this->assertArrayNotHasKey('errors', $response['body']); + $response = $response['body']['data']['localeGetCountries']; + $this->assertEquals(194, $response['total']); + } + + public function testSingleQueryJSONContentType() + { + $projectId = $this->getProject()['$id']; + $query = 'query { localeGetCountries { total countries { code } } }'; + $graphQLPayload = ['query' => $query]; + $response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $graphQLPayload); + + $this->assertIsArray($response['body']['data']); + $this->assertArrayNotHasKey('errors', $response['body']); + $response = $response['body']['data']['localeGetCountries']; + $this->assertEquals(194, $response['total']); + } + + public function testBatchedJSONContentType() + { + $projectId = $this->getProject()['$id']; + $query1 = 'query { localeGetCountries { total countries { code } } }'; + $query2 = 'query { localeGetContinents { total continents { code } } }'; + $graphQLPayload = [ + ['query' => $query1], + ['query' => $query2], + ]; + $response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $graphQLPayload); + + $this->assertIsArray($response['body']['data']); + $this->assertArrayNotHasKey('errors', $response['body']); + $this->assertArrayHasKey('localeGetCountries', $response['body']['data']); + $this->assertArrayHasKey('localeGetContinents', $response['body']['data']); + $this->assertEquals(194, $response['body']['data']['localeGetCountries']['total']); + $this->assertEquals(7, $response['body']['data']['localeGetContinents']['total']); + } + + public function testMultipartFormDataContentType() + { + $projectId = $this->getProject()['$id']; + + $query = $this->getQuery(self::$CREATE_BUCKET); + $gqlPayload = [ + 'query' => $query, + 'variables' => [ + 'bucketId' => 'unique()', + 'name' => 'Test Bucket', + 'permission' => 'bucket', + 'read' => ['role:all'], + 'write' => ['role:all'], + ] + ]; + $bucket = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $bucket = $bucket['body']['data']['storageCreateBucket']; + + $query = $this->getQuery(self::$CREATE_FILE); + $gqlPayload = [ + 'operations' => \json_encode([ + 'query' => $query, + 'variables' => [ + 'bucketId' => $bucket['_id'], + 'fileId' => 'unique()', + 'file' => null, + 'permissions' => 'file', + 'read' => ['role:all'], + 'write' => ['role:all'], + ] + ]), + 'map' => \json_encode([ + 'file' => ["variables.file"] + ]), + 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'), + ]; + + $file = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), $gqlPayload); + + $this->assertIsArray($file['body']['data']); + $this->assertArrayNotHasKey('errors', $file['body']); + $this->assertIsArray($file['body']['data']['storageCreateFile']); + } +} \ No newline at end of file