Added support to handle empty query parameters through a global
option set ``RequestOptions`` applied to ``API``.
This option can be enabled by using
api.req_options.keep_blank_qs_values = True where api is an instance
of ``API``.
This change allows queries such as '?a=1&bool' and '?a=1&empty=' to
preserve the 'bool' and 'empty' values respectively as '' instead
of the default value of None.
Another option was added to ``Request.get_param_as_bool`` to allow
empty query parameters to return a True value.
Because of the non-obvious nature of this change ('' == True), it
requires a blank_as_true=True argument be passed to
``Request.get_param_as_bool``.
22 lines
668 B
Python
22 lines
668 B
Python
from falcon.request import RequestOptions
|
|
import falcon.testing as testing
|
|
|
|
|
|
class TestRequestOptions(testing.TestBase):
|
|
|
|
def test_correct_options(self):
|
|
options = RequestOptions()
|
|
self.assertFalse(options.keep_blank_qs_values)
|
|
options.keep_blank_qs_values = True
|
|
self.assertTrue(options.keep_blank_qs_values)
|
|
options.keep_blank_qs_values = False
|
|
self.assertFalse(options.keep_blank_qs_values)
|
|
|
|
def test_incorrect_options(self):
|
|
options = RequestOptions()
|
|
|
|
def _assign_invalid():
|
|
options.invalid_option_and_attribute = True
|
|
|
|
self.assertRaises(AttributeError, _assign_invalid)
|