From 096d9381f0988fa9af0c1fb621123b00c05f9c56 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Thu, 11 Feb 2016 15:07:06 +0100 Subject: [PATCH] Support header params on 204 response code --- connexion/decorators/produces.py | 4 ++-- tests/fakeapi/api.yaml | 24 ++++++++++++++++++++++++ tests/fakeapi/hello.py | 18 ++++++++++++++++++ tests/test_app.py | 29 +++++++++++++++++++++++------ 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/connexion/decorators/produces.py b/connexion/decorators/produces.py index f65a513d0..de6d25650 100644 --- a/connexion/decorators/produces.py +++ b/connexion/decorators/produces.py @@ -119,10 +119,10 @@ def wrapper(*args, **kwargs): logger.debug('Endpoint returned a Flask Response', extra={'url': url, 'mimetype': data.mimetype}) return data elif data is NoContent: - return '', status_code + return '', status_code, headers elif status_code == 204: logger.debug('Endpoint returned an empty response (204)', extra={'url': url, 'mimetype': self.mimetype}) - return '', 204 + return '', 204, headers data = json.dumps(data, indent=2, cls=JSONEncoder) response = flask.current_app.response_class(data, mimetype=self.mimetype) # type: flask.Response diff --git a/tests/fakeapi/api.yaml b/tests/fakeapi/api.yaml index 019f9f71b..2afb4039d 100644 --- a/tests/fakeapi/api.yaml +++ b/tests/fakeapi/api.yaml @@ -721,6 +721,30 @@ paths: 302: description: 302 Found + /test-204-with-headers: + get: + summary: Tests that response code 204 can have headers set + operationId: fakeapi.hello.test_204_with_headers + responses: + 204: + headers: + X-Something: + description: A value that might be send in the response + type: string + description: 204 no content + + /test-204-with-headers-nocontent-obj: + get: + summary: Tests that response code 204 using NoContent obj can have headers set + operationId: fakeapi.hello.test_nocontent_obj_with_headers + responses: + 204: + headers: + X-Something: + description: A value that might be send in the response + type: string + description: 204 no content + definitions: new_stack: type: object diff --git a/tests/fakeapi/hello.py b/tests/fakeapi/hello.py index b7c1bee26..b73b1643b 100755 --- a/tests/fakeapi/hello.py +++ b/tests/fakeapi/hello.py @@ -15,18 +15,23 @@ def test_method(self): class_instance = DummyClass() + def get(): return '' + def search(): return '' + def list(): return '' + def post(): return '' + def post_greeting(name): data = {'greeting': 'Hello {name}'.format(name=name)} return data @@ -160,12 +165,15 @@ def schema_query(image_version=None): def schema_list(): return '' + def schema_map(): return '' + def schema_recursive(): return '' + def schema_format(): return '' @@ -253,3 +261,13 @@ def test_redirect_endpoint(): def test_redirect_response_endpoint(): return redirect('http://www.google.com/') + + +def test_204_with_headers(): + headers = {'X-Something': 'test'} + return '', 204, headers + + +def test_nocontent_obj_with_headers(): + headers = {'X-Something': 'test'} + return NoContent, 204, headers diff --git a/tests/test_app.py b/tests/test_app.py index b08b61630..3dc152a4e 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -2,7 +2,6 @@ import json import logging import pytest -import _pytest.monkeypatch from connexion.app import App logging.basicConfig(level=logging.DEBUG) @@ -397,6 +396,7 @@ def test_schema_list(app): assert wrong_items_response['title'] == 'Bad Request' assert wrong_items_response['detail'].startswith("42 is not of type 'string'") + def test_schema_map(app): app_client = app.app.test_client() headers = {'Content-type': 'application/json'} @@ -433,6 +433,7 @@ def test_schema_map(app): data=json.dumps(valid_object)) # type: flask.Response assert right_type.status_code == 200 + def test_schema_recursive(app): app_client = app.app.test_client() headers = {'Content-type': 'application/json'} @@ -470,6 +471,7 @@ def test_schema_recursive(app): data=json.dumps(valid_object)) # type: flask.Response assert right_type.status_code == 200 + def test_schema_format(app): app_client = app.app.test_client() headers = {'Content-type': 'application/json'} @@ -671,7 +673,7 @@ def test_bool_as_default_param(app): resp = app_client.get('/v1.0/test-bool-param', query_string={'thruthiness': True}) assert resp.status_code == 200 response = json.loads(resp.data.decode()) - assert response == True + assert response is True def test_bool_param(app): @@ -679,12 +681,12 @@ def test_bool_param(app): resp = app_client.get('/v1.0/test-bool-param', query_string={'thruthiness': True}) assert resp.status_code == 200 response = json.loads(resp.data.decode()) - assert response == True + assert response is True resp = app_client.get('/v1.0/test-bool-param', query_string={'thruthiness': False}) assert resp.status_code == 200 response = json.loads(resp.data.decode()) - assert response == False + assert response is False def test_bool_array_param(app): @@ -692,13 +694,14 @@ def test_bool_array_param(app): resp = app_client.get('/v1.0/test-bool-array-param?thruthiness=true,true,true') assert resp.status_code == 200 response = json.loads(resp.data.decode()) - assert response == True + assert response is True app_client = app.app.test_client() resp = app_client.get('/v1.0/test-bool-array-param?thruthiness=true,true,false') assert resp.status_code == 200 response = json.loads(resp.data.decode()) - assert response == False + assert response is False + def test_required_param_miss_config(app): app_client = app.app.test_client() @@ -753,3 +756,17 @@ def test_security_over_inexistent_endpoints(oauth_requests): post_greeting = app_client.post('/v1.0/greeting/rcaricio', data={}) # type: flask.Response assert post_greeting.status_code == 401 + + +def test_no_content_response_have_headers(app): + app_client = app.app.test_client() + resp = app_client.get('/v1.0/test-204-with-headers') + assert resp.status_code == 204 + assert 'X-Something' in resp.headers + + +def test_no_content_object_and_have_headers(app): + app_client = app.app.test_client() + resp = app_client.get('/v1.0/test-204-with-headers-nocontent-obj') + assert resp.status_code == 204 + assert 'X-Something' in resp.headers