def test_path_safe_chars_preserved(self): req = ClientRequest('get', "http://0.0.0.0/get/%:=", loop=self.loop) self.assertEqual(req.path, "/get/%:=") self.loop.run_until_complete(req.close())
def maker(method, url, *args, **kwargs): nonlocal request request = ClientRequest(method, URL(url), *args, loop=loop, **kwargs) return request
async def test_no_content_length_head(loop, conn): req = ClientRequest('head', URL('http://python.org'), loop=loop) resp = req.send(conn) assert req.headers.get('CONTENT-LENGTH') is None await req.close() resp.close()
def test_pass_falsy_data(self, _): req = ClientRequest( 'post', 'http://python.org/', data={}, loop=self.loop) req.update_body_from_data.assert_called_once_with({}, frozenset()) self.loop.run_until_complete(req.close())
def test_terminate_without_writer(loop): req = ClientRequest('get', URL('http://python.org'), loop=loop) assert req._writer is None req.terminate() assert req._writer is None
def maker(*args, **kwargs): nonlocal request request = ClientRequest(*args, loop=loop, **kwargs) return request
def test_content_type_auto_header_get(self): req = ClientRequest('get', 'http://python.org', loop=self.loop) resp = req.send(self.transport, self.protocol) self.assertNotIn('CONTENT-TYPE', req.headers) resp.close()
def test_content_type_auto_header_bytes(loop, conn): req = ClientRequest('post', URL('http://python.org'), data=b'hey you', loop=loop) resp = req.send(conn) assert 'application/octet-stream' == req.headers.get('CONTENT-TYPE') resp.close()
async def test_compress_and_content_encoding(loop, conn): with pytest.raises(ValueError): ClientRequest('post', URL('http://python.org/'), data='foo', headers={'content-encoding': 'deflate'}, compress='deflate', loop=loop)
async def test_content_type_auto_header_bytes(loop: Any, conn: Any) -> None: req = ClientRequest("post", URL("http://python.org"), data=b"hey you", loop=loop) resp = await req.send(conn) assert "application/octet-stream" == req.headers.get("CONTENT-TYPE") resp.close()
async def test_pass_falsy_data(loop: Any) -> None: with mock.patch("aiohttp.client_reqrep.ClientRequest.update_body_from_data"): req = ClientRequest("post", URL("http://python.org/"), data={}, loop=loop) req.update_body_from_data.assert_called_once_with({}) await req.close()
def test_no_content_length(loop, conn): req = ClientRequest('get', URL('http://python.org'), loop=loop) resp = req.send(conn) assert '0' == req.headers.get('CONTENT-LENGTH') yield from req.close() resp.close()
def test_default_loop(self): asyncio.set_event_loop(self.loop) self.addCleanup(asyncio.set_event_loop, None) req = ClientRequest('get', 'http://python.org/') self.assertIs(req.loop, self.loop)
def test_no_path(self): req = ClientRequest('get', 'http://python.org', loop=self.loop) self.assertEqual('/', req.path) self.loop.run_until_complete(req.close())
async def test_content_type_auto_header_get(loop, conn) -> None: req = ClientRequest("get", URL("http://python.org"), loop=loop) resp = await req.send(conn) assert "CONTENT-TYPE" not in req.headers resp.close()
async def test_chunked_length(loop, conn): with pytest.raises(ValueError): ClientRequest( 'post', URL('http://python.org/'), headers={'CONTENT-LENGTH': '1000'}, chunked=True, loop=loop)
def test_no_content_length2(loop): req = ClientRequest('head', URL('http://python.org'), loop=loop) resp = req.send(mock.Mock(), mock.Mock()) assert '0' == req.headers.get('CONTENT-LENGTH') yield from req.close() resp.close()
async def test_chunked_transfer_encoding(loop, conn): with pytest.raises(ValueError): ClientRequest( 'post', URL('http://python.org/'), headers={'TRANSFER-ENCODING': 'chunked'}, chunked=True, loop=loop)
def test_no_content_length2(self): req = ClientRequest('head', 'http://python.org', loop=self.loop) resp = req.send(self.transport, self.protocol) self.assertEqual('0', req.headers.get('CONTENT-LENGTH')) self.loop.run_until_complete(req.close()) resp.close()
def test_auth_utf8(self): proxy_req = ClientRequest( 'GET', URL('http://proxy.example.com'), auth=aiohttp.helpers.BasicAuth('юзер', 'пасс', 'utf-8'), loop=self.loop) self.assertIn('AUTHORIZATION', proxy_req.headers)
def test_content_type_skip_auto_header_form(self): req = ClientRequest('post', 'http://python.org', data={'hey': 'you'}, loop=self.loop, skip_auto_headers={'CONTENT-TYPE'}) resp = req.send(self.transport, self.protocol) self.assertNotIn('CONTENT-TYPE', req.headers) resp.close()
def test_https_connect_pass_ssl_context(self, ClientRequestMock) -> None: proxy_req = ClientRequest("GET", URL("http://proxy.example.com"), loop=self.loop) ClientRequestMock.return_value = proxy_req proxy_resp = ClientResponse( "get", URL("http://proxy.example.com"), request_info=mock.Mock(), writer=mock.Mock(), continue100=None, timer=TimerNoop(), traces=[], loop=self.loop, session=mock.Mock(), ) proxy_req.send = make_mocked_coro(proxy_resp) proxy_resp.start = make_mocked_coro(mock.Mock(status=200)) async def make_conn(): return aiohttp.TCPConnector() connector = self.loop.run_until_complete(make_conn()) connector._resolve_host = make_mocked_coro([{ "hostname": "hostname", "host": "127.0.0.1", "port": 80, "family": socket.AF_INET, "proto": 0, "flags": 0, }]) tr, proto = mock.Mock(), mock.Mock() self.loop.create_connection = make_mocked_coro((tr, proto)) req = ClientRequest( "GET", URL("https://www.python.org"), proxy=URL("http://proxy.example.com"), loop=self.loop, ) self.loop.run_until_complete( connector._create_connection(req, None, aiohttp.ClientTimeout())) self.loop.create_connection.assert_called_with( mock.ANY, ssl=connector._make_ssl_context(True), sock=mock.ANY, server_hostname="www.python.org", ) self.assertEqual(req.url.path, "/") self.assertEqual(proxy_req.method, "CONNECT") self.assertEqual(proxy_req.url, URL("https://www.python.org")) tr.close.assert_called_once_with() tr.get_extra_info.assert_called_with("socket", default=None) self.loop.run_until_complete(proxy_req.close()) proxy_resp.close() self.loop.run_until_complete(req.close())
def test_terminate_without_writer(self): req = ClientRequest('get', 'http://python.org', loop=self.loop) self.assertIsNone(req._writer) req.terminate() self.assertIsNone(req._writer)
def test_https_auth(self, ClientRequestMock) -> None: proxy_req = ClientRequest( "GET", URL("http://proxy.example.com"), auth=aiohttp.helpers.BasicAuth("user", "pass"), loop=self.loop, ) ClientRequestMock.return_value = proxy_req proxy_resp = ClientResponse( "get", URL("http://proxy.example.com"), request_info=mock.Mock(), writer=mock.Mock(), continue100=None, timer=TimerNoop(), traces=[], loop=self.loop, session=mock.Mock(), ) proxy_req.send = make_mocked_coro(proxy_resp) proxy_resp.start = make_mocked_coro(mock.Mock(status=200)) async def make_conn(): return aiohttp.TCPConnector() connector = self.loop.run_until_complete(make_conn()) connector._resolve_host = make_mocked_coro([{ "hostname": "hostname", "host": "127.0.0.1", "port": 80, "family": socket.AF_INET, "proto": 0, "flags": 0, }]) tr, proto = mock.Mock(), mock.Mock() self.loop.create_connection = make_mocked_coro((tr, proto)) self.assertIn("AUTHORIZATION", proxy_req.headers) self.assertNotIn("PROXY-AUTHORIZATION", proxy_req.headers) req = ClientRequest( "GET", URL("https://www.python.org"), proxy=URL("http://proxy.example.com"), loop=self.loop, ) self.assertNotIn("AUTHORIZATION", req.headers) self.assertNotIn("PROXY-AUTHORIZATION", req.headers) self.loop.run_until_complete( connector._create_connection(req, None, aiohttp.ClientTimeout())) self.assertEqual(req.url.path, "/") self.assertNotIn("AUTHORIZATION", req.headers) self.assertNotIn("PROXY-AUTHORIZATION", req.headers) self.assertNotIn("AUTHORIZATION", proxy_req.headers) self.assertIn("PROXY-AUTHORIZATION", proxy_req.headers) connector._resolve_host.assert_called_with("proxy.example.com", 80, traces=mock.ANY) self.loop.run_until_complete(proxy_req.close()) proxy_resp.close() self.loop.run_until_complete(req.close())
def test_bad_fingerprint(loop): with pytest.raises(ValueError): ClientRequest('get', URL('http://python.org'), fingerprint=b'invalid', loop=loop)
def test_terminate_without_writer(loop) -> None: req = ClientRequest("get", URL("http://python.org"), loop=loop) assert req._writer is None req.terminate() assert req._writer is None
def test_default_loop(loop): asyncio.set_event_loop(loop) req = ClientRequest('get', URL('http://python.org/')) assert req.loop is loop
async def test_no_content_length_head(loop, conn) -> None: req = ClientRequest("head", URL("http://python.org"), loop=loop) resp = await req.send(conn) assert req.headers.get("CONTENT-LENGTH") is None await req.close() resp.close()
def test_content_type_auto_header_get(loop, conn): req = ClientRequest('get', URL('http://python.org'), loop=loop) resp = req.send(conn) assert 'CONTENT-TYPE' not in req.headers resp.close()
def test_default_headers_useragent(self): req = ClientRequest('get', 'http://python.org/', loop=self.loop) self.assertNotIn('SERVER', req.headers) self.assertIn('USER-AGENT', req.headers)