Skip to content

Commit

Permalink
Merge pull request #989 from appwrite/feat-better-multipart-tests
Browse files Browse the repository at this point in the history
feat: better multipart tests
  • Loading branch information
loks0n authored Oct 1, 2024
2 parents 35c43c9 + a796ccc commit b2ffda2
Show file tree
Hide file tree
Showing 59 changed files with 757 additions and 211 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,23 @@ jobs:

- name: Lint
run: composer lint

specs:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: curl

- name: Install
run: composer install

- name: Validate specs
run: composer test tests/SpecsTest.php

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"ext-mbstring": "*",
"ext-json": "*",
"twig/twig": "3.14.*",
"matthiasmullie/minify": "1.3.*"
"matthiasmullie/minify": "1.3.*",
"utopia-php/fetch": "^0.2.1"
},
"require-dev": {
"phpunit/phpunit": "11.*",
Expand Down
77 changes: 58 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 52 additions & 4 deletions mock-server/app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,18 @@

$chunkSize = 5 * 1024 * 1024; // 5MB

if ($x != 'string') {
throw new Exception(Exception::GENERAL_MOCK, 'Wrong string value: ' . $x . ', expected: string');
}

if ($y !== 123) {
throw new Exception(Exception::GENERAL_MOCK, 'Wrong numeric value: ' . $y . ', expected: 123');
}

if ($z[0] !== 'string in array' || \count($z) !== 1) {
throw new Exception(Exception::GENERAL_MOCK, 'Wrong array value: ' . \json_encode($z) . ', expected: ["string in array"]');
}

if (!empty($contentRange)) {
$start = $request->getContentRangeStart();
$end = $request->getContentRangeEnd();
Expand Down Expand Up @@ -373,15 +385,16 @@
$file['size'] = (\is_array($file['size'])) ? $file['size'][0] : $file['size'];

if ($file['name'] !== 'file.png') {
throw new Exception(Exception::GENERAL_MOCK, 'Wrong file name');
throw new Exception(Exception::GENERAL_MOCK, 'Wrong file name: ' . $file['name'] . ', expected: file.png');
}

if ($file['size'] !== 38756) {
throw new Exception(Exception::GENERAL_MOCK, 'Wrong file size');
throw new Exception(Exception::GENERAL_MOCK, 'Wrong file size: ' . $file['size'] . ', expected: 38756');
}

if (\md5(\file_get_contents($file['tmp_name'])) !== 'd80e7e6999a3eb2ae0d631a96fe135a4') {
throw new Exception(Exception::GENERAL_MOCK, 'Wrong file uploaded');
$hash = \md5(\file_get_contents($file['tmp_name']));
if ($hash !== 'd80e7e6999a3eb2ae0d631a96fe135a4') {
throw new Exception(Exception::GENERAL_MOCK, 'Wrong file uploaded: ' . $hash . ', expected: d80e7e6999a3eb2ae0d631a96fe135a4');
}
}
});
Expand Down Expand Up @@ -410,6 +423,41 @@
]);
});

App::post('/v1/mock/tests/general/multipart-echo')
->desc('Multipart echo')
->groups(['mock'])
->label('scope', 'public')
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
->label('sdk.namespace', 'general')
->label('sdk.method', 'multipartEcho')
->label('sdk.description', 'Echo a multipart request.')
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_MULTIPART)
->label('sdk.response.model', Response::MODEL_MULTIPART)
->label('sdk.mock', true)
->param('body', '', new File(), 'Sample file param', false, [], true)
->inject('response')
->inject('request')
->action(function (string $body, Response $response, Request $request) {
if (empty($body)) {
$file = $request->getFiles('body');

if (empty($file)) {
$file = $request->getFiles(0);
}

if (isset($file['tmp_name'])) {
$body = \file_get_contents($file['tmp_name']);
} else {
$body = '';
}
}

$response->multipart([
'responseBody' => $body
]);
});

App::get('/v1/mock/tests/general/redirect')
->desc('Redirect')
->groups(['mock'])
Expand Down
2 changes: 2 additions & 0 deletions mock-server/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
services:
mockapi:
container_name: mockapi
ports:
- 3175:80
build:
context: .
args:
Expand Down
2 changes: 2 additions & 0 deletions src/SDK/Language/Ruby.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ public function getTypeName(array $parameter, array $spec = []): string
self::TYPE_STRING => 'String',
self::TYPE_ARRAY => 'Array',
self::TYPE_OBJECT => 'Hash',
self::TYPE_FILE => 'Payload',
self::TYPE_PAYLOAD => 'Payload',
self::TYPE_BOOLEAN => '',
default => $parameter['type'],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ class Client @JvmOverloads constructor(
)
}
}
it.value is Payload -> {
val payload = it.value as Payload
if (payload.sourceType == "path") {
builder.addFormDataPart(it.key, payload.filename, File(payload.path).asRequestBody())
} else {
builder.addFormDataPart(it.key, payload.toString())
}
}
else -> {
builder.addFormDataPart(it.key, it.value.toString())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Payload private constructor() {
}

fun toFile(path: String): File {
Files.createDirectories(Paths.get(path).parent);

val file = File(path)
file.appendBytes(toBinary())
return file
Expand Down
10 changes: 10 additions & 0 deletions templates/dart/lib/payload.dart.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'src/exception.dart';
import 'dart:io';

class Payload {
late final String? path;
Expand Down Expand Up @@ -44,6 +45,15 @@ class Payload {
}
}

/// Create a file from the payload
void toFile(String path) {
if(data == null) {
throw {{spec.title | caseUcfirst}}Exception('`data` is not defined.');
}
final file = File(path);
file.writeAsBytesSync(data!);
}

/// Create a Payload from binary data
factory Payload.fromBinary({
required List<int> data,
Expand Down
3 changes: 2 additions & 1 deletion templates/deno/src/payload.ts.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename } from "https://deno.land/[email protected]/path/mod.ts";
import { basename, dirname } from "https://deno.land/[email protected]/path/mod.ts";

export class Payload {
private data: Uint8Array;
Expand Down Expand Up @@ -30,6 +30,7 @@ export class Payload {
}

public async toFile(path: string): Promise<void> {
await Deno.mkdir(dirname(path), { recursive: true });
await Deno.writeFile(path, this.data);
}

Expand Down
Loading

0 comments on commit b2ffda2

Please sign in to comment.