Skip to content

Commit

Permalink
Support header params on 204 response code
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcaricio committed Feb 11, 2016
1 parent 812ed1c commit 096d938
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
4 changes: 2 additions & 2 deletions connexion/decorators/produces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tests/fakeapi/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/fakeapi/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ''

Expand Down Expand Up @@ -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
29 changes: 23 additions & 6 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import logging
import pytest
import _pytest.monkeypatch
from connexion.app import App

logging.basicConfig(level=logging.DEBUG)
Expand Down Expand Up @@ -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'}
Expand Down Expand Up @@ -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'}
Expand Down Expand Up @@ -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'}
Expand Down Expand Up @@ -671,34 +673,35 @@ 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):
app_client = app.app.test_client()
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):
app_client = app.app.test_client()
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()
Expand Down Expand Up @@ -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

0 comments on commit 096d938

Please sign in to comment.