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())
Esempio n. 2
0
 def maker(method, url, *args, **kwargs):
     nonlocal request
     request = ClientRequest(method, URL(url), *args, loop=loop, **kwargs)
     return request
Esempio n. 3
0
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()
Esempio n. 4
0
 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())
Esempio n. 5
0
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
Esempio n. 6
0
 def maker(*args, **kwargs):
     nonlocal request
     request = ClientRequest(*args, loop=loop, **kwargs)
     return request
Esempio n. 7
0
 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()
Esempio n. 8
0
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()
Esempio n. 9
0
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)
Esempio n. 10
0
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()
Esempio n. 11
0
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()
Esempio n. 12
0
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()
Esempio n. 13
0
 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)
Esempio n. 14
0
 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())
Esempio n. 15
0
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()
Esempio n. 16
0
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)
Esempio n. 17
0
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()
Esempio n. 18
0
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)
Esempio n. 19
0
 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()
Esempio n. 20
0
 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)
Esempio n. 21
0
 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()
Esempio n. 22
0
    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())
Esempio n. 23
0
    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)
Esempio n. 24
0
    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())
Esempio n. 25
0
def test_bad_fingerprint(loop):
    with pytest.raises(ValueError):
        ClientRequest('get',
                      URL('http://python.org'),
                      fingerprint=b'invalid',
                      loop=loop)
Esempio n. 26
0
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
Esempio n. 27
0
def test_default_loop(loop):
    asyncio.set_event_loop(loop)
    req = ClientRequest('get', URL('http://python.org/'))
    assert req.loop is loop
Esempio n. 28
0
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()
Esempio n. 29
0
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)