Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class TestSerializer(BaseSerializer):
one = StringField("one different than two")
two = StringField("two different than one")
def validate(self, object_dict, partial=False):
super().validate(object_dict, partial)
# possible use case: kind of uniqueness relationship
if object_dict['one'] == object_dict['two']:
raise ValidationError("one must be different than two")
class TestResource(Resource):
serializer = TestSerializer()
resource = TestResource()
env = create_environ(
body=json.dumps({'one': 'foo', 'two': 'foo'}),
headers={'Content-Type': 'application/json'},
)
with pytest.raises(errors.HTTPBadRequest):
resource.require_validated(Request(env))
def _test_header_expected_value(self, name, value, attr, expected_value):
headers = {name: value}
req = Request(testing.create_environ(headers=headers))
assert getattr(req, attr) == expected_value
def create_req(ctx, body):
'''creates a falcon request'''
env = testing.create_environ(
path='/',
query_string='',
protocol='HTTP/1.1',
scheme='http',
host='falconframework.org',
port=None,
headers={'Content-Type': 'application/json'},
app='',
body=body,
method='POST',
wsgierrors=None,
file_wrapper=None)
req = falcon.Request(env)
req.context = ctx
return req
def test_netloc_default_port(self, protocol):
req = Request(testing.create_environ(
protocol=protocol,
app=self.app,
path='/hello',
query_string=self.qs,
headers=self.headers))
assert req.netloc == 'falconframework.org'
def simulate(protocol, headers=None):
env = testing.create_environ(
method='GET',
scheme=protocol,
path=_TEST_PATH,
query_string='',
headers=headers,
)
srmock = testing.StartResponseMock()
iterable = app(env, srmock)
return testing.Result(iterable, srmock.status, srmock.headers)
def setup_method(self, method):
self.qs = 'marker=deadbeef&limit=10'
self.headers = {
'Content-Type': 'text/plain',
'Content-Length': '4829',
'Authorization': ''
}
self.app = '/test'
self.path = '/hello'
self.relative_uri = self.path + '?' + self.qs
self.req = Request(testing.create_environ(
app=self.app,
port=8080,
path='/hello',
query_string=self.qs,
headers=self.headers))
self.req_noqs = Request(testing.create_environ(
app=self.app,
path='/hello',
headers=self.headers))
def test_x_forwarded_host():
req = Request(testing.create_environ(
host='suchproxy.suchtesting.com',
path='/languages',
headers={'X-Forwarded-Host': 'something.org'}
))
assert req.forwarded is None
assert req.forwarded_host == 'something.org'
assert req.forwarded_uri != req.uri
assert req.forwarded_uri == 'http://something.org/languages'
assert req.forwarded_prefix == 'http://something.org'
assert req.forwarded_prefix == 'http://something.org' # Check cached value
def queues_env():
request_headers = {'Content-Type': 'application/json'}
path = ('/v1/852809/queues/0fd4c8c6-bd72-11e2-8e47-db5ebd4c8125'
'/claims/db5ebd4c8125')
qs = 'limit=10&thing=a%20b&x=%23%24'
return helpers.create_environ(path, query_string=qs,
headers=request_headers)