def test_keep_alive_http10(self): message = RawRequestMessage('GET', '/', HttpVersion10, CIMultiDict(), True, False) req = self.request_from_message(message) resp = StreamResponse() resp.start(req) self.assertFalse(resp.keep_alive) headers = CIMultiDict(Connection='keep-alive') message = RawRequestMessage('GET', '/', HttpVersion10, headers, False, False) req = self.request_from_message(message) resp = StreamResponse() resp.start(req) self.assertEqual(resp.keep_alive, True)
def maker(method, path, headers=CIMultiDict(), *, version=HttpVersion(1, 1), closing=False, sslcontext=None, secure_proxy_ssl_header=None): if version < HttpVersion(1, 1): closing = True app = mock.Mock() app._debug = False app.on_response_prepare = Signal(app) message = RawRequestMessage(method, path, version, headers, [(k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items()], closing, False) payload = mock.Mock() transport = mock.Mock() def get_extra_info(key): if key == 'sslcontext': return sslcontext else: return None transport.get_extra_info.side_effect = get_extra_info writer = mock.Mock() reader = mock.Mock() req = Request(app, message, payload, transport, reader, writer, secure_proxy_ssl_header=secure_proxy_ssl_header) assert req.app is app assert req.content is payload assert req.transport is transport return req
def make_request(self, method, path, headers=CIMultiDict(), *, version=HttpVersion(1, 1), closing=False, sslcontext=None, secure_proxy_ssl_header=None): if version < HttpVersion(1, 1): closing = True self.app = mock.Mock() message = RawRequestMessage(method, path, version, headers, closing, False) self.payload = mock.Mock() self.transport = mock.Mock() def get_extra_info(key): if key == 'sslcontext': return sslcontext else: return None self.transport.get_extra_info.side_effect = get_extra_info self.writer = mock.Mock() self.reader = mock.Mock() req = Request(self.app, message, self.payload, self.transport, self.reader, self.writer, secure_proxy_ssl_header=secure_proxy_ssl_header) return req
def make_request(method, path, headers=CIMultiDict(), version=HttpVersion11, **kwargs): message = RawRequestMessage(method, path, version, headers, [(k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items()], False, False) return request_from_message(message, **kwargs)
def make_request(self, method, path, headers=None, protocols=False): self.app = mock.Mock() self.app._debug = False if headers is None: headers = CIMultiDict({ 'HOST': 'server.example.com', 'UPGRADE': 'websocket', 'CONNECTION': 'Upgrade', 'SEC-WEBSOCKET-KEY': 'dGhlIHNhbXBsZSBub25jZQ==', 'ORIGIN': 'http://example.com', 'SEC-WEBSOCKET-VERSION': '13' }) if protocols: headers['SEC-WEBSOCKET-PROTOCOL'] = 'chat, superchat' message = RawRequestMessage(method, path, HttpVersion11, headers, [(k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items()], False, False) self.payload = mock.Mock() self.transport = mock.Mock() self.reader = mock.Mock() self.writer = mock.Mock() self.app.loop = self.loop self.app.on_response_prepare = signals.Signal(self.app) req = Request(self.app, message, self.payload, self.transport, self.reader, self.writer) return req
def make_request(method, path, headers=CIMultiDict(), version=HttpVersion11, **kwargs): message = RawRequestMessage(method, path, version, headers, False, False) return request_from_message(message, **kwargs)
def make_request(self, method='GET', path='/', headers=CIMultiDict()): self.app = mock.Mock() message = RawRequestMessage(method, path, HttpVersion11, headers, False, False) req = Request(self.app, message, self.payload, self.transport, self.reader, self.writer) return req
def test_keep_alive_http10_default(): message = RawRequestMessage('GET', '/', HttpVersion10, CIMultiDict(), [], True, False) req = request_from_message(message) resp = StreamResponse() yield from resp.prepare(req) assert not resp.keep_alive
def make_request(self, method, path, headers=CIMultiDict(), version=HttpVersion11): message = RawRequestMessage(method, path, version, headers, False, False) return self.request_from_message(message)
def test_keep_alive_http09(self): headers = CIMultiDict(Connection='keep-alive') message = RawRequestMessage('GET', '/', HttpVersion(0, 9), headers, False, False) req = self.request_from_message(message) resp = StreamResponse() resp.start(req) self.assertFalse(resp.keep_alive)
def test_keep_alive_http09(): headers = CIMultiDict(Connection='keep-alive') message = RawRequestMessage('GET', '/', HttpVersion(0, 9), headers, [(b'Connection', b'keep-alive')], False, False) req = request_from_message(message) resp = StreamResponse() yield from resp.prepare(req) assert not resp.keep_alive
def test_keep_alive_http10_switched_on(): headers = CIMultiDict(Connection='keep-alive') message = RawRequestMessage('GET', '/', HttpVersion10, headers, False, False) req = request_from_message(message) resp = StreamResponse() yield from resp.prepare(req) assert resp.keep_alive is True
def make_request(self, method, path, headers=MultiDict()): self.app = mock.Mock() message = RawRequestMessage(method, path, HttpVersion11, headers, False, False) self.payload = mock.Mock() self.writer = mock.Mock() req = Request(self.app, message, self.payload, self.writer) return req
def make_request(self, method, path): self.app = mock.Mock() message = RawRequestMessage(method, path, HttpVersion(1, 1), MultiDict(), False, False) self.payload = mock.Mock() self.transport = mock.Mock() self.writer = mock.Mock() req = Request(self.app, message, self.payload, self.transport, self.writer, 15) return req
def make_request(self, method, path, headers=MultiDict(), *, version=HttpVersion(1, 1), closing=False, keep_alive_timeout=15): self.app = mock.Mock() message = RawRequestMessage(method, path, version, headers, closing, False) self.payload = mock.Mock() self.transport = mock.Mock() self.writer = mock.Mock() req = Request(self.app, message, self.payload, self.transport, self.writer, keep_alive_timeout) return req
def make_request(self, method, path, headers=MultiDict(), *, version=HttpVersion(1, 1), closing=False): self.app = mock.Mock() message = RawRequestMessage(method, path, version, headers, closing, False) self.payload = mock.Mock() self.writer = mock.Mock() req = Request(self.app, message, self.payload, self.writer) return req
def test_handler_returns_not_response(self): app = web.Application(loop=self.loop) def handler(request): return 'abc' app.router.add_route('GET', '/', handler) h = app.make_handler()() message = RawRequestMessage('GET', '/', HttpVersion11, MultiDict(), False, False) payload = mock.Mock() with self.assertRaises(RuntimeError): self.loop.run_until_complete(h.handle_request(message, payload))
def make_raw_request_message(method, path, headers, version=HttpVersion11, should_close=False, compression=False): raw_headers = [(k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items()] try: message = RawRequestMessage(method=method, path=path, headers=headers, raw_headers=raw_headers, version=version, should_close=should_close, compression=compression) except TypeError: # aiohttp < 0.21.x message = RawRequestMessage(method=method, path=path, headers=headers, version=version, should_close=should_close, compression=compression) return message
def call_application(self, application, catch_exc_info=False): if self.is_body_seekable: self.body_file_raw.seek(0) http_version = HttpVersion10 if self.http_version == 'HTTP/1.0' else HttpVersion11 message = RawRequestMessage(self.method, self.path, http_version, CIMultiDict(self.headers), False, False) payload = self.body_file_raw loop = asyncio.get_event_loop() handler = RequestHandlerFactory(application, application.router, handler=TestRequestHandler, loop=loop)() response = loop.run_until_complete( handler.handle_request(message, payload)) return response.status, response.headers.items(), [response.body], None
def make_request(self, method, path, headers=None): self.app = mock.Mock() if headers is None: headers = CIMultiDict( {'HOST': 'server.example.com', 'UPGRADE': 'websocket', 'CONNECTION': 'Upgrade', 'SEC-WEBSOCKET-KEY': 'dGhlIHNhbXBsZSBub25jZQ==', 'ORIGIN': 'http://example.com', 'SEC-WEBSOCKET-PROTOCOL': 'chat, superchat', 'SEC-WEBSOCKET-VERSION': '13'}) message = RawRequestMessage(method, path, HttpVersion11, headers, False, False) self.payload = mock.Mock() self.transport = mock.Mock() self.reader = mock.Mock() self.writer = mock.Mock() self.app.loop = self.loop req = Request(self.app, message, self.payload, self.transport, self.reader, self.writer) return req
def request(buf): method = 'GET' path = '/' headers = CIMultiDict() transport = mock.Mock() payload = mock.Mock() reader = mock.Mock() writer = mock.Mock() writer.drain.return_value = () def append(data): buf.extend(data) writer.write.side_effect = append app = mock.Mock() app._debug = False app.on_response_prepare = signals.Signal(app) message = RawRequestMessage(method, path, HttpVersion11, headers, [], False, False) req = Request(app, message, payload, transport, reader, writer) return req
def make_request(app, method, path, headers=CIMultiDict()): message = RawRequestMessage(method, path, HttpVersion11, headers, False, False) return request_from_message(message, app)