Skip to content

Commit

Permalink
lifecycle: add cookies attribute to ConnexionRequest (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbee committed May 15, 2020
1 parent bed4b95 commit 98e71e3
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion connexion/apis/aiohttp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ async def get_request(cls, req):
body=body,
json_getter=lambda: cls.jsonifier.loads(body),
files={},
context=req)
context=req,
cookies=req.cookies)

@classmethod
async def get_response(cls, response, mimetype=None, request=None):
Expand Down
3 changes: 2 additions & 1 deletion connexion/apis/flask_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ def get_request(cls, *args, **params):
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={
Expand Down
4 changes: 3 additions & 1 deletion connexion/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def __init__(self,
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 {}
Expand All @@ -21,6 +22,7 @@ def __init__(self,
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):
Expand Down
8 changes: 8 additions & 0 deletions tests/api/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,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"}
4 changes: 4 additions & 0 deletions tests/fakeapi/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,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

Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/simple/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/simple/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98e71e3

Please sign in to comment.