flask_restplus.reqparse.RequestParser

Here are the examples of the python api flask_restplus.reqparse.RequestParser taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

80 Examples 7

Page 1 Selected Page 2

Example 1

Project: flask-restplus Source File: test_reqparse.py
Function: test_parse_unicode
    def test_parse_unicode(self):
        req = Request.from_values('/bubble?foo=barß')
        parser = RequestParser()
        parser.add_argument('foo')

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'barß')

Example 2

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_default_append(self):
        req = Request.from_values('/bubble')
        parser = RequestParser()
        parser.add_argument('foo', default='bar', action='append',
                            store_missing=True)

        args = parser.parse_args(req)

        self.assertEqual(args['foo'], 'bar')

Example 3

Project: flask-restplus Source File: test_reqparse.py
Function: test_filestorage_custom_type
    def test_filestorage_custom_type(self):
        def _custom_type(f):
            return FileStorage(stream=f.stream,
                               filename='{0}aaaa'.format(f.filename),
                               name='{0}aaaa'.format(f.name))

        parser = RequestParser()
        parser.add_argument('foo', type=_custom_type, location='files')

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

            self.assertEqual(args['foo'].name, 'fooaaaa')
            self.assertEqual(args['foo'].filename, 'baz.txtaaaa')
            self.assertEqual(args['foo'].read(), fdata)

Example 4

Project: flask-restplus Source File: test_reqparse.py
Function: test_not_json_location_and_content_type_json
    def test_not_json_location_and_content_type_json(self):
        parser = RequestParser()
        parser.add_argument('foo', location='args')

        with self.app.test_request_context('/bubble', method='get',
                                      content_type='application/json'):
            parser.parse_args()  # Should not raise a 400: BadRequest

Example 5

Project: flask-restplus Source File: test_reqparse.py
Function: test_type_callable
    def test_type_callable(self):
        req = Request.from_values('/bubble?foo=1')

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

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], '1')

Example 6

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_eq(self):
        req = Request.from_values('/bubble?foo=bar')
        parser = RequestParser()
        parser.add_argument('foo'),
        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'bar')

Example 7

Project: flask-restplus Source File: test_reqparse.py
    def test_request_parser_copy(self):
        req = Request.from_values('/bubble?foo=101&bar=baz')
        parser = RequestParser()
        foo_arg = Argument('foo', type=int)
        parser.args.append(foo_arg)
        parser_copy = parser.copy()

        # Deepcopy should create a clone of the argument object instead of
        # copying a reference to the new args list
        self.assertFalse(foo_arg in parser_copy.args)

        # Args added to new parser should not be added to the original
        bar_arg = Argument('bar')
        parser_copy.args.append(bar_arg)
        self.assertFalse(bar_arg in parser.args)

        args = parser_copy.parse_args(req)
        self.assertEqual(args['foo'], 101)
        self.assertEqual(args['bar'], 'baz')

Example 8

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_append(self):
        req = Request.from_values('/bubble?foo=bar&foo=bat')

        parser = RequestParser()
        parser.add_argument('foo', action='append'),

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], ['bar', 'bat'])

Example 9

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_dest(self):
        req = Request.from_values('/bubble?foo=bar')

        parser = RequestParser()
        parser.add_argument('foo', dest='bat')

        args = parser.parse_args(req)
        self.assertEqual(args['bat'], 'bar')

Example 10

Project: flask-restplus Source File: test_reqparse.py
Function: test_trim_argument
    def test_trim_argument(self):
        req = Request.from_values('/bubble?foo= 1 &bar=bees&n=22')
        parser = RequestParser()
        parser.add_argument('foo')
        args = parser.parse_args(req)
        self.assertEqual(args['foo'], ' 1 ')

        parser = RequestParser()
        parser.add_argument('foo', trim=True)
        args = parser.parse_args(req)
        self.assertEqual(args['foo'], '1')

        parser = RequestParser()
        parser.add_argument('foo', trim=True, type=int)
        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 1)

Example 11

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_none(self):
        req = Request.from_values('/bubble')

        parser = RequestParser()
        parser.add_argument('foo')

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], None)

Example 12

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_ignore(self):
        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)
        self.assertEqual(args['foo'], None)

Example 13

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_lte_gte_mock(self):
        mock_type = Mock()
        req = Request.from_values('/bubble?foo<=bar')

        parser = RequestParser()
        parser.add_argument('foo', type=mock_type, operators=['<='])

        parser.parse_args(req)
        mock_type.assert_called_with('bar', 'foo', '<=')

Example 14

Project: flask-restplus Source File: test_reqparse.py
Function: test_type_decimal
    def test_type_decimal(self):
        parser = RequestParser()
        parser.add_argument('foo', type=decimal.Decimal, location='json')

        with self.app.test_request_context('/bubble', method='post',
                                      data=json.dumps({'foo': '1.0025'}),
                                      content_type='application/json'):
            args = parser.parse_args()
            self.assertEqual(args['foo'], decimal.Decimal('1.0025'))

Example 15

Project: flask-restplus Source File: test_reqparse.py
Function: test_parse_append_ignore
    def test_parse_append_ignore(self):
        req = Request.from_values('/bubble?foo=bar')

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

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], None)

Example 16

Project: flask-restplus Source File: test_reqparse.py
Function: test_int_choice_types
    def test_int_choice_types(self):
        parser = RequestParser()
        parser.add_argument('foo', type=int, choices=[1, 2, 3], location='json')

        with self.app.test_request_context(
                '/bubble', method='post',
                data=json.dumps({'foo': 5}),
                content_type='application/json'
        ):
            try:
                parser.parse_args()
                self.fail()
            except BadRequest:
                pass

Example 17

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_error_bundling(self):
        self.app.config['BUNDLE_ERRORS'] = True
        req = Request.from_values('/bubble')

        parser = RequestParser()
        parser.add_argument('foo', required=True, location='values')
        parser.add_argument('bar', required=True, location=['values', 'cookies'])

        with self.app.app_context():
            try:
                parser.parse_args(req)
            except BadRequest as e:
                self.assertEqual(e.data['message'], 'Input payload validation failed')
                self.assertEqual(e.data['errors'], {
                    'foo': 'Missing required parameter in the post body or the query string',
                    'bar': ("Missing required parameter in the post body or the query string "
                            "or the request's cookies")
                })

Example 18

Project: flask-restplus Source File: test_reqparse.py
Function: test_request_parser_replace_argument
    def test_request_parser_replace_argument(self):
        req = Request.from_values('/bubble?foo=baz')
        parser = RequestParser()
        parser.add_argument('foo', type=int)
        parser_copy = parser.copy()
        parser_copy.replace_argument('foo')

        args = parser_copy.parse_args(req)
        self.assertEqual(args['foo'], 'baz')

Example 19

Project: flask-restplus Source File: test_reqparse.py
Function: test_viewargs
    def test_viewargs(self):
        req = Request.from_values()
        req.view_args = {'foo': 'bar'}
        parser = RequestParser()
        parser.add_argument('foo', location=['view_args'])
        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'bar')

        req = 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)
        self.assertEqual(args['foo'], None)

Example 20

Project: flask-restplus Source File: test_reqparse.py
Function: test_strict_parsing_off_partial_hit
    def test_strict_parsing_off_partial_hit(self):
        req = Request.from_values('/bubble?foo=1&bar=bees&n=22')
        parser = RequestParser()
        parser.add_argument('foo', type=int)
        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 1)

Example 21

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_callable_default(self):
        req = Request.from_values('/bubble')

        parser = RequestParser()
        parser.add_argument('foo', default=lambda: 'bar', store_missing=True)

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'bar')

Example 22

Project: flask-restplus Source File: test_reqparse.py
    def test_parse(self):
        req = Request.from_values('/bubble?foo=bar')

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

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'bar')

Example 23

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_choices_correct(self):
        req = Request.from_values('/bubble?foo=bat')

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

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'bat')

Example 24

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_store_missing(self):
        req = Request.from_values('/bubble')

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

        args = parser.parse_args(req)
        self.assertFalse('foo' in args)

Example 25

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_gte_lte_eq(self):
        req = Request.from_values('/bubble?foo>=bar&foo<=bat&foo=foo')

        parser = RequestParser()
        parser.add_argument('foo', operators=['>=', '<=', '='], action='append'),

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], ['bar', 'bat', 'foo'])

Example 26

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_choices_insensitive(self):
        req = Request.from_values('/bubble?foo=BAT')

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

        args = parser.parse_args(req)
        self.assertEqual('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)
        self.assertEqual('bat', args.get('foo'))

Example 27

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_choices(self):
        with self.app.app_context():
            req = Request.from_values('/bubble?foo=bar')

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

            self.assertRaises(BadRequest, lambda: parser.parse_args(req))

Example 28

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_foo_operators_ignore(self):
        parser = RequestParser()
        parser.add_argument('foo', ignore=True, store_missing=True)

        args = parser.parse_args(Request.from_values('/bubble'))
        self.assertEqual(args['foo'], None)

Example 29

Project: flask-restplus Source File: test_reqparse.py
Function: test_none_argument
    def test_none_argument(self):
        parser = RequestParser()
        parser.add_argument('foo', location='json')
        with self.app.test_request_context('/bubble', method='post',
                                      data=json.dumps({'foo': None}),
                                      content_type='application/json'):
            args = parser.parse_args()
            self.assertEqual(args['foo'], None)

Example 30

Project: flask-restplus Source File: test_reqparse.py
Function: test_get_json_location
    def test_get_json_location(self):
        parser = RequestParser()
        parser.add_argument('foo', location='json')

        with self.app.test_request_context('/bubble', method='post',
                                      data=json.dumps({'foo': 'bar'}),
                                      content_type='application/json'):
            args = parser.parse_args()
            self.assertEqual(args['foo'], 'bar')

Example 31

Project: flask-restplus Source File: test_reqparse.py
Function: test_type_callable_none
    def test_type_callable_none(self):
        parser = RequestParser()
        parser.add_argument('foo', type=lambda x: x, location='json', required=False),

        with self.app.test_request_context('/bubble', method='post',
                                      data=json.dumps({'foo': None}),
                                      content_type='application/json'):
            try:
                args = parser.parse_args()
                self.assertEqual(args['foo'], None)
            except BadRequest:
                self.fail()

Example 32

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_lte_gte_append(self):
        parser = RequestParser()
        parser.add_argument('foo', operators=['<=', '='], action='append')

        args = parser.parse_args(Request.from_values('/bubble?foo<=bar'))
        self.assertEqual(args['foo'], ['bar'])

Example 33

Project: flask-restplus Source File: test_reqparse.py
    def test_type_filestorage(self):
        parser = RequestParser()
        parser.add_argument('foo', type=FileStorage, location='files')

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

            self.assertEqual(args['foo'].name, 'foo')
            self.assertEqual(args['foo'].filename, 'baz.txt')
            self.assertEqual(args['foo'].read(), fdata)

Example 34

Project: flask-restplus Source File: test_reqparse.py
    @patch('flask_restplus.reqparse.abort', side_effect=BadRequest('Bad Request'))
    def test_no_help(self, abort):
        parser = RequestParser()
        parser.add_argument('foo', choices=['one', 'two'])
        req = Mock(['values'])
        req.values = MultiDict([('foo', 'three')])
        with self.app.app_context():
            with self.assertRaises(BadRequest):
                parser.parse_args(req)
        expected = {'foo': 'three is not a valid choice'}
        abort.assert_called_with(400, 'Input payload validation failed', errors=expected)

Example 35

Project: flask-restplus Source File: test_reqparse.py
    def test_passing_arguments_object(self):
        req = Request.from_values('/bubble?foo=bar')
        parser = RequestParser()
        parser.add_argument(Argument('foo'))

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'bar')

Example 36

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_lte(self):
        req = Request.from_values('/bubble?foo<=bar')
        parser = RequestParser()
        parser.add_argument('foo', operators=['<='])

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'bar')

Example 37

Project: flask-restplus Source File: test_reqparse.py
Function: test_int_range_choice_types
    def test_int_range_choice_types(self):
        parser = RequestParser()
        parser.add_argument('foo', type=int, choices=range(100), location='json')

        with self.app.test_request_context(
                '/bubble', method='post',
                data=json.dumps({'foo': 101}),
                content_type='application/json'
        ):
            try:
                parser.parse_args()
                self.fail()
            except BadRequest:
                pass

Example 38

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_append_default(self):
        req = Request.from_values('/bubble?')

        parser = RequestParser()
        parser.add_argument('foo', action='append', store_missing=True),

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], None)

Example 39

Project: flask-restplus Source File: test_reqparse.py
    def test_request_parse_copy_including_settings(self):
        parser = RequestParser(trim=True, bundle_errors=True)
        parser_copy = parser.copy()

        self.assertEqual(parser.trim, parser_copy.trim)
        self.assertEqual(parser.bundle_errors, parser_copy.bundle_errors)

Example 40

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_error_bundling_w_parser_arg(self):
        self.app.config['BUNDLE_ERRORS'] = False
        req = Request.from_values('/bubble')

        parser = RequestParser(bundle_errors=True)
        parser.add_argument('foo', required=True, location='values')
        parser.add_argument('bar', required=True, location=['values', 'cookies'])
        with self.app.app_context():
            try:
                parser.parse_args(req)
            except BadRequest as e:
                self.assertEqual(e.data['message'], 'Input payload validation failed')
                self.assertEqual(e.data['errors'], {
                    'foo': 'Missing required parameter in the post body or the query string',
                    'bar': ("Missing required parameter in the post body or the query string "
                            "or the request's cookies")
                })

Example 41

Project: flask-restplus Source File: test_reqparse.py
Function: test_both_json_and_values_location
    def test_both_json_and_values_location(self):
        parser = RequestParser()
        parser.add_argument('foo', type=int)
        parser.add_argument('baz', type=int)
        with self.app.test_request_context('/bubble?foo=1', method='post',
                                      data=json.dumps({'baz': 2}),
                                      content_type='application/json'):
            args = parser.parse_args()
            self.assertEqual(args['foo'], 1)
            self.assertEqual(args['baz'], 2)

Example 42

Project: flask-restplus Source File: test_reqparse.py
    @patch('flask_restplus.reqparse.abort')
    def test_help_with_error_msg(self, abort):
        parser = RequestParser()
        parser.add_argument('foo', choices=('one', 'two'), help='Bad choice: {error_msg}')
        req = Mock(['values'])
        req.values = MultiDict([('foo', 'three')])
        with self.app.app_context():
            parser.parse_args(req)
        expected = {'foo': 'Bad choice: three is not a valid choice'}
        abort.assert_called_with(400, 'Input payload validation failed', errors=expected)

Example 43

Project: flask-restplus Source File: test_reqparse.py
Function: test_request_parser_remove_argument
    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)
        self.assertEqual(args, {})

Example 44

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_default(self):
        req = Request.from_values('/bubble')

        parser = RequestParser()
        parser.add_argument('foo', default='bar', store_missing=True)

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'bar')

Example 45

Project: flask-restplus Source File: test_reqparse.py
Function: test_parse_append_single
    def test_parse_append_single(self):
        req = Request.from_values('/bubble?foo=bar')

        parser = RequestParser()
        parser.add_argument('foo', action='append'),

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], ['bar'])

Example 46

Project: flask-restplus Source File: test_reqparse.py
Function: test_parse_model
    def test_parse_model(self):
        model = Model('Todo', {
            'task': fields.String(required=True)
        })

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

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

        with self.app.test_request_context('/', method='post',
                                           data=json.dumps(data),
                                           content_type='application/json'):
            args = parser.parse_args()
            self.assertEqual(args['todo'], {'task': 'aaa'})

Example 47

Project: flask-restplus Source File: test_reqparse.py
    @patch('flask_restplus.reqparse.abort')
    def test_help_no_error_msg(self, abort):
        parser = RequestParser()
        parser.add_argument('foo', choices=['one', 'two'], help='Please select a valid choice')
        req = Mock(['values'])
        req.values = MultiDict([('foo', 'three')])
        with self.app.app_context():
            parser.parse_args(req)
        expected = {'foo': 'Please select a valid choice'}
        abort.assert_called_with(400, 'Input payload validation failed', errors=expected)

Example 48

Project: flask-restplus Source File: test_reqparse.py
Function: test_json_location
    def test_json_location(self):
        parser = RequestParser()
        parser.add_argument('foo', location='json', store_missing=True)

        with self.app.test_request_context('/bubble', method='post'):
            args = parser.parse_args()
            self.assertEqual(args['foo'], None)

Example 49

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_gte(self):
        req = Request.from_values('/bubble?foo>=bar')

        parser = RequestParser()
        parser.add_argument('foo', operators=['>='])

        args = parser.parse_args(req)
        self.assertEqual(args['foo'], 'bar')

Example 50

Project: flask-restplus Source File: test_reqparse.py
    def test_parse_choices_sensitive(self):
        with self.app.app_context():
            req = Request.from_values('/bubble?foo=BAT')

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

            self.assertRaises(BadRequest, lambda: parser.parse_args(req))
See More Examples - Go to Next Page
Page 1 Selected Page 2