Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KeyError if no Content-Type was provided in request #497

Merged
merged 4 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion connexion/decorators/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def wrapper(request):
return problem(415,
"Unsupported Media Type",
"Invalid Content-type ({content_type}), expected JSON data".format(
content_type=request.headers["Content-Type"]
content_type=request.headers.get("Content-Type", "")
))

logger.debug("%s validating schema...", request.url)
Expand Down
20 changes: 20 additions & 0 deletions tests/api/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,26 @@ def test_post_wrong_content_type(simple_app):
)
assert resp.status_code == 415

# this test checks exactly what the test directly above is supposed to check,
# i.e. no content-type is provided in the header
# unfortunately there is an issue with the werkzeug test environment
# (https://github.com/pallets/werkzeug/issues/1159)
# so that content-type is added to every request, we remove it here manually for our test
# this test can be removed once the werkzeug issue is addressed
from werkzeug.test import Client, EnvironBuilder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this import to the top level (beginning of the file)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

builder = EnvironBuilder(path='/v1.0/post_wrong_content_type', method='POST',
data=json.dumps({"some": "data"}))
try:
environ = builder.get_environ()
finally:
builder.close()
environ.pop('CONTENT_TYPE')
# we cannot just call app_client.open() since app_client is a flask.testing.FlaskClient
# which overrides werkzeug.test.Client.open() but does not allow passing an environment
# directly
resp = Client.open(app_client, environ)
assert resp.status_code == 415


def test_get_unicode_response(simple_app):
app_client = simple_app.app.test_client()
Expand Down