async def test_new_style_middleware_method(loop, test_client): async def handler(request): return web.Response(body=b'OK') class Middleware: @web.middleware async def call(self, request, handler): resp = await handler(request) assert 200 == resp.status resp.set_status(201) resp.text = resp.text + '[new style middleware]' return resp with pytest.warns(None) as warning_checker, \ mock.patch("aiohttp.http_writer.PayloadWriter.write") as patched: patched.return_value = noop() app = web.Application() app.middlewares.append(Middleware().call) app.router.add_route('GET', '/', handler) client = await test_client(app) resp = await client.get('/') assert 201 == resp.status txt = await resp.text() assert 'OK[new style middleware]' == txt assert len(warning_checker) == 0
async def test_old_style_middleware_class(loop, test_client): async def handler(request): return web.Response(body=b'OK') class Middleware: async def __call__(self, app, handler): async def middleware(request): resp = await handler(request) assert 200 == resp.status resp.set_status(201) resp.text = resp.text + '[old style middleware]' return resp return middleware with pytest.warns(DeprecationWarning) as warning_checker, \ mock.patch("aiohttp.http_writer.PayloadWriter.write") as patched: patched.return_value = noop() app = web.Application() app.middlewares.append(Middleware()) app.router.add_route('GET', '/', handler) client = await test_client(app) resp = await client.get('/') assert 201 == resp.status txt = await resp.text() assert 'OK[old style middleware]' == txt assert len(warning_checker) == 1 msg = str(warning_checker.list[0].message) assert re.match( '^old-style middleware ' '"<test_web_middleware.test_old_style_middleware_class.' '<locals>.Middleware object ' 'at 0x[0-9a-fA-F]+>" deprecated, see #2252$', msg)
async def test_mixed_middleware(loop, test_client): async def handler(request): return web.Response(body=b'OK') async def m_old1(app, handler): async def middleware(request): resp = await handler(request) resp.text += '[old style 1]' return resp return middleware @web.middleware async def m_new1(request, handler): resp = await handler(request) resp.text += '[new style 1]' return resp async def m_old2(app, handler): async def middleware(request): resp = await handler(request) resp.text += '[old style 2]' return resp return middleware @web.middleware async def m_new2(request, handler): resp = await handler(request) resp.text += '[new style 2]' return resp middlewares = m_old1, m_new1, m_old2, m_new2 with pytest.warns(DeprecationWarning) as w, \ mock.patch("aiohttp.http_writer.PayloadWriter.write") as patched: patched.return_value = noop() app = web.Application(middlewares=middlewares) app.router.add_route('GET', '/', handler) client = await test_client(app) resp = await client.get('/') assert 200 == resp.status txt = await resp.text() assert 'OK[new style 2][old style 2][new style 1][old style 1]' == txt assert len(w) == 2 tmpl = ('^old-style middleware ' '"<function test_mixed_middleware.<locals>.' '{} at 0x[0-9a-fA-F]+>" ' 'deprecated, see #2252$') p1 = tmpl.format('m_old1') p2 = tmpl.format('m_old2') assert re.match(p2, str(w.list[0].message)) assert re.match(p1, str(w.list[1].message))
def _send_frame(self, message, opcode): """Send a frame over the websocket with message as its payload.""" if self._closing: ws_logger.warning('websocket connection is closing.') msg_length = len(message) use_mask = self.use_mask if use_mask: mask_bit = 0x80 else: mask_bit = 0 if msg_length < 126: header = PACK_LEN1(0x80 | opcode, msg_length | mask_bit) elif msg_length < (1 << 16): header = PACK_LEN2(0x80 | opcode, 126 | mask_bit, msg_length) else: header = PACK_LEN3(0x80 | opcode, 127 | mask_bit, msg_length) if use_mask: mask = self.randrange(0, 0xffffffff) mask = mask.to_bytes(4, 'big') message = _websocket_mask(mask, bytearray(message)) self.writer.write(header + mask + message) self._output_size += len(header) + len(mask) + len(message) else: if len(message) > MSG_SIZE: self.writer.write(header) self.writer.write(message) else: self.writer.write(header + message) self._output_size += len(header) + len(message) if self._output_size > self._limit: self._output_size = 0 return self.writer.drain() return noop()
def append(data=b''): buf.extend(data) return helpers.noop()
def function2620(arg1868=b''): function376.extend(arg1868) return helpers.noop()