Skip to content

Commit 2b327dd

Browse files
authored
Merge pull request #333 from p1c2u/feature/response-headers-contrib-tests
Response headers support in contrib
2 parents 10675ca + a4cba7c commit 2b327dd

File tree

19 files changed

+90
-10
lines changed

19 files changed

+90
-10
lines changed

openapi_core/contrib/django/compat.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
)
55

66

7-
def get_headers(req):
7+
def get_request_headers(req):
88
# in Django 1 headers is not defined
99
return req.headers if hasattr(req, 'headers') else \
1010
HttpHeaders(req.META)
1111

1212

13+
def get_response_headers(resp):
14+
# in Django 2 headers is not defined
15+
return resp.headers if hasattr(resp, 'headers') else \
16+
dict(resp._headers.values())
17+
18+
1319
def get_current_scheme_host(req):
1420
# in Django 1 _current_scheme_host is not defined
1521
return req._current_scheme_host if hasattr(req, '_current_scheme_host') \

openapi_core/contrib/django/requests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from six.moves.urllib.parse import urljoin
55

66
from openapi_core.contrib.django.compat import (
7-
get_headers, get_current_scheme_host,
7+
get_request_headers, get_current_scheme_host,
88
)
99
from openapi_core.validation.request.datatypes import (
1010
RequestParameters, OpenAPIRequest,
@@ -39,7 +39,7 @@ def create(cls, request):
3939
path_pattern = '/' + route
4040

4141
path = request.resolver_match and request.resolver_match.kwargs or {}
42-
headers = get_headers(request)
42+
headers = get_request_headers(request)
4343
parameters = RequestParameters(
4444
path=path,
4545
query=request.GET,

openapi_core/contrib/django/responses.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""OpenAPI core contrib django responses module"""
2+
from openapi_core.contrib.django.compat import get_response_headers
23
from openapi_core.validation.response.datatypes import OpenAPIResponse
34

45

@@ -7,8 +8,10 @@ class DjangoOpenAPIResponseFactory(object):
78
@classmethod
89
def create(cls, response):
910
mimetype = response["Content-Type"]
11+
headers = get_response_headers(response)
1012
return OpenAPIResponse(
1113
data=response.content,
1214
status_code=response.status_code,
15+
headers=headers.items(),
1316
mimetype=mimetype,
1417
)

openapi_core/contrib/falcon/responses.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ def create(cls, response):
1919
return OpenAPIResponse(
2020
data=data,
2121
status_code=status_code,
22+
headers=response.headers,
2223
mimetype=mimetype,
2324
)

openapi_core/contrib/flask/responses.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ def create(cls, response):
99
return OpenAPIResponse(
1010
data=response.data,
1111
status_code=response._status_code,
12+
headers=response.headers,
1213
mimetype=response.mimetype,
1314
)

openapi_core/contrib/requests/responses.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ class RequestsOpenAPIResponseFactory(object):
77
@classmethod
88
def create(cls, response):
99
mimetype = response.headers.get('Content-Type')
10+
headers = dict(response.headers)
1011
return OpenAPIResponse(
1112
data=response.content,
1213
status_code=response.status_code,
14+
headers=headers,
1315
mimetype=mimetype,
1416
)

tests/integration/contrib/django/data/djangoproject/testapp/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def get(self, request, pk):
3030
"test": "test_val",
3131
}
3232
django_response = JsonResponse(response_dict)
33+
django_response['X-Rate-Limit'] = '12'
3334

3435
openapi_response = DjangoOpenAPIResponse(django_response)
3536
validator = ResponseValidator(spec)

tests/integration/contrib/django/data/openapi.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ paths:
1717
type: string
1818
required:
1919
- test
20+
headers:
21+
X-Rate-Limit:
22+
description: Rate limit
23+
schema:
24+
type: integer
25+
required: true
2026
parameters:
2127
- required: true
2228
in: path

tests/integration/contrib/falcon/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ def create_request(
4040
@pytest.fixture
4141
def response_factory(environ_factory):
4242
def create_response(
43-
data, status_code=200, content_type='application/json'):
43+
data, status_code=200, headers=None,
44+
content_type='application/json'):
4445
options = ResponseOptions()
4546
resp = Response(options)
4647
resp.body = data
4748
resp.content_type = content_type
4849
resp.status = HTTP_200
50+
resp.set_headers(headers or {})
4951
return resp
5052
return create_response

tests/integration/contrib/falcon/data/v3.0/falcon_factory.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ paths:
3232
properties:
3333
data:
3434
type: string
35+
headers:
36+
X-Rate-Limit:
37+
description: Rate limit
38+
schema:
39+
type: integer
40+
required: true
3541
default:
3642
description: Return errors.
3743
content:

0 commit comments

Comments
 (0)