Example #1
0
    def test_passing_arguments_object(self, app):
        req = Request.from_values('/bubble?foo=bar')
        parser = RequestParser()
        parser.add_argument(Argument('foo'))

        args = parser.parse_args(req)
        assert args['foo'] == 'bar'
Example #2
0
    def test_parse_unicode(self, app):
        req = Request.from_values('/bubble?foo=barß')
        parser = RequestParser()
        parser.add_argument('foo')

        args = parser.parse_args(req)
        assert args['foo'] == 'barß'
Example #3
0
    def test_parse_unicode_app(self, app):
        parser = RequestParser()
        parser.add_argument('foo')

        with app.test_request_context('/bubble?foo=barß'):
            args = parser.parse_args()
            assert args['foo'] == 'barß'
Example #4
0
    def test_parse_gte_lte_eq(self):
        parser = RequestParser()
        parser.add_argument('foo',
                            operators=['>=', '<=', '='],
                            action='append'),

        args = parser.parse_args()
        assert args['foo'] == ['bar', 'bat', 'foo']
Example #5
0
 def test_location_form(self):
     parser = RequestParser()
     parser.add_argument('in_form', type=int, location='form')
     assert parser.__schema__ == [{
         'name': 'in_form',
         'type': 'integer',
         'in': 'formData',
     }]
Example #6
0
 def test_location_files(self):
     parser = RequestParser()
     parser.add_argument('in_files', type=FileStorage, location='files')
     assert parser.__schema__ == [{
         'name': 'in_files',
         'type': 'file',
         'in': 'formData',
     }]
Example #7
0
    def test_type_callable(self, app):
        req = Request.from_values('/bubble?foo=1')

        parser = RequestParser()
        parser.add_argument('foo', type=lambda x: x, required=False),

        args = parser.parse_args(req)
        assert args['foo'] == '1'
Example #8
0
    def test_parse_ignore(self, app):
        req = Request.from_values('/bubble?foo=bar')

        parser = RequestParser()
        parser.add_argument('foo', type=int, ignore=True, store_missing=True),

        args = parser.parse_args(req)
        assert args['foo'] is None
Example #9
0
 def test_location_json(self):
     parser = RequestParser()
     parser.add_argument('in_json', type=str, location='json')
     assert parser.__schema__ == [{
         'name': 'in_json',
         'type': 'string',
         'in': 'body',
     }]
Example #10
0
    def test_parse_choices_correct(self, app):
        req = Request.from_values('/bubble?foo=bat')

        parser = RequestParser()
        parser.add_argument('foo', choices=['bat']),

        args = parser.parse_args(req)
        assert args['foo'] == 'bat'
Example #11
0
    def test_parse_store_missing(self, app):
        req = Request.from_values('/bubble')

        parser = RequestParser()
        parser.add_argument('foo', store_missing=False)

        args = parser.parse_args(req)
        assert 'foo' not in args
Example #12
0
 def test_unknown_type(self):
     parser = RequestParser()
     parser.add_argument('unknown', type=lambda v: v)
     assert parser.__schema__ == [{
         'name': 'unknown',
         'type': 'string',
         'in': 'query',
     }]
Example #13
0
 def test_required(self):
     parser = RequestParser()
     parser.add_argument('int', type=int, required=True)
     assert parser.__schema__ == [{
         'name': 'int',
         'type': 'integer',
         'in': 'query',
         'required': True,
     }]
Example #14
0
    def test_request_parser_remove_argument(self):
        req = Request.from_values('/bubble?foo=baz')
        parser = RequestParser()
        parser.add_argument('foo', type=int)
        parser_copy = parser.copy()
        parser_copy.remove_argument('foo')

        args = parser_copy.parse_args(req)
        assert args == {}
Example #15
0
 def test_default(self):
     parser = RequestParser()
     parser.add_argument('int', type=int, default=5)
     assert parser.__schema__ == [{
         'name': 'int',
         'type': 'integer',
         'in': 'query',
         'default': 5,
     }]
Example #16
0
 def test_default_as_false(self):
     parser = RequestParser()
     parser.add_argument('bool', type=inputs.boolean, default=False)
     assert parser.__schema__ == [{
         'name': 'bool',
         'type': 'boolean',
         'in': 'query',
         'default': False,
     }]
Example #17
0
 def test_none_argument(self, app):
     parser = RequestParser()
     parser.add_argument('foo', location='json')
     with app.test_request_context('/bubble',
                                   method='post',
                                   data=json.dumps({'foo': None}),
                                   content_type='application/json'):
         args = parser.parse_args()
     assert args['foo'] is None
Example #18
0
    def test_parse_default_append(self):
        parser = RequestParser()
        parser.add_argument('foo',
                            default='bar',
                            action='append',
                            store_missing=True)

        args = parser.parse_args()

        assert args['foo'] == 'bar'
Example #19
0
    def test_parse_append_ignore(self, app):
        parser = RequestParser()
        parser.add_argument('foo',
                            ignore=True,
                            type=int,
                            action='append',
                            store_missing=True),

        args = parser.parse_args()
        assert args['foo'] is None
Example #20
0
 def test_choices(self):
     parser = RequestParser()
     parser.add_argument('string', type=str, choices=['a', 'b'])
     assert parser.__schema__ == [{
         'name': 'string',
         'type': 'string',
         'in': 'query',
         'enum': ['a', 'b'],
         'collectionFormat': 'multi',
     }]
Example #21
0
    def test_type_decimal(self, app):
        parser = RequestParser()
        parser.add_argument('foo', type=decimal.Decimal, location='json')

        with app.test_request_context('/bubble',
                                      method='post',
                                      data=json.dumps({'foo': '1.0025'}),
                                      content_type='application/json'):
            args = parser.parse_args()
            assert args['foo'] == decimal.Decimal('1.0025')
Example #22
0
 def test_both_json_and_values_location(self, app):
     parser = RequestParser()
     parser.add_argument('foo', type=int)
     parser.add_argument('baz', type=int)
     with app.test_request_context('/bubble?foo=1',
                                   method='post',
                                   data=json.dumps({'baz': 2}),
                                   content_type='application/json'):
         args = parser.parse_args()
         assert args['foo'] == 1
         assert args['baz'] == 2
Example #23
0
 def test_models(self):
     todo_fields = Model('Todo', {
         'task':
         fields.String(required=True, description='The task details')
     })
     parser = RequestParser()
     parser.add_argument('todo', type=todo_fields)
     assert parser.__schema__ == [{
         'name': 'todo',
         'type': 'Todo',
         'in': 'body',
     }]
Example #24
0
 def test_split_lists(self):
     parser = RequestParser()
     parser.add_argument('int', type=int, action='split')
     assert parser.__schema__ == [{
         'name': 'int',
         'in': 'query',
         'type': 'array',
         'collectionFormat': 'csv',
         'items': {
             'type': 'integer'
         }
     }]
Example #25
0
    def test_type_callable_none(self, app):
        parser = RequestParser()
        parser.add_argument('foo',
                            type=lambda x: x,
                            location='json',
                            required=False),

        with app.test_request_context('/bubble',
                                      method='post',
                                      data=json.dumps({'foo': None}),
                                      content_type='application/json'):
            args = parser.parse_args()
            assert args['foo'] is None
Example #26
0
    def test_parse_model(self, app):
        model = Model('Todo', {'task': fields.String(required=True)})

        parser = RequestParser()
        parser.add_argument('todo', type=model, required=True)

        data = {'todo': {'task': 'aaa'}}

        with app.test_request_context('/',
                                      method='post',
                                      data=json.dumps(data),
                                      content_type='application/json'):
            args = parser.parse_args()
            assert args['todo'] == {'task': 'aaa'}
Example #27
0
    def test_type_filestorage(self, app):
        parser = RequestParser()
        parser.add_argument('foo', type=FileStorage, location='files')

        fdata = 'foo bar baz qux'.encode('utf-8')
        with app.test_request_context(
                '/bubble',
                method='POST',
                data={'foo': (six.BytesIO(fdata), 'baz.txt')}):
            args = parser.parse_args()

            assert args['foo'].name == 'foo'
            assert args['foo'].filename == 'baz.txt'
            assert args['foo'].read() == fdata
Example #28
0
    def test_viewargs(self, mocker):
        req = Request.from_values()
        req.view_args = {'foo': 'bar'}
        parser = RequestParser()
        parser.add_argument('foo', location=['view_args'])
        args = parser.parse_args(req)
        assert args['foo'] == 'bar'

        req = mocker.Mock()
        req.values = ()
        req.json = None
        req.view_args = {'foo': 'bar'}
        parser = RequestParser()
        parser.add_argument('foo', store_missing=True)
        args = parser.parse_args(req)
        assert args['foo'] is None
Example #29
0
    def test_files_and_body_location(self):
        parser = RequestParser()
        parser.add_argument('default', type=int)
        parser.add_argument('in_files', type=FileStorage, location='files')
        parser.add_argument('in_json', type=str, location='json')
        with pytest.raises(SpecsError) as cm:
            parser.__schema__

        assert cm.value.msg == "Can't use formData and body at the same time"
Example #30
0
    def test_parse_choices_insensitive(self, app):
        req = Request.from_values('/bubble?foo=BAT')

        parser = RequestParser()
        parser.add_argument('foo', choices=['bat'], case_sensitive=False),

        args = parser.parse_args(req)
        assert 'bat' == args.get('foo')

        # both choices and args are case_insensitive
        req = Request.from_values('/bubble?foo=bat')

        parser = RequestParser()
        parser.add_argument('foo', choices=['BAT'], case_sensitive=False),

        args = parser.parse_args(req)
        assert 'bat' == args.get('foo')