Skip to content

Commit

Permalink
Remove all extra fields and create status field, Closes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
mehrdad1373pedramfar committed Jun 9, 2018
1 parent 7cdfeaf commit 2367704
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
49 changes: 19 additions & 30 deletions nanohttp/exceptions.py
Expand Up @@ -11,69 +11,58 @@ class HttpStatus(Exception):
status_text = None
info = None

def __init__(self, message=None, reason=None, info=None, status_code=None, status_text=None):
if info is not None:
self.info = info
def __init__(self, status=None, reason=None):

if status_code is not None:
self.status_code = status_code

if status_text is not None:
self.status_text = status_text
if status is not None:
self.status = status

if reason:
context.response_headers.add_header('X-Reason', reason)

super().__init__(message or self.status_text)

@property
def status(self):
return '%s %s' % (self.status_code, self.status_text)
super().__init__(self.status)

def to_dict(self):
return dict(
description=self.info
)
return dict()

def render(self):
if context.response_content_type == 'application/json':
return ujson.encode(self.to_dict())
else:
context.response_encoding = 'utf-8'
context.response_content_type = 'text/plain'
return "%s\n%s" % (self.status_text, self.info)
return self.status


class HttpBadRequest(HttpStatus):
status_code, status_text, info = 400, 'Bad Request', 'Bad request syntax or unsupported method'
status = '400 Bad Request'


class HttpUnauthorized(HttpStatus):
status_code, status_text, info = 401, 'Unauthorized', 'No permission -- see authorization schemes'
status = '401 Unauthorized'


class HttpForbidden(HttpStatus):
status_code, status_text, info = 403, 'Forbidden', 'Request forbidden -- authorization will not help'
status = '403 Forbidden'


class HttpNotFound(HttpStatus):
status_code, status_text, info = 404, 'Not Found', 'Nothing matches the given URI'
status = '404 Not Found'


class HttpMethodNotAllowed(HttpStatus):
status_code, status_text, info = 405, 'Method Not Allowed', 'Specified method is invalid for this resource'
status = '405 Method Not Allowed'


class HttpConflict(HttpStatus):
status_code, status_text, info = 409, 'Conflict', 'Request conflict'
status = '409 Conflict'


class HttpGone(HttpStatus):
status_code, status_text, info = 410, 'Gone', 'URI no longer exists and has been permanently removed'
status = '410 Gone'


class HttpPreconditionFailed(HttpStatus):
status_code, status_text, info = 412, 'Precondition Failed', 'Request cannot be fulfilled'
status = '412 Precondition Failed'


class HttpRedirect(HttpStatus):
Expand All @@ -87,19 +76,19 @@ def __init__(self, location, *args, **kw):


class HttpMovedPermanently(HttpRedirect):
status_code, status_text, info = 301, 'Moved Permanently', 'Object moved permanently'
status = '301 Moved Permanently'


class HttpFound(HttpRedirect):
status_code, status_text, info = 302, 'Found', 'Object moved temporarily'
status = '302 Found'


class HttpNotModified(HttpStatus):
status_code, status_text, info = 304, 'Not Modified', '' # 304 is only header
status = '304 Not Modified'


class HttpInternalServerError(HttpStatus):
status_code, status_text = 500, 'Internal Server Error'
status = '500 Internal Server Error'

@property
def info(self):
Expand All @@ -109,4 +98,4 @@ def info(self):


class HttpBadGatewayError(HttpStatus):
status_code, status_text = 502, 'Bad Gateway'
status = '502 Bad Gateway'
2 changes: 1 addition & 1 deletion nanohttp/helpers.py
Expand Up @@ -143,7 +143,7 @@ def parse_any_form(environ, content_length=None, content_type=None):
keep_blank_values=True
)
except TypeError:
raise exceptions.HttpBadRequest('Cannot parse the request.')
raise exceptions.HttpBadRequest('400 Cannot parse the request.')

result = {}
if storage.list is None or not len(storage.list):
Expand Down
18 changes: 11 additions & 7 deletions nanohttp/tests/test_exceptions.py
Expand Up @@ -20,7 +20,7 @@ def data(self):

@json
def custom(self):
raise HttpStatus(status_code=462, status_text='custom text', info='custom info')
raise HttpStatus(status='462 custom text')

@html
def err(self):
Expand All @@ -35,19 +35,23 @@ def test_reason(self):
response, content = self.assert_get('/data', status=400)
self.assertIn('x-reason', response)
self.assertEqual(response['x-reason'], 'blah blah')
self.assertDictEqual(ujson.loads(content), {
'description': 'Bad request syntax or unsupported method',
})

# @Arash Fatahzade Consider this while changing
# self.assertDictEqual(ujson.loads(content), {
# 'description': 'Bad request syntax or unsupported method',
# })

response, content = self.assert_get('/err', status=500)
self.assertIsNotNone(content)
self.assertIsNotNone(response.reason)

def test_custom_exception(self):
response, content = self.assert_get('/custom', status=462)
self.assertDictEqual(ujson.loads(content), {
'description': 'custom info',
})

# @Arash Fatahzade Consider this while changing
# self.assertDictEqual(ujson.loads(content), {
# 'description': 'custom info',
# })


if __name__ == '__main__': # pragma: no cover
Expand Down
7 changes: 2 additions & 5 deletions nanohttp/tests/test_validation.py
Expand Up @@ -205,14 +205,11 @@ def test_validation_custom_status(self):
try:
validator(dict(param1='NotInteger'))
except HttpStatus as e:
self.assertEqual(e.status_code, 999)
self.assertEqual(e.status_text, 'Type error')

self.assertEqual(e.status, '999 Type error')
try:
validator(dict(param1=29))
except HttpStatus as e:
self.assertEqual(e.status_code, 666)
self.assertEqual(e.status_text, 'Bad request')
self.assertEqual(e.status, '666 Bad request')


class ValidationDecoratorTestCase(WsgiAppTestCase):
Expand Down
3 changes: 1 addition & 2 deletions nanohttp/validation.py
Expand Up @@ -87,8 +87,7 @@ def create_exception(self):
return HttpBadRequest

return HttpStatus(
status_code=self.status_code,
status_text=self.status_text
status='%s %s' % (self.status_code, self.status_text)
)


Expand Down

0 comments on commit 2367704

Please sign in to comment.