本文整理汇总了Python中aiohttp.test_utils.make_mocked_request方法的典型用法代码示例。如果您正苦于以下问题:Python test_utils.make_mocked_request方法的具体用法?Python test_utils.make_mocked_request怎么用?Python test_utils.make_mocked_request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.test_utils
的用法示例。
在下文中一共展示了test_utils.make_mocked_request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_render_not_initialized
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_render_not_initialized():
async def func(request):
return aiohttp_jinja2.render_template('template', request, {})
app = web.Application()
app.router.add_route('GET', '/', func)
req = make_mocked_request('GET', '/', app=app)
msg = "Template engine is not initialized, " \
"call aiohttp_jinja2.setup(..., app_key={}" \
") first".format(aiohttp_jinja2.APP_KEY)
with pytest.raises(web.HTTPInternalServerError) as ctx:
await func(req)
assert msg == ctx.value.text
示例2: test_template_not_found
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_template_not_found():
async def func(request):
return aiohttp_jinja2.render_template('template', request, {})
app = web.Application()
aiohttp_jinja2.setup(app, loader=jinja2.DictLoader({}))
app.router.add_route('GET', '/', func)
req = make_mocked_request('GET', '/', app=app)
with pytest.raises(web.HTTPInternalServerError) as ctx:
await func(req)
t = "Template 'template' not found"
assert t == ctx.value.text
assert t == ctx.value.reason
示例3: test_render_not_mapping
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_render_not_mapping():
@aiohttp_jinja2.template('tmpl.jinja2')
async def func(request):
return 123
app = web.Application()
aiohttp_jinja2.setup(app, loader=jinja2.DictLoader(
{'tmpl.jinja2': 'tmpl'}))
app.router.add_route('GET', '/', func)
req = make_mocked_request('GET', '/', app=app)
msg = "context should be mapping, not <class 'int'>"
with pytest.raises(web.HTTPInternalServerError) as ctx:
await func(req)
assert msg == ctx.value.text
示例4: test_get_new_session
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_get_new_session():
req = make_mocked_request('GET', '/')
session = Session('identity', data=None, new=False)
class Storage(AbstractStorage):
async def load_session(self, request):
pass
async def save_session(self, request, response, session):
pass
req[SESSION_KEY] = session
req[STORAGE_KEY] = Storage()
ret = await new_session(req)
assert ret is not session
示例5: test_default_options
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_default_options(test_client):
headers = {
hdrs.ACCESS_CONTROL_REQUEST_HEADERS: hdrs.AUTHORIZATION}
request = make_request('OPTIONS', '/', headers=headers)
router = TreeUrlDispatcher()
mi = await router.resolve(request)
assert isinstance(mi, MatchInfoError)
app = web.Application(router=router)
router.set_cors(app)
router.add_get('/', lambda request: web.Response())
mi = await router.resolve(request)
assert not isinstance(mi, MatchInfoError)
client = await test_client(app)
response = await client.options('/', headers=headers)
assert response.status == 200
h = response.headers
assert h[hdrs.ACCESS_CONTROL_ALLOW_ORIGIN] == '*'
assert h[hdrs.ACCESS_CONTROL_ALLOW_METHODS] == 'GET'
assert h[hdrs.ACCESS_CONTROL_ALLOW_HEADERS] == hdrs.AUTHORIZATION
示例6: test_content_receiver
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_content_receiver():
cr = ContentReceiver()
l1 = len(cr)
assert l1
mime_json = 'application/json'
assert mime_json in cr
cr[None] = cr[mime_json]
assert len(cr) == l1 + 1
request = make_request('PUT', '/', headers={'Content-Type': mime_json})
request._read_bytes = json.dumps(2).encode()
assert 2 == await cr.receive(request)
del cr[None]
cr.freeze()
with pytest.raises(RuntimeError):
del cr[mime_json]
with pytest.raises(RuntimeError):
cr[None] = None
request = make_request('PUT', '/', headers={'Content-Type': '1'})
with pytest.raises(TypeError):
await cr.receive(request)
assert list(cr)
示例7: test_render_not_initialized
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_render_not_initialized():
async def func(request):
return aiohttp_mako.render_template('template', request, {})
app = web.Application()
app.router.add_route('GET', '/', func)
req = make_mocked_request('GET', '/', app=app)
with pytest.raises(web.HTTPInternalServerError) as ctx:
await func(req)
assert "Template engine is not initialized, " \
"call aiohttp_mako.setup(app_key={}) first" \
"".format(aiohttp_mako.APP_KEY) == ctx.value.text
示例8: test_template_not_mapping
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_template_not_mapping():
@aiohttp_mako.template('tmpl.html')
async def func(request):
return 'data'
app = web.Application()
lookup = aiohttp_mako.setup(app, input_encoding='utf-8',
output_encoding='utf-8',
default_filters=['decode.utf8'])
tplt = "<html><body><h1>${head}</h1>${text}</body></html>"
lookup.put_string('tmpl.html', tplt)
app.router.add_route('GET', '/', func)
req = make_mocked_request('GET', '/', app=app)
with pytest.raises(web.HTTPInternalServerError) as ctx:
await func(req)
assert "context should be mapping, not" \
" <class 'str'>" == ctx.value.text
示例9: make_mocked_view_instance
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def make_mocked_view_instance(app, view_cls, method, url, params=None, post=None, *, headers=None,
content_type='application/json') -> Union[BaseView, AbstractSQLView, PeeweeView]:
if not headers:
headers = {}
if content_type:
headers['Content-Type'] = content_type
request = _make_mocked_request(method, url, headers=headers, protocol=mock.Mock(), app=app)
_polyfill_post(request, post)
view = view_cls(app, request)
view._params_cache = params
view._post_data_cache = post
await view._prepare()
return view
示例10: test_middleware_with_not_skip_route
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_middleware_with_not_skip_route(tracer, fake_transport):
async def handler(request):
return web.Response(body=b'data')
app = web.Application()
skip_route = app.router.add_get('/', handler)
az.setup(app, tracer)
match_info = Mock()
match_info.route = skip_route
req = make_mocked_request('GET', '/', headers={'token': 'x'}, app=app)
req._match_info = match_info
middleware = middleware_maker(skip_routes=[skip_route])
await middleware(req, handler)
assert len(fake_transport.records) == 0
示例11: test_middleware_with_invalid_ip
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_middleware_with_invalid_ip(tracer, version, address):
app = web.Application()
az.setup(app, tracer)
# Fake transport
transp = Mock()
transp.get_extra_info.return_value = (address, '0')
async def handler(request):
return web.Response(body=b'data')
req = make_mocked_request('GET', '/',
headers={'token': 'x'},
transport=transp, app=app)
middleware = middleware_maker()
with patch('aiozipkin.span.Span.remote_endpoint') as mocked_remote_ep:
await middleware(req, handler)
assert mocked_remote_ep.call_count == 0
示例12: test_get_stored_session
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_get_stored_session():
req = make_mocked_request('GET', '/')
session = Session('identity', data=None, new=False)
req[SESSION_KEY] = session
ret = await get_session(req)
assert session is ret
示例13: test_session_is_not_stored
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_session_is_not_stored():
req = make_mocked_request('GET', '/')
with pytest.raises(RuntimeError):
await get_session(req)
示例14: test_storage_returns_not_session_on_load_session
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_storage_returns_not_session_on_load_session():
req = make_mocked_request('GET', '/')
class Storage():
async def load_session(self, request):
return None
req[STORAGE_KEY] = Storage()
with pytest.raises(RuntimeError):
await get_session(req)
示例15: test_get_new_session_no_storage
# 需要导入模块: from aiohttp import test_utils [as 别名]
# 或者: from aiohttp.test_utils import make_mocked_request [as 别名]
def test_get_new_session_no_storage():
req = make_mocked_request('GET', '/')
session = Session('identity', data=None, new=False)
req[SESSION_KEY] = session
with pytest.raises(RuntimeError):
await new_session(req)