forked from spec-first/connexion
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests to demonstrate the problems of spec-first#975
- Taken mostly from existing PR: spec-first#987
- Loading branch information
Showing
4 changed files
with
273 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import asyncio | ||
import os | ||
|
||
import aiohttp | ||
import pytest | ||
|
||
from connexion import AioHttpApp | ||
|
||
try: | ||
import ujson as json | ||
except ImportError: | ||
import json | ||
|
||
|
||
@pytest.fixture | ||
def aiohttp_app(aiohttp_api_spec_dir): | ||
app = AioHttpApp(__name__, port=5001, | ||
specification_dir=aiohttp_api_spec_dir, | ||
debug=True) | ||
app.add_api( | ||
'openapi_multipart.yaml', | ||
validate_responses=True, | ||
strict_validation=True, | ||
pythonic_params=True, | ||
pass_context_arg_name='request_ctx', | ||
) | ||
return app | ||
|
||
|
||
@asyncio.coroutine | ||
def test_single_file_upload(aiohttp_app, aiohttp_client): | ||
app_client = yield from aiohttp_client(aiohttp_app.app) | ||
|
||
resp = yield from app_client.post( | ||
'/v1.0/upload_file', | ||
data=aiohttp.FormData(fields=[('funky_funky', open(__file__, 'rb'))])(), | ||
) | ||
|
||
data = yield from resp.json() | ||
assert resp.status == 200 | ||
assert data['fileName'] == f'{__name__}.py' | ||
|
||
|
||
@asyncio.coroutine | ||
def test_many_files_upload(aiohttp_app, aiohttp_client): | ||
app_client = yield from aiohttp_client(aiohttp_app.app) | ||
|
||
dir_name = os.path.dirname(__file__) | ||
files_field = [('files', open(f'{dir_name}/{file_name}', 'rb')) for file_name in os.listdir(dir_name) if file_name.endswith('.py')] | ||
|
||
form_data = aiohttp.FormData(fields=files_field) | ||
|
||
resp = yield from app_client.post( | ||
'/v1.0/upload_files', | ||
data=form_data(), | ||
) | ||
|
||
data = yield from resp.json() | ||
|
||
assert resp.status == 200 | ||
assert data['filesCount'] == len(files_field) | ||
|
||
|
||
@asyncio.coroutine | ||
def test_mixed_multipart_single_file(aiohttp_app, aiohttp_client): | ||
app_client = yield from aiohttp_client(aiohttp_app.app) | ||
|
||
form_data = aiohttp.FormData() | ||
form_data.add_field('dirName', os.path.dirname(__file__)) | ||
form_data.add_field('funky_funky', open(__file__, 'rb')) | ||
|
||
resp = yield from app_client.post( | ||
'/v1.0/mixed_single_file', | ||
data=form_data(), | ||
) | ||
|
||
data = yield from resp.json() | ||
|
||
assert resp.status == 200 | ||
assert data['dirName'] == os.path.dirname(__file__) | ||
assert data['fileName'] == f'{__name__}.py' | ||
|
||
|
||
@asyncio.coroutine | ||
def test_mixed_multipart_many_files(aiohttp_app, aiohttp_client): | ||
app_client = yield from aiohttp_client(aiohttp_app.app) | ||
|
||
dir_name = os.path.dirname(__file__) | ||
files_field = [('files', open(f'{dir_name}/{file_name}', 'rb')) for file_name in os.listdir(dir_name) if file_name.endswith('.py')] | ||
|
||
form_data = aiohttp.FormData(fields=files_field) | ||
form_data.add_field('dirName', os.path.dirname(__file__)) | ||
form_data.add_field('testCount', str(len(files_field))) | ||
|
||
resp = yield from app_client.post( | ||
'/v1.0/mixed_many_files', | ||
data=form_data(), | ||
) | ||
|
||
data = yield from resp.json() | ||
|
||
assert resp.status == 200 | ||
assert data['dirName'] == os.path.dirname(__file__) | ||
assert data['testCount'] == len(files_field) | ||
assert data['filesCount'] == len(files_field) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
--- | ||
openapi: 3.0.0 | ||
servers: | ||
- url: /v1.0 | ||
info: | ||
title: "{{title}}" | ||
version: "1.0" | ||
paths: | ||
"/mixed_single_file": | ||
post: | ||
summary: Reads multipart data | ||
description: Handles multipart data reading | ||
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_mixed_single_file | ||
responses: | ||
"200": | ||
description: OK response | ||
content: | ||
'application/json': | ||
schema: | ||
type: object | ||
properties: | ||
dirName: | ||
type: string | ||
fileName: | ||
type: string | ||
default: | ||
description: unexpected error | ||
requestBody: | ||
required: true | ||
content: | ||
multipart/form-data: | ||
schema: | ||
type: object | ||
properties: | ||
dirName: | ||
type: string | ||
funky_funky: | ||
type: string | ||
format: binary | ||
"/mixed_many_files": | ||
post: | ||
summary: Reads multipart data | ||
description: Handles multipart data reading | ||
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_mixed_many_files | ||
responses: | ||
"200": | ||
description: OK response | ||
content: | ||
'application/json': | ||
schema: | ||
type: object | ||
properties: | ||
dirName: | ||
type: string | ||
testCount: | ||
type: number | ||
filesCount: | ||
type: number | ||
default: | ||
description: unexpected error | ||
requestBody: | ||
required: true | ||
content: | ||
multipart/form-data: | ||
schema: | ||
type: object | ||
properties: | ||
dirName: | ||
type: string | ||
testCount: | ||
type: number | ||
files: | ||
type: array | ||
items: | ||
type: string | ||
format: binary | ||
"/upload_file": | ||
post: | ||
summary: Uploads single file | ||
description: Handles multipart file upload. | ||
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_single_file | ||
responses: | ||
"200": | ||
description: OK response | ||
content: | ||
'application/json': | ||
schema: | ||
type: object | ||
properties: | ||
fileName: | ||
type: string | ||
default: | ||
description: unexpected error | ||
requestBody: | ||
required: true | ||
content: | ||
multipart/form-data: | ||
schema: | ||
type: object | ||
properties: | ||
funky_funky: | ||
type: string | ||
format: binary | ||
"/upload_files": | ||
post: | ||
summary: Uploads many files | ||
description: Handles multipart file upload. | ||
operationId: fakeapi.aiohttp_handlers.aiohttp_multipart_many_files | ||
responses: | ||
"200": | ||
description: OK response | ||
content: | ||
'application/json': | ||
schema: | ||
type: object | ||
properties: | ||
filesCount: | ||
type: number | ||
default: | ||
description: unexpected error | ||
requestBody: | ||
required: true | ||
content: | ||
multipart/form-data: | ||
schema: | ||
type: object | ||
properties: | ||
file: | ||
type: array | ||
items: | ||
type: string | ||
format: binary |