def test_keep_alive_http10(self): message = RawRequestMessage("GET", "/", HttpVersion10, CIMultiDict(), True, False) req = self.request_from_message(message) resp = StreamResponse() self.loop.run_until_complete(resp.prepare(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() self.loop.run_until_complete(resp.prepare(req)) self.assertEqual(resp.keep_alive, True)
def test_start(self, ResponseImpl): req = self.make_request("GET", "/") resp = StreamResponse() self.assertIsNone(resp.keep_alive) msg = self.loop.run_until_complete(resp.prepare(req)) self.assertTrue(msg.send_headers.called) self.assertIs(msg, self.loop.run_until_complete(resp.prepare(req))) self.assertTrue(resp.keep_alive) req2 = self.make_request("GET", "/") with self.assertRaises(RuntimeError): self.loop.run_until_complete(resp.prepare(req2))
def test_prepare_twice(): req = make_request('GET', '/') resp = StreamResponse() impl1 = yield from resp.prepare(req) impl2 = yield from resp.prepare(req) assert impl1 is impl2
def test_chunked_encoding_forbidden_for_http_10(self): req = self.make_request("GET", "/", version=HttpVersion10) resp = StreamResponse() resp.enable_chunked_encoding() with self.assertRaisesRegex(RuntimeError, "Using chunked encoding is forbidden for HTTP/1.0"): self.loop.run_until_complete(resp.prepare(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 test_keep_alive_http10_switched_on(): headers = CIMultiDict(Connection='keep-alive') req = make_request('GET', '/', version=HttpVersion10, headers=headers) req._message = req._message._replace(should_close=False) resp = StreamResponse() yield from resp.prepare(req) assert resp.keep_alive
def test_set_nodelay_prepared(): resp = StreamResponse() writer = mock.Mock() req = make_request('GET', '/', payload_writer=writer) yield from resp.prepare(req) resp.set_tcp_nodelay(True) writer.set_tcp_nodelay.assert_called_with(True)
def test_start(): req = make_request('GET', '/', payload_writer=mock.Mock()) resp = StreamResponse() assert resp.keep_alive is None msg = yield from resp.prepare(req) assert msg.write_headers.called msg2 = yield from resp.prepare(req) assert msg is msg2 assert resp.keep_alive req2 = make_request('GET', '/') # with pytest.raises(RuntimeError): msg3 = yield from resp.prepare(req2) assert msg is msg3
def test_get_cork_prepared(): resp = StreamResponse() writer = mock.Mock() writer.tcp_cork = False req = make_request('GET', '/', writer=writer) yield from resp.prepare(req) assert not resp.tcp_cork
def test_set_cork_prepared(): resp = StreamResponse() writer = mock.Mock() req = make_request('GET', '/', writer=writer) yield from resp.prepare(req) resp.set_tcp_cork(True) writer.set_tcp_cork.assert_called_with(True)
def test_start(): req = make_request('GET', '/') resp = StreamResponse() assert resp.keep_alive is None with mock.patch('aiohttp.web_reqrep.ResponseImpl'): msg = yield from resp.prepare(req) assert msg.send_headers.called msg2 = yield from resp.prepare(req) assert msg is msg2 assert resp.keep_alive req2 = make_request('GET', '/') with pytest.raises(RuntimeError): yield from resp.prepare(req2)
def test_set_tcp_nodelay_on_start(): req = make_request('GET', '/') resp = StreamResponse() with mock.patch('aiohttp.web_reqrep.ResponseImpl'): resp_impl = yield from resp.prepare(req) resp_impl.transport.set_tcp_nodelay.assert_called_with(True) resp_impl.transport.set_tcp_cork.assert_called_with(False)
def test_start_force_close(): req = make_request('GET', '/') resp = StreamResponse() resp.force_close() assert not resp.keep_alive yield from resp.prepare(req) assert not resp.keep_alive
def test_keep_alive_http09(): headers = CIMultiDict(Connection='keep-alive') message = RawRequestMessage('GET', '/', HttpVersion(0, 9), headers, False, False) req = request_from_message(message) resp = StreamResponse() yield from resp.prepare(req) assert not resp.keep_alive
def test_get_nodelay_prepared(): resp = StreamResponse() writer = mock.Mock() writer.tcp_nodelay = False req = make_request('GET', '/', payload_writer=writer) yield from resp.prepare(req) assert not resp.tcp_nodelay
def test_start(): req = make_request('GET', '/') resp = StreamResponse() assert resp.keep_alive is None with mock.patch('aiohttp.web_response.PayloadWriter'): msg = yield from resp.prepare(req) assert msg.buffer_data.called msg2 = yield from resp.prepare(req) assert msg is msg2 assert resp.keep_alive req2 = make_request('GET', '/') # with pytest.raises(RuntimeError): msg3 = yield from resp.prepare(req2) assert msg is msg3
def test_chunked_encoding_forbidden_for_http_10(): req = make_request('GET', '/', version=HttpVersion10) resp = StreamResponse() resp.enable_chunked_encoding() with pytest.raises_regexp( RuntimeError, "Using chunked encoding is forbidden for HTTP/1.0"): yield from resp.prepare(req)
def test_start_force_close(self): req = self.make_request("GET", "/") resp = StreamResponse() resp.force_close() self.assertFalse(resp.keep_alive) msg = self.loop.run_until_complete(resp.prepare(req)) self.assertFalse(resp.keep_alive) self.assertTrue(msg.closing)
def hello(request): resp = StreamResponse() name = request.match_info.get('name', 'Anonymous') answer = ('Hello, ' + name).encode('utf8') resp.content_length = len(answer) yield from resp.prepare(request) resp.write(answer) yield from resp.write_eof() return resp
def test_keep_alive_http10_switched_on(): headers = CIMultiDict(Connection='keep-alive') message = RawRequestMessage('GET', '/', HttpVersion10, headers, [(b'Connection', b'keep-alive')], False, False) req = request_from_message(message) resp = StreamResponse() yield from resp.prepare(req) assert resp.keep_alive is True
def test_chunked_encoding_forbidden_for_http_10(): req = make_request('GET', '/', version=HttpVersion10) resp = StreamResponse() resp.enable_chunked_encoding() with pytest.raises(RuntimeError) as ctx: yield from resp.prepare(req) assert re.match("Using chunked encoding is forbidden for HTTP/1.0", str(ctx.value))
def test_force_compression_false_backwards_compat(self, ResponseImpl): req = self.make_request("GET", "/") resp = StreamResponse() self.assertFalse(resp.compression) resp.enable_compression(force=False) self.assertTrue(resp.compression) msg = self.loop.run_until_complete(resp.prepare(req)) self.assertFalse(msg.add_compression_filter.called)
def test_force_compression_deflate(self, ResponseImpl): req = self.make_request("GET", "/", headers=CIMultiDict({hdrs.ACCEPT_ENCODING: "gzip, deflate"})) resp = StreamResponse() resp.enable_compression(ContentCoding.deflate) self.assertTrue(resp.compression) msg = self.loop.run_until_complete(resp.prepare(req)) msg.add_compression_filter.assert_called_with("deflate") self.assertEqual("deflate", resp.headers.get(hdrs.CONTENT_ENCODING))
def test_force_compression_no_accept_gzip(self, ResponseImpl): req = self.make_request("GET", "/") resp = StreamResponse() resp.enable_compression(ContentCoding.gzip) self.assertTrue(resp.compression) msg = self.loop.run_until_complete(resp.prepare(req)) msg.add_compression_filter.assert_called_with("gzip") self.assertEqual("gzip", resp.headers.get(hdrs.CONTENT_ENCODING))
def test_force_compression_no_accept_gzip(): req = make_request('GET', '/') resp = StreamResponse() resp.enable_compression(ContentCoding.gzip) assert resp.compression msg = yield from resp.prepare(req) msg.enable_compression.assert_called_with('gzip') assert 'gzip' == resp.headers.get(hdrs.CONTENT_ENCODING)
def test_chunked_encoding(self, ResponseImpl): req = self.make_request("GET", "/") resp = StreamResponse() self.assertFalse(resp.chunked) resp.enable_chunked_encoding() self.assertTrue(resp.chunked) msg = self.loop.run_until_complete(resp.prepare(req)) self.assertTrue(msg.chunked)
def test_chunked_encoding(): req = make_request('GET', '/') resp = StreamResponse() assert not resp.chunked resp.enable_chunked_encoding() assert resp.chunked msg = yield from resp.prepare(req) assert msg.chunked
def test_prepare_calls_signal(): app = mock.Mock() req = make_request('GET', '/', app=app) resp = StreamResponse() sig = mock.Mock() app.on_response_prepare.append(sig) yield from resp.prepare(req) sig.assert_called_with(req, resp)
def test_force_compression_false_backwards_compat(): req = make_request('GET', '/', payload_writer=mock.Mock()) resp = StreamResponse() assert not resp.compression resp.enable_compression(force=False) assert resp.compression msg = yield from resp.prepare(req) assert not msg.enable_compression.called
def test_force_compression_no_accept_gzip(): req = make_request('GET', '/') resp = StreamResponse() resp.enable_compression(ContentCoding.gzip) assert resp.compression with mock.patch('aiohttp.web_reqrep.ResponseImpl'): msg = yield from resp.prepare(req) msg.add_compression_filter.assert_called_with('gzip') assert 'gzip' == resp.headers.get(hdrs.CONTENT_ENCODING)
def test_compression_default_coding(self, ResponseImpl): req = self.make_request('GET', '/', headers=CIMultiDict( {hdrs.ACCEPT_ENCODING: 'gzip, deflate'})) resp = StreamResponse() self.assertFalse(resp.chunked) self.assertFalse(resp.compression) resp.enable_compression() self.assertTrue(resp.compression) msg = self.loop.run_until_complete(resp.prepare(req)) msg.add_compression_filter.assert_called_with('deflate') self.assertEqual('deflate', resp.headers.get(hdrs.CONTENT_ENCODING)) self.assertIsNotNone(msg.filter)
def test_cannot_write_eof_twice(): resp = StreamResponse() writer = mock.Mock() resp_impl = yield from resp.prepare(make_request('GET', '/')) resp_impl.write = mock.Mock() resp_impl.write_eof = mock.Mock() resp_impl.write_eof.return_value = () resp.write(b'data') assert resp_impl.write.called yield from resp.write_eof() resp_impl.write.reset_mock() yield from resp.write_eof() assert not writer.write.called
def test_compression_default_coding(): req = make_request( 'GET', '/', headers=CIMultiDict({hdrs.ACCEPT_ENCODING: 'gzip, deflate'})) resp = StreamResponse() assert not resp.chunked assert not resp.compression resp.enable_compression() assert resp.compression msg = yield from resp.prepare(req) msg.enable_compression.assert_called_with('deflate') assert 'deflate' == resp.headers.get(hdrs.CONTENT_ENCODING) assert msg.filter is not None
def test_remove_content_length_if_compression_enabled_http10(): writer = mock.Mock() def write_headers(status_line, headers): assert hdrs.CONTENT_LENGTH not in headers assert hdrs.TRANSFER_ENCODING not in headers writer.write_headers.side_effect = write_headers req = make_request('GET', '/', version=HttpVersion10, payload_writer=writer) resp = StreamResponse() resp.content_length = 123 resp.enable_compression(ContentCoding.gzip) yield from resp.prepare(req) assert resp.content_length is None
def test___repr__(self): req = self.make_request('GET', '/path/to') resp = StreamResponse(reason=301) self.loop.run_until_complete(resp.prepare(req)) self.assertEqual("<StreamResponse 301 GET /path/to >", repr(resp))
def test___repr__(): req = make_request('GET', '/path/to') resp = StreamResponse(reason=301) yield from resp.prepare(req) assert "<StreamResponse 301 GET /path/to >" == repr(resp)
def test_write_returns_drain(): resp = StreamResponse() yield from resp.prepare(make_request('GET', '/')) assert () == resp.write(b'data')
def test_keep_alive_http09(): headers = CIMultiDict(Connection='keep-alive') req = make_request('GET', '/', version=HttpVersion(0, 9), headers=headers) resp = StreamResponse() yield from resp.prepare(req) assert not resp.keep_alive
def test_keep_alive_http10_default(): req = make_request('GET', '/', version=HttpVersion10) resp = StreamResponse() yield from resp.prepare(req) assert not resp.keep_alive
def test_response_output_length(): resp = StreamResponse() yield from resp.prepare(make_request('GET', '/')) with pytest.warns(DeprecationWarning): assert resp.output_length
def _test_write_returns_empty_tuple_on_empty_data(): resp = StreamResponse() yield from resp.prepare(make_request('GET', '/')) with mock.patch('aiohttp.http_writer.noop') as noop: assert noop.return_value == resp.write(b'')
def _test_write_returns_drain(): resp = StreamResponse() yield from resp.prepare(make_request('GET', '/')) with mock.patch('aiohttp.http_writer.noop') as noop: assert noop == resp.write(b'data')
def test_write_returns_empty_tuple_on_empty_data(self): resp = StreamResponse() self.loop.run_until_complete( resp.prepare(self.make_request('GET', '/'))) self.assertEqual((), resp.write(b''))
def test_write_non_byteish(): resp = StreamResponse() yield from resp.prepare(make_request('GET', '/')) with pytest.raises(AssertionError): resp.write(123)
def test_started_when_started(self): resp = StreamResponse() self.loop.run_until_complete( resp.prepare(self.make_request('GET', '/'))) self.assertTrue(resp.prepared)
def test_write_returns_empty_tuple_on_empty_data(): resp = StreamResponse() yield from resp.prepare(make_request('GET', '/')) assert () == resp.write(b'')
def test_response_output_length(): resp = StreamResponse() yield from resp.prepare(make_request('GET', '/')) assert resp.output_length
def test_started_when_started(): resp = StreamResponse() yield from resp.prepare(make_request('GET', '/')) assert resp.prepared
def test_changing_status_after_prepare_raises(): resp = StreamResponse() yield from resp.prepare(make_request('GET', '/')) with pytest.raises(AssertionError): resp.set_status(400)