Skip to content
This repository has been archived by the owner on Jan 25, 2018. It is now read-only.

Commit

Permalink
Merge pull request #466 from mstriemer/json-error-messages-1013440
Browse files Browse the repository at this point in the history
Show errors as JSON if application/json is in Accept (bug 1013440)
  • Loading branch information
mstriemer committed May 23, 2014
2 parents 3869678 + 235dccb commit 541c099
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions webpay/api/tests/test_api.py
Expand Up @@ -261,13 +261,13 @@ def setUp(self):
'notes': {}
}

def post(self, data=None, req=None, mcc='423', mnc='555'):
def post(self, data=None, req=None, mcc='423', mnc='555', **kwargs):
if data is None:
data = {}
if req is None:
req = self.request()
data = {'req': req, 'mnc': mnc, 'mcc': mcc}
return self.client.post(self.url, data=data)
return self.client.post(self.url, data=data, **kwargs)

def test_bad(self):
res = self.post(data={})
Expand All @@ -291,9 +291,12 @@ def test_stores_mnc_mcc(self):
args = self.start_pay.delay.call_args[0][1]
eq_(args['network'], {'mnc': '423', 'mcc': '555'})

def test_invalid__mcc(self):
res = self.post(mcc='abc')
def test_invalid_mcc(self):
accept = 'application/json, text/javascript, */*; q=0.01'
res = self.post(mcc='abc', HTTP_ACCEPT=accept)
eq_(res.status_code, 400)
errors = json.loads(res.content)
ok_('error_code' in errors)

def test_missing_mcc(self):
res = self.post(mcc=None)
Expand Down
2 changes: 1 addition & 1 deletion webpay/base/utils.py
Expand Up @@ -67,7 +67,7 @@ def system_error(request, **kw):

def custom_error(request, user_message, code=None, status=400):
error = {'error': user_message, 'error_code': code}
if request.META.get('HTTP_ACCEPT') == 'application/json':
if 'application/json' in request.META.get('HTTP_ACCEPT'):
return HttpResponse(
content=json.dumps(error),
content_type='application/json; charset=utf-8',
Expand Down

0 comments on commit 541c099

Please sign in to comment.