def __call__(self, req): serializer = serializers.JSONResponseSerializer() resp = webob.Response(request=req) default_webob_exc = webob.exc.HTTPInternalServerError() resp.status_code = self.error.get('code', default_webob_exc.code) serializer.default(resp, self.error) return resp
def test_to_json_with_date_format_value(self): test_date = tu.parse_strtime("0001-03-08T02:00:00", '%Y-%m-%dT%H:%M:%S') fixture = {"date": test_date} expected = '{"date": "0001-03-08T02:00:00"}' actual = serializers.JSONResponseSerializer().to_json(fixture) self.assertEqual(expected, actual)
def __init__(self, controller): """Initializer. :param controller: object that implement methods created by routes lib """ self.controller = controller self.deserializer = serializers.JSONRequestDeserializer() self.serializer = serializers.JSONResponseSerializer()
def test_default(self): fixture = {"key": "value"} response = webob.Response() serializers.JSONResponseSerializer().default(response, fixture) self.assertEqual(200, response.status_int) content_types = [h for h in response.headerlist if h[0] == 'Content-Type'] # NOTE: filter returns a iterator in python 3. types = [t for t in content_types] self.assertEqual(1, len(types)) self.assertEqual('application/json', response.content_type) self.assertEqual('{"key": "value"}', encodeutils.safe_decode(response.body))
def test_to_json_with_more_deep_format(self): val = complex(1, 2) fixture = {"is_public": True, "v": val} expected = '{"is_public": true, "v": "(1+2j)"}' actual = serializers.JSONResponseSerializer().to_json(fixture) self.assertEqual(expected, actual)
def test_to_json(self): fixture = {"key": "value"} expected = '{"key": "value"}' actual = serializers.JSONResponseSerializer().to_json(fixture) self.assertEqual(expected, actual)