Skip to content

feat: python multipart #943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mock-server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor
# Add Source Code
COPY ./src /usr/src/code/src
COPY ./app /usr/src/code/app
COPY ./resources /usr/src/code/resources

EXPOSE 80

Expand Down
61 changes: 43 additions & 18 deletions mock-server/app/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->inject('response')
->action(function (UtopiaSwooleResponse $response) {
->action(function (Response $response) {
$response->json([ 'version' => '1.0.0' ]);
});

Expand Down Expand Up @@ -263,7 +263,7 @@
->label('sdk.mock', true)
->inject('request')
->inject('response')
->action(function (Request $request, UtopiaSwooleResponse $response) {
->action(function (Request $request, Response $response) {
$res = [
'x-sdk-name' => $request->getHeader('x-sdk-name'),
'x-sdk-platform' => $request->getHeader('x-sdk-platform'),
Expand Down Expand Up @@ -291,7 +291,7 @@
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.mock', true)
->inject('response')
->action(function (UtopiaSwooleResponse $response) {
->action(function (Response $response) {

$response
->setContentType('text/plain')
Expand All @@ -317,12 +317,12 @@
->param('x', '', new Text(100), 'Sample string param')
->param('y', '', new Integer(true), 'Sample numeric param')
->param('z', null, new ArrayList(new Text(256), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Sample array param')
->param('file', [], new File(), 'Sample file param', skipValidation: true)
->param('payload', [], new File(), 'Sample file param', skipValidation: true)
->inject('request')
->inject('response')
->action(function (string $x, int $y, array $z, mixed $file, Request $request, UtopiaSwooleResponse $response) {
->action(function (string $x, int $y, array $z, mixed $file, Request $request, Response $response) {

$file = $request->getFiles('file');
$file = $request->getFiles('payload');

$contentRange = $request->getHeader('content-range');

Expand Down Expand Up @@ -389,6 +389,29 @@
}
});

App::get('/v1/mock/tests/general/multipart')
->desc('Multipart')
->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', 'multipart')
->label('sdk.description', 'Mock 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)
->inject('response')
->action(function (Response $response) {
$file = \file_get_contents(\getcwd() . '/resources/file.png');

$response->multipart([
'x' => 'abc',
'y' => 123,
'responseBody' => $file,
]);
});

App::get('/v1/mock/tests/general/redirect')
->desc('Redirect')
->groups(['mock'])
Expand All @@ -402,7 +425,7 @@
->label('sdk.response.model', Response::MODEL_MOCK)
->label('sdk.mock', true)
->inject('response')
->action(function (UtopiaSwooleResponse $response) {
->action(function (Response $response) {
$response->redirect('/v1/mock/tests/general/redirect/done');
});

Expand Down Expand Up @@ -435,7 +458,7 @@
->label('sdk.mock', true)
->inject('response')
->inject('request')
->action(function (UtopiaSwooleResponse $response, Request $request) {
->action(function (Response $response, Request $request) {
$response->addCookie('cookieName', 'cookieValue', \time() + 31536000, '/', $request->getHostname(), true, true);
});

Expand Down Expand Up @@ -470,7 +493,7 @@
->label('sdk.response.model', Response::MODEL_NONE)
->label('sdk.mock', true)
->inject('response')
->action(function (UtopiaSwooleResponse $response) {
->action(function (Response $response) {
$response->noContent();
});

Expand Down Expand Up @@ -547,7 +570,7 @@
->label('sdk.response.model', Response::MODEL_ANY)
->label('sdk.mock', true)
->inject('response')
->action(function (UtopiaSwooleResponse $response) {
->action(function (Response $response) {

$response
->setStatusCode(502)
Expand All @@ -569,7 +592,7 @@
->param('success', '', new Text(1024), 'OAuth2 success redirect URI.')
->param('failure', '', new Text(1024), 'OAuth2 failure redirect URI.')
->inject('response')
->action(function (string $clientId, array $scopes, string $state, string $success, string $failure, UtopiaSwooleResponse $response) {
->action(function (string $clientId, array $scopes, string $state, string $success, string $failure, Response $response) {
$response->redirect($success . '?' . \http_build_query(['code' => 'abcdef', 'state' => $state]));
});

Expand All @@ -587,7 +610,7 @@
->param('code', '', new Text(100), 'OAuth2 state.', true)
->param('refresh_token', '', new Text(100), 'OAuth2 refresh token.', true)
->inject('response')
->action(function (string $client_id, string $client_secret, string $grantType, string $redirectURI, string $code, string $refreshToken, UtopiaSwooleResponse $response) {
->action(function (string $client_id, string $client_secret, string $grantType, string $redirectURI, string $code, string $refreshToken, Response $response) {
if ($client_id != '1') {
throw new Exception(Exception::GENERAL_MOCK, 'Invalid client ID');
}
Expand Down Expand Up @@ -626,7 +649,7 @@
->label('docs', false)
->param('token', '', new Text(100), 'OAuth2 Access Token.')
->inject('response')
->action(function (string $token, UtopiaSwooleResponse $response) {
->action(function (string $token, Response $response) {
if ($token != '123456') {
throw new Exception(Exception::GENERAL_MOCK, 'Invalid token');
}
Expand All @@ -644,7 +667,7 @@
->label('scope', 'public')
->label('docs', false)
->inject('response')
->action(function (UtopiaSwooleResponse $response) {
->action(function (Response $response) {

$response->json([
'result' => 'success',
Expand All @@ -657,7 +680,7 @@
->label('scope', 'public')
->label('docs', false)
->inject('response')
->action(function (UtopiaSwooleResponse $response) {
->action(function (Response $response) {

$response
->setStatusCode(Response::STATUS_CODE_BAD_REQUEST)
Expand All @@ -671,7 +694,7 @@
->inject('utopia')
->inject('response')
->inject('request')
->action(function (App $utopia, UtopiaSwooleResponse $response, Request $request) {
->action(function (App $utopia, Response $response, Request $request) {

$result = [];
$route = $utopia->getRoute();
Expand All @@ -690,7 +713,9 @@
throw new Exception(Exception::GENERAL_MOCK, 'Failed to save results', 500);
}

$response->json(['result' => $route->getMethod() . ':' . $route->getPath() . ':passed']);
if ($route->getPath() !== '/v1/mock/tests/general/multipart') {
$response->json(['result' => $route->getMethod() . ':' . $route->getPath() . ':passed']);
}
});

App::error()
Expand Down Expand Up @@ -779,7 +804,7 @@ function () use ($http) {

$http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
$request = new Request($swooleRequest);
$response = new UtopiaSwooleResponse($swooleResponse);
$response = new Response($swooleResponse);

$app = new App('UTC');

Expand Down
3 changes: 2 additions & 1 deletion mock-server/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"utopia-php/framework": "0.33.*",
"utopia-php/database": "0.48.*",
"utopia-php/cli": "0.16.*",
"utopia-php/swoole": "0.8.*"
"utopia-php/swoole": "0.8.*",
"utopia-php/fetch": "0.2.*"
},
"require-dev": {
"swoole/ide-helper": "5.1.2"
Expand Down
Loading
Loading