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 906c1fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion backdrop/write/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ 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
17 changes: 12 additions & 5 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,19 +69,23 @@ 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:
message = "any"
else:
message = '"{}"'.format(self.message)
description.append_text(
'error response'
'error response with {} error message'.format(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):
return datetime.datetime(year, month, day, hour, minute, seconds,
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 906c1fb

Please sign in to comment.