Skip to content

Commit

Permalink
Merge pull request #354 from mozilla-services/content-type-checking-w…
Browse files Browse the repository at this point in the history
…ith-no-content-type

Do not raise a 415 error when no content-type and no body
  • Loading branch information
Natim committed Mar 15, 2016
2 parents b749473 + 7c87be7 commit 4d99a3d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cornice/pyramidhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ def register_service_views(config, service):
predicate_definitions = _pop_complex_predicates(args)

if predicate_definitions:
for predicate_list in predicate_definitions:
empty_contenttype = [({'kind': 'content_type', 'value': ''},)]
for predicate_list in predicate_definitions + empty_contenttype:
args = dict(args) # make a copy of the dict to not modify it

# prepare view args by evaluating complex predicates
Expand Down
14 changes: 13 additions & 1 deletion cornice/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,10 @@ def test_content_type_missing(self):

# Requesting without a Content-Type header should
# return "415 Unsupported Media Type" ...
request = app.RequestClass.blank('/service5', method='POST')
request = app.RequestClass.blank('/service5', method='POST',
POST="some data")
response = app.do_request(request, 415, True)
self.assertEqual(response.status_code, 415)

# ... with an appropriate json error structure.
error_location = response.json['errors'][0]['location']
Expand All @@ -200,6 +202,16 @@ def test_content_type_missing(self):
self.assertEqual('Content-Type', error_name)
self.assertIn('application/json', error_description)

def test_content_type_missing_with_no_body_should_pass(self):
# test that a Content-Type request headers is present
app = TestApp(main({}))

# requesting without a Content-Type header nor a body should
# return a 200.
request = app.RequestClass.blank('/service5', method='POST')
response = app.do_request(request, 200, True)
self.assertEqual(response.status_code, 200)

def test_content_type_wrong_single(self):
# Tests that the Content-Type request header satisfies the requirement.
app = TestApp(main({}))
Expand Down

0 comments on commit 4d99a3d

Please sign in to comment.