Exemplo n.º 1
0
 def test_marshal_list_of_lists(self):
     fields = OrderedDict([
         ('foo', flask_restful.fields.Raw),
         ('fee', flask_restful.fields.List(flask_restful.fields.List(
             flask_restful.fields.String)))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', [['fye'], ['fum']])])
     output = flask_restful.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'bar'), ('fee', [['fye'], ['fum']])])
     self.assertEquals(output, expected)
Exemplo n.º 2
0
 def test_marshal_list_of_nesteds(self):
     fields = {
         'foo': flask_restful.fields.Raw,
         'fee': flask_restful.fields.List(flask_restful.fields.Nested({
             'fye': flask_restful.fields.String
         }))
     }
     output = flask_restful.marshal([{'foo': 'bar', 'bat': 'baz',
         'fee': {'fye': 'fum'}}], fields)
     self.assertEquals(output, [OrderedDict({'fee': [OrderedDict({'fye': 'fum'})],
         'foo': 'bar'})])
Exemplo n.º 3
0
 def test_marshal_list_of_nesteds(self):
     fields = OrderedDict([
         ('foo', flask_restful.fields.Raw),
         ('fee', flask_restful.fields.List(flask_restful.fields.Nested({
             'fye': flask_restful.fields.String
         })))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', {'fye': 'fum'})])
     output = flask_restful.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'bar'), ('fee', [OrderedDict([('fye', 'fum')])])])
     self.assertEquals(output, expected)
Exemplo n.º 4
0
 def test_marshal_nested_dict(self):
     fields = OrderedDict([
         ('foo', flask_restful.fields.Raw),
         ('bar', OrderedDict([
             ('a', flask_restful.fields.Raw),
             ('b', flask_restful.fields.Raw),
         ])),
     ])
     marshal_fields = OrderedDict([('foo', 'foo-val'), ('bar', 'bar-val'), ('bat', 'bat-val'),
                                   ('a', 1), ('b', 2), ('c', 3)])
     output = flask_restful.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'foo-val'), ('bar', OrderedDict([('a', 1), ('b', 2)]))])
     self.assertEquals(output, expected)
Exemplo n.º 5
0
 def test_marshal_nested_with_null(self):
     fields = OrderedDict([('foo', flask_restful.fields.Raw),
                           ('fee',
                            flask_restful.fields.Nested(OrderedDict([
                                ('fye', flask_restful.fields.String),
                                ('blah', flask_restful.fields.String)
                            ]),
                                                        allow_null=True))])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', None)])
     output = flask_restful.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'bar'), ('fee', None)])
     self.assertEquals(output, expected)
Exemplo n.º 6
0
 def test_allow_null_presents_data(self):
     fields = OrderedDict([
         ('foo', flask_restful.fields.Raw),
         ('fee', flask_restful.fields.Nested(
             OrderedDict([
                 ('fye', flask_restful.fields.String),
                 ('blah', flask_restful.fields.String)
             ]), allow_null=True))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', {'blah': 'cool'})])
     output = flask_restful.marshal(marshal_fields, fields)
     expected = OrderedDict([('foo', 'bar'), ('fee', OrderedDict([('fye', None), ('blah', 'cool')]))])
     self.assertEquals(output, expected)
Exemplo n.º 7
0
    def test_marshal_decorator(self):
        fields = OrderedDict([('foo', flask_restful.fields.Raw)])

        @flask_restful.marshal_with(fields)
        def try_me():
            return OrderedDict([('foo', 'bar'), ('bat', 'baz')])
        self.assertEquals(try_me(), {'foo': 'bar'})
Exemplo n.º 8
0
 def test_marshal_nested_dict(self):
     fields = {
         'foo': flask_restful.fields.Raw,
         'bar': {
             'a': flask_restful.fields.Raw,
             'b': flask_restful.fields.Raw,
         },
     }
     output = flask_restful.marshal(
         {
             'foo': 'foo-val',
             'bar': 'bar-val',
             'bat': 'bat-val',
             'a': 1,
             'b': 2,
             'c': 3
         }, fields)
     self.assertEquals(
         output, OrderedDict({
             'foo': 'foo-val',
             'bar': {
                 'a': 1,
                 'b': 2
             }
         }))
Exemplo n.º 9
0
    def test_marshal_decorator_tuple(self):
        fields = OrderedDict([('foo', flask_restful.fields.Raw)])

        @flask_restful.marshal_with(fields)
        def try_me():
            return OrderedDict([('foo', 'bar'), ('bat', 'baz')]), 200, {'X-test': 123}
        self.assertEquals(try_me(), ({'foo': 'bar'}, 200, {'X-test': 123}))
Exemplo n.º 10
0
    def setUp(self):
        self.app = Flask(__name__)
        self.api = flask_restful.Api(self.app)

        self.app_401 = Flask(__name__)
        self.api_401 = flask_restful.Api(self.app, serve_challenge_on_401=True)

        self.fields = OrderedDict([('foo', flask_restful.fields.Raw)])
Exemplo n.º 11
0
    def test_marshal_decorator_with_envelope(self):
        fields = OrderedDict([('foo', flask_restful.fields.Raw)])

        @flask_restful.marshal_with(fields, envelope='hey')
        def try_me():
            return OrderedDict([('foo', 'bar'), ('bat', 'baz')])

        self.assertEqual(try_me(), {'hey': {'foo': 'bar'}})
Exemplo n.º 12
0
 def test_marshal_nested_with_non_null_and_no_display_empty(self):
     fields = OrderedDict([('foo', flask_restful.fields.Raw),
                           ('fee',
                            flask_restful.fields.Nested(OrderedDict([
                                ('fye', flask_restful.fields.String),
                                ('blah', flask_restful.fields.String)
                            ]),
                                                        allow_null=False,
                                                        display_null=False))
                           ])
     marshal_fields = [
         OrderedDict([('foo', 'bar'), ('bat', 'baz'), ('fee', None)])
     ]
     output = flask_restful.marshal(marshal_fields,
                                    fields,
                                    display_null=False)
     expected = [OrderedDict([('foo', 'bar'), ('fee', OrderedDict([]))])]
     self.assertEquals(output, expected)
Exemplo n.º 13
0
 def test_marshal_list(self):
     fields = {
         'foo': flask_restful.fields.Raw,
         'fee': flask_restful.fields.List(flask_restful.fields.String)
     }
     output = flask_restful.marshal([{'foo': 'bar', 'bat': 'baz',
         'fee': ['fye', 'fum']}], fields)
     self.assertEquals(output, [OrderedDict({'fee': (['fye', 'fum']),
         'foo': 'bar'})])
Exemplo n.º 14
0
 def test_marshal_nested_property(self):
     class TestObject(object):
         @property
         def fee(self):
             return {'blah': 'cool'}
     fields = OrderedDict([
         ('foo', flask_restful.fields.Raw),
         ('fee', flask_restful.fields.Nested(
             OrderedDict([
                 ('fye', flask_restful.fields.String),
                 ('blah', flask_restful.fields.String)
             ]), allow_null=True))
     ])
     obj = TestObject()
     obj.foo = 'bar'
     obj.bat = 'baz'
     output = flask_restful.marshal([obj], fields)
     expected = [OrderedDict([('foo', 'bar'), ('fee', OrderedDict([('fye', None), ('blah', 'cool')]))])]
     self.assertEquals(output, expected)
Exemplo n.º 15
0
 def test_marshal_nested(self):
     fields = {
         'foo':
         flask_restful.fields.Raw,
         'fee':
         flask_restful.fields.Nested({
             'fye': flask_restful.fields.String,
         })
     }
     output = flask_restful.marshal([{
         'foo': 'bar',
         'bat': 'baz',
         'fee': {
             'fye': 'fum'
         }
     }], fields)
     self.assertEquals(output, [{
         'fee': OrderedDict({'fye': 'fum'}),
         'foo': 'bar'
     }])
Exemplo n.º 16
0
    def dispatch_request(self, *args, **kwargs):

        # Taken from flask
        #noinspection PyUnresolvedReferences
        meth = getattr(self, request.method.lower(), None)
        if meth is None and request.method == 'HEAD':
            meth = getattr(self, 'get', None)
        assert meth is not None, 'Unimplemented method %r' % request.method

        for decorator in self.method_decorators:
            meth = decorator(meth)

        resp = meth(*args, **kwargs)

        # adds default success key
        if isinstance(resp, dict):
            if resp.get('success', None) is None:
                resp['success'] = True
            elif resp.get('success', True) is False:
                resp = self.__serialize_errors(resp)
        elif isinstance(resp, tuple):
            if resp[0].get('success', True) is False:
                list(resp)[0] = self.__serialize_errors(resp[0])
                resp = tuple(resp)

        if isinstance(resp, ResponseBase):  # There may be a better way to test
            return resp

        representations = self.representations or OrderedDict()

        #noinspection PyUnresolvedReferences
        mediatype = request.accept_mimetypes.best_match(representations,
                                                        default=None)
        if mediatype in representations:
            data, code, headers = unpack(resp)
            resp = representations[mediatype](data, code, headers)
            resp.headers['Content-Type'] = mediatype
            return resp

        return resp
Exemplo n.º 17
0
 def test_marshal(self):
     fields = OrderedDict([('foo', flask_restful.fields.Raw)])
     marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = flask_restful.marshal(marshal_dict, fields)
     self.assertEquals(output, {'foo': 'bar'})
Exemplo n.º 18
0
 def test_marshal_field(self):
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = flask_restful.marshal(marshal_fields, self.fields)
     self.assertEquals(output, {'foo': 'bar'})
Exemplo n.º 19
0
 def test_marshal_tuple(self):
     fields = OrderedDict({'foo': flask_restful.fields.Raw})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = flask_restful.marshal((marshal_fields, ), fields)
     self.assertEquals(output, [{'foo': 'bar'}])
Exemplo n.º 20
0
 def try_me():
     return OrderedDict([('foo', 'bar'), ('bat', 'baz')]), 200, {
         'X-test': 123
     }
Exemplo n.º 21
0
 def try_me():
     return OrderedDict([('foo', 'bar'), ('bat', 'baz')])
Exemplo n.º 22
0
 def test_marshal_with_envelope(self):
     fields = OrderedDict([('foo', flask_restful.fields.Raw)])
     marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = flask_restful.marshal(marshal_dict, fields, envelope='hey')
     self.assertEquals(output, {'hey': {'foo': 'bar'}})
Exemplo n.º 23
0
 def test_marshal_tuple_with_envelope(self):
     fields = OrderedDict({'foo': flask_restful.fields.Raw})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = flask_restful.marshal((marshal_fields,), fields, envelope='hey')
     self.assertEquals(output, {'hey': [{'foo': 'bar'}]})