async def clone_request(self, method, endpoint, payload, headers): container_url = IAbsoluteURL(self.request.container, self.request)() url = posixpath.join(container_url, endpoint) parsed = urlparse(url) dct = {'method': method, 'url': URL(url), 'path': parsed.path} dct['headers'] = CIMultiDict(headers) dct['raw_headers'] = tuple( (k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items()) message = self.request._message._replace(**dct) payload_writer = mock.Mock() payload_writer.write_eof.side_effect = noop payload_writer.drain.side_effect = noop protocol = mock.Mock() protocol.transport = test_utils._create_transport(None) protocol.writer = payload_writer request = self.request.__class__( message, SimplePayload(payload), protocol, payload_writer, self.request._task, self.request._loop, client_max_size=self.request._client_max_size, state=self.request._state.copy(), scheme=self.request.scheme, host=self.request.host, remote=self.request.remote) request._db_write_enabled = True request._db_id = self.request._db_id request._tm = self.request._tm request._txn = self.request._txn request._container_id = self.context.id request.container = self.context annotations_container = IAnnotations(self.context) request.container_settings = await annotations_container.async_get( REGISTRY_DATA_KEY) layers = request.container_settings.get(ACTIVE_LAYERS_KEY, []) for layer in layers: try: alsoProvides(request, import_class(layer)) except ModuleNotFoundError: pass request._futures = self.request._futures return request
def make_mocked_request(method, path, headers=None, *, version=HttpVersion(1, 1), closing=False, app=None, writer=sentinel, payload_writer=sentinel, protocol=sentinel, transport=sentinel, payload=sentinel, sslcontext=None, client_max_size=1024**2): """ XXX copied from aiohttp but using guillotina request object Creates mocked web.Request testing purposes. Useful in unit tests, when spinning full web server is overkill or specific conditions and errors are hard to trigger. """ task = mock.Mock() loop = mock.Mock() loop.create_future.return_value = () if version < HttpVersion(1, 1): closing = True if headers: headers = CIMultiDict(headers) raw_hdrs = tuple( (k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items()) else: headers = CIMultiDict() raw_hdrs = () chunked = 'chunked' in headers.get(hdrs.TRANSFER_ENCODING, '').lower() message = RawRequestMessage( method, path, version, headers, raw_hdrs, closing, False, False, chunked, URL(path)) if app is None: app = test_utils._create_app_mock() if protocol is sentinel: protocol = mock.Mock() if transport is sentinel: transport = test_utils._create_transport(sslcontext) if writer is sentinel: writer = mock.Mock() writer.transport = transport if payload_writer is sentinel: payload_writer = mock.Mock() payload_writer.write_eof.side_effect = noop payload_writer.drain.side_effect = noop protocol.transport = transport protocol.writer = writer if payload is sentinel: payload = mock.Mock() time_service = mock.Mock() time_service.time.return_value = 12345 time_service.strtime.return_value = "Tue, 15 Nov 1994 08:12:31 GMT" @contextmanager def timeout(*args, **kw): yield time_service.timeout = mock.Mock() time_service.timeout.side_effect = timeout req = Request(message, payload, protocol, payload_writer, time_service, task, client_max_size=client_max_size) match_info = UrlMappingMatchInfo({}, mock.Mock()) match_info.add_app(app) req._match_info = match_info return req
async def create_request(self): req_data = self.data['req_data'] url = req_data['url'] parsed = urlparse(url) dct = { 'method': req_data['method'], 'url': yarl.URL(url), 'path': parsed.path, 'headers': CIMultiDict(req_data['headers']), 'raw_headers': tuple((k.encode('utf-8'), v.encode('utf-8')) for k, v in req_data['headers'].items()) } message = self.base_request._message._replace(**dct) payload_writer = mock.Mock() payload_writer.write_eof.side_effect = noop payload_writer.drain.side_effect = noop protocol = mock.Mock() protocol.transport = test_utils._create_transport(None) protocol.writer = payload_writer request = self.base_request.__class__( message, EmptyPayload(), protocol, payload_writer, self.task, self.task._loop, client_max_size=self.base_request._client_max_size, state=self.base_request._state.copy()) g_task_vars.request.set(request) request.annotations = req_data.get('annotations', {}) if self.data.get('db_id'): root = get_utility(IApplication, name='root') db = await root.async_get(self.data['db_id']) g_task_vars.db.set(db) # Add a transaction Manager to request tm = db.get_transaction_manager() g_task_vars.tm.set(tm) # Start a transaction txn = await tm.begin() # Get the root of the tree context = await tm.get_root(txn=txn) if self.data.get('container_id'): container = await context.async_get(self.data['container_id']) if container is None: raise Exception( f'Could not find container: {self.data["container_id"]}' ) g_task_vars.container.set(container) annotations_container = IAnnotations(container) container_settings = await annotations_container.async_get( REGISTRY_DATA_KEY) layers = container_settings.get(ACTIVE_LAYERS_KEY, []) for layer in layers: try: alsoProvides(request, import_class(layer)) except ModuleNotFoundError: pass g_task_vars.registry.set(container_settings) return request