Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Better content header error and slight refactor of test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbashdown committed May 7, 2014
1 parent 4ef0f61 commit dc91902
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion backdrop/write/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ def _empty_data_set(data_set_config):

def listify_json(data):
if data is None:
raise ValidationError("Request must be JSON")
raise ValidationError(
"Expected header: Content-type: application/json")

if isinstance(data, list):
return data
Expand Down
16 changes: 13 additions & 3 deletions tests/support/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def has_header(name, value):


class IsErrorResponse(BaseMatcher):
def __init__(self, message):
self.message = message

def _matches(self, response):
try:
data = json.loads(response.data)
Expand All @@ -66,18 +69,25 @@ def _matches(self, response):
# it should not fail with out a message
if not data.get('message'):
return False
if self.message and not data.get('message') == self.message:
return False
return True
except ValueError:
return False

def describe_to(self, description):
if not self.message:
description_message = 'error response with any error message'
else:
description_message = 'error response with message: "{}"'.format(
self.message)
description.append_text(
'error response'
description_message
)


def is_error_response():
return IsErrorResponse()
def is_error_response(message=None):
return IsErrorResponse(message)


def d_tz(year, month, day, hour=0, minute=0, seconds=0, tzinfo=None):
Expand Down
2 changes: 1 addition & 1 deletion tests/write/test_flask_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_request_must_be_json(self):
)

assert_that( response, is_bad_request())
assert_that( response, is_error_response())
assert_that( response, is_error_response("ValidationError('Expected header: Content-type: application/json',)"))

@fake_data_set_exists("foo_data_set", bearer_token="foo_data_set-bearer-token")
@patch("backdrop.core.data_set.DataSet.store")
Expand Down

0 comments on commit dc91902

Please sign in to comment.