lifecycle: add cookies attribute to ConnexionRequest (#1168) (#1209)

Co-authored-by: Robbe Sneyders <robbe.sneyders@ml6.eu>
This commit is contained in:
Nicolas Noirbent
2022-02-23 22:45:34 +01:00
committed by GitHub
parent a6717c3bbf
commit a1dddf62d4
10 changed files with 71 additions and 3 deletions

View File

@@ -362,7 +362,8 @@ class AioHttpApi(AbstractAPI):
json_getter=lambda: cls.jsonifier.loads(body),
form=form,
files=files,
context=req)
context=req,
cookies=req.cookies)
@classmethod
async def get_response(cls, response, mimetype=None, request=None):

View File

@@ -245,7 +245,8 @@ class FlaskApi(AbstractAPI):
json_getter=lambda: flask_request.get_json(silent=True),
files=flask_request.files,
path_params=params,
context=context_dict
context=context_dict,
cookies=flask_request.cookies,
)
logger.debug('Getting data and status code',
extra={

View File

@@ -16,7 +16,8 @@ class ConnexionRequest:
body=None,
json_getter=None,
files=None,
context=None):
context=None,
cookies=None):
self.url = url
self.method = method
self.path_params = path_params or {}
@@ -27,6 +28,7 @@ class ConnexionRequest:
self.json_getter = json_getter
self.files = files
self.context = context if context is not None else {}
self.cookies = cookies or {}
@property
def json(self):

View File

@@ -189,6 +189,19 @@ async def test_pythonic_path_param(aiohttp_api_spec_dir, aiohttp_client):
assert j['id_'] == 100
async def test_cookie_param(aiohttp_api_spec_dir, aiohttp_client):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir,
debug=True)
app.add_api('openapi_simple.yaml', pass_context_arg_name="request")
app_client = await aiohttp_client(app.app)
response = await app_client.get('/v1.0/test-cookie-param', headers={"Cookie": "test_cookie=hello"})
assert response.status == 200
j = await response.json()
assert j['cookie_value'] == "hello"
async def test_swagger_ui_static(aiohttp_api_spec_dir, aiohttp_client):
app = AioHttpApp(__name__, port=5001,
specification_dir=aiohttp_api_spec_dir,

View File

@@ -509,3 +509,11 @@ def test_get_unicode_request(simple_app):
resp = app_client.get('/v1.0/get_unicode_request?price=%C2%A319.99') # £19.99
assert resp.status_code == 200
assert json.loads(resp.data.decode('utf-8'))['price'] == '£19.99'
def test_cookie_param(simple_app):
app_client = simple_app.app.test_client()
app_client.set_cookie("localhost", "test_cookie", "hello")
response = app_client.get("/v1.0/test-cookie-param")
assert response.status_code == 200
assert response.json == {"cookie_value": "hello"}

View File

@@ -147,3 +147,7 @@ async def aiohttp_multipart_mixed_many_files(myfiles, body):
'myfiles_content': [ f.file.read().decode('utf8') for f in myfiles ]
},
)
async def test_cookie_param(request):
return {"cookie_value": request.cookies["test_cookie"]}

View File

@@ -330,6 +330,10 @@ def test_required_param(simple):
return simple
def test_cookie_param():
return {"cookie_value": request.cookies["test_cookie"]}
def test_exploded_deep_object_param(id):
return id

View File

@@ -20,3 +20,16 @@ paths:
'200':
description: ok
security: []
/test-cookie-param:
get:
summary: Test cookie parameter support.
operationId: fakeapi.aiohttp_handlers.test_cookie_param
parameters:
- name: test_cookie
in: cookie
required: true
schema:
type: string
responses:
'200':
description: OK

View File

@@ -663,6 +663,19 @@ paths:
responses:
'200':
description: OK
/test-cookie-param:
get:
summary: Test cookie parameter support.
operationId: fakeapi.hello.test_cookie_param
parameters:
- name: test_cookie
in: cookie
required: true
schema:
type: string
responses:
'200':
description: OK
/parameters-in-root-path:
parameters:
- in: query

View File

@@ -518,6 +518,15 @@ paths:
200:
description: OK
/test-cookie-param:
get:
summary: Test cookie parameter support.
operationId: fakeapi.hello.test_cookie_param
# No parameters because swagger / openapi 2.0 does not support describing cookie parameters.
responses:
200:
description: OK
/parameters-in-root-path:
parameters:
- in: query