Esempio n. 1
0
 def test_accept_handler_with_mix_value(self):
     accept_handlers_list = [
         ('application/json', lambda req, res, ct: None),
         ('application/xhtml', lambda req, res, ct: HttpResponse(dumps({'fake_fn2': True}), 'text/plain', status=200))
     ]
     fake_accept_str = 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'
     content_type, handler = accept_handler.accept(fake_accept_str, accept_handlers_list)
     expect_http_response = handler(self.request, self.response, self.request.META.get('CONTENT_TYPE', None))
     self.assertTrue(isinstance(expect_http_response, HttpResponse))
     self.assertEqual(loads(expect_http_response.content), {'fake_fn2': True})
Esempio n. 2
0
 def test_accept_handler_with_content(self):
     test_str = {"test_value": "hello"}
     accept_handlers_list = [
         ("application/json", lambda req, res, ct: HttpResponse(dumps(test_str), "text/plain", status=200))
     ]
     fake_accept_str = "text/*; application/json;"
     content_type, handler = accept_handler.accept(fake_accept_str, accept_handlers_list)
     expect_http_response = handler(self.request, self.response, self.request.META.get("CONTENT_TYPE", None))
     self.assertTrue(isinstance(expect_http_response, HttpResponse))
     self.assertEqual(loads(expect_http_response.content), test_str)
Esempio n. 3
0
 def test_accept_handler_with_content(self):
     test_str = {'test_value': 'hello'}
     accept_handlers_list = [
         ('application/json', lambda req, res, ct: HttpResponse(dumps(test_str), 'text/plain', status=200))
     ]
     fake_accept_str = 'text/*; application/json;'
     content_type, handler = accept_handler.accept(fake_accept_str, accept_handlers_list)
     expect_http_response = handler(self.request, self.response, self.request.META.get('CONTENT_TYPE', None))
     self.assertTrue(isinstance(expect_http_response, HttpResponse))
     self.assertEqual(loads(expect_http_response.content), test_str)
Esempio n. 4
0
 def test_accept_handler_with_mix_value(self):
     accept_handlers_list = [
         ("application/json", lambda req, res, ct: None),
         (
             "application/xhtml",
             lambda req, res, ct: HttpResponse(dumps({"fake_fn2": True}), "text/plain", status=200),
         ),
     ]
     fake_accept_str = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
     content_type, handler = accept_handler.accept(fake_accept_str, accept_handlers_list)
     expect_http_response = handler(self.request, self.response, self.request.META.get("CONTENT_TYPE", None))
     self.assertTrue(isinstance(expect_http_response, HttpResponse))
     self.assertEqual(loads(expect_http_response.content), {"fake_fn2": True})
Esempio n. 5
0
 def test_accept_handler(self):
     content_type, handler = accept_handler.accept(self.request.META['HTTP_ACCEPT'])
     expect_http_response = handler(self.request, self.response, self.request.META.get('CONTENT_TYPE', None))
     self.assertTrue(isinstance(expect_http_response, HttpResponse))
Esempio n. 6
0
 def test_accept_handler(self):
     content_type, handler = accept_handler.accept(self.request.META["HTTP_ACCEPT"])
     expect_http_response = handler(self.request, self.response, self.request.META.get("CONTENT_TYPE", None))
     self.assertTrue(isinstance(expect_http_response, HttpResponse))
Esempio n. 7
0
                'error':
                'No user is logged in'
            }
        except Forbidden, exception:
            response = {
                '_meta': dict(status=403, message='Forbidden'),
                'error': unicode(exception)
            }
        except ObjectDoesNotExist, exception:
            response = {
                '_meta': dict(status=404, message='Not Found'),
                'error': unicode(exception)
            }
        except NotImplementedError, _:
            response = {
                '_meta': dict(status=501, message='Not Implemented'),
                'error': "Not implemented"
            }
        if request.user.is_authenticated():
            response['_meta']['username'] = request.user.username
        else:
            logging.debug("Request user %s not authenticated", request.user)
        accepting = meta.get('HTTP_ACCEPT', 'text/plain')
        content_type, handler = accept_handler.accept(accepting)
        http_response = handler(request, response, content_type)
        for header, value in response['_meta'].get('headers', {}).items():
            http_response[header] = value
        return http_response

    return wrapper if not USE_CSRF else csrf_exempt(wrapper)
Esempio n. 8
0
            http_response = view(request, response, *args, **kwargs)
            if http_response:
                return http_response
        except NotAuthorised, _:
            response = {
                '_meta': dict(status=401, message='Unauthorized',
                    headers= {'WWW-Authenticate':'FOST Realm="Slumber"'}),
                'error': 'No user is logged in'}
        except Forbidden, exception:
            response = {'_meta': dict(status=403, message='Forbidden'),
                'error': unicode(exception)}
        except ObjectDoesNotExist, exception:
            response = {'_meta': dict(status=404, message='Not Found'),
                'error': unicode(exception)}
        except NotImplementedError, _:
            response = {
                '_meta': dict(status=501, message='Not Implemented'),
                'error': "Not implemented"}
        if request.user.is_authenticated():
            response['_meta']['username'] = request.user.username
        else:
            logging.debug("Request user %s not authenticated", request.user)
        accepting = meta.get('HTTP_ACCEPT', 'text/plain')
        content_type, handler = accept_handler.accept(accepting)
        http_response = handler(request, response, content_type)
        for header, value in response['_meta'].get('headers', {}).items():
            http_response[header] = value
        return http_response
    return wrapper if not USE_CSRF else csrf_exempt(wrapper)