Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def exception_handler(self, request, http_exception):
nonlocal app
assert self is app
return Response(200, content=TextContent('Called'))
async def example(request):
nonlocal calls
calls.append(5)
return Response(200, [(b'Server', b'Python/3.7')], JsonContent({'id': '123'}))
async def test_from_body_json_binding_extra_parameters_strategy():
request = Request('POST', b'/', [
JsonContentType
]).with_content(JsonContent({
'a': 'world',
'b': 9000,
'c': 'This is an extra parameter, accepted by constructor explicitly'
}))
parameter = FromJson(ExampleTwo)
value = await parameter.get_value(request)
assert isinstance(value, ExampleTwo)
assert value.a == 'world'
assert value.b == 9000
def test_request_declares_json(headers, expected_result):
request = Request('GET', b'/', headers)
assert request.declares_json() is expected_result
def test_small_request_headers_add_through_higher_api():
request = Request('GET', b'https://hello-world', None)
request.headers.add(b'Hello', b'World')
raw_bytes = write_small_request(request)
assert b'Hello: World\r\n' in raw_bytes
async def test_from_body_json_binding_request_missing_content_type():
request = Request('POST', b'/', [])
parameter = FromJson(ExampleOne)
value = await parameter.get_value(request)
assert value is None
async def test_identity_binder():
request = Request('GET', b'/', None)
request.identity = object()
parameter = IdentityBinder()
value = await parameter.get_value(request)
assert value is request.identity
async def test_middleware_query_normalization():
services = {'context': object()}
fake_request = Request('GET', b'/?example=Lorem', None)
async def middleware(request, handler, example: FromQuery(str)):
assert request is fake_request
assert handler is fake_handler
assert example == 'Lorem'
return await handler(request)
normalized = normalize_middleware(middleware, services)
# NB: middlewares base signature is (request, handler)
result = await normalized(fake_request, fake_handler)
assert result == 'fake-handler-result'
def test_cookie_parsing_duplicated_cookie_header_value():
request = Request('POST', b'/', [
(b'Cookie', b'ai=something; hello=world; foo=Hello%20World%3B; hello=kitty;')
])
print(request.cookies)
assert request.cookies == {
'ai': 'something',
'hello': 'kitty',
'foo': 'Hello World;'
}
def test_cookie_parsing_multiple_cookie_headers():
request = Request('POST', b'/', [
(b'Cookie', b'ai=something; hello=world; foo=Hello%20World%3B;'),
(b'Cookie', b'jib=jab; ai=else;'),
])
assert request.cookies == {
'ai': 'else',
'hello': 'world',
'foo': 'Hello World;',
'jib': 'jab'
}