Example #1
0
    def test_data_stream_continue(self):
        def gen():
            yield b'binary data'
            return b' result'

        req = ClientRequest(
            'POST', 'http://python.org/', data=gen(),
            expect100=True, loop=self.loop)
        self.assertTrue(req.chunked)
        self.assertTrue(inspect.isgenerator(req.body))

        def coro():
            yield from asyncio.sleep(0.0001, loop=self.loop)
            req._continue.set_result(1)

        asyncio.async(coro(), loop=self.loop)

        resp = req.send(self.transport, self.protocol)
        self.loop.run_until_complete(req._writer)
        self.assertEqual(
            self.transport.write.mock_calls[-3:],
            [unittest.mock.call(b'binary data result'),
             unittest.mock.call(b'\r\n'),
             unittest.mock.call(b'0\r\n\r\n')])
        self.loop.run_until_complete(req.close())
        resp.close()
Example #2
0
def test_urlencoded_formdata_charset(loop, conn):
    req = ClientRequest(
        'post', URL('http://python.org'),
        data=aiohttp.FormData({'hey': 'you'}, charset='koi8-r'), loop=loop)
    req.send(conn)
    assert 'application/x-www-form-urlencoded; charset=koi8-r' == \
        req.headers.get('CONTENT-TYPE')
Example #3
0
def test_data_stream_exc(loop, conn):
    fut = helpers.create_future(loop)

    @aiohttp.streamer
    def gen(writer):
        writer.write(b'binary data')
        yield from fut

    req = ClientRequest(
        'POST', URL('http://python.org/'), data=gen(), loop=loop)
    assert req.chunked
    assert req.headers['TRANSFER-ENCODING'] == 'chunked'

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(ValueError)

    helpers.ensure_future(exc(), loop=loop)

    req.send(conn)
    yield from req._writer
    # assert conn.close.called
    assert conn.protocol.set_exception.called
    yield from req.close()
Example #4
0
def test_content_type_auto_header_form(loop, conn):
    req = ClientRequest('post', URL('http://python.org'),
                        data={'hey': 'you'}, loop=loop)
    resp = req.send(conn)
    assert 'application/x-www-form-urlencoded' == \
        req.headers.get('CONTENT-TYPE')
    resp.close()
Example #5
0
def test_content_type_skip_auto_header_form(loop, conn):
    req = ClientRequest('post', URL('http://python.org'),
                        data={'hey': 'you'}, loop=loop,
                        skip_auto_headers={'Content-Type'})
    resp = req.send(conn)
    assert 'CONTENT-TYPE' not in req.headers
    resp.close()
Example #6
0
def test_data_stream_exc_chain(loop, conn):
    fut = helpers.create_future(loop)

    @aiohttp.streamer
    def gen(writer):
        yield from fut

    req = ClientRequest('POST', URL('http://python.org/'),
                        data=gen(), loop=loop)

    inner_exc = ValueError()

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(inner_exc)

    helpers.ensure_future(exc(), loop=loop)

    req.send(conn)
    yield from req._writer
    # assert connection.close.called
    assert conn.protocol.set_exception.called
    outer_exc = conn.protocol.set_exception.call_args[0][0]
    assert isinstance(outer_exc, ValueError)
    assert inner_exc is outer_exc
    assert inner_exc is outer_exc
    yield from req.close()
def test_expect_100_continue_header(loop):
    req = ClientRequest("get", URL("http://python.org/"), headers={"expect": "100-continue"}, loop=loop)
    resp = req.send(mock.Mock(), mock.Mock())
    assert "100-continue" == req.headers["EXPECT"]
    assert req._continue is not None
    req.terminate()
    resp.close()
def test_chunked_length(loop):
    req = ClientRequest("get", URL("http://python.org/"), headers={"CONTENT-LENGTH": "1000"}, chunked=1024, loop=loop)
    resp = req.send(mock.Mock(), mock.Mock())
    assert req.headers["TRANSFER-ENCODING"] == "chunked"
    assert "CONTENT-LENGTH" not in req.headers
    yield from req.close()
    resp.close()
Example #9
0
 def test_content_type_skip_auto_header_bytes(self):
     req = ClientRequest('post', 'http://python.org', data=b'hey you',
                         skip_auto_headers=set('CONTENT-TYPE'),
                         loop=self.loop)
     resp = req.send(self.transport, self.protocol)
     self.assertNotIn('application/octet-stream', req.headers)
     resp.close()
Example #10
0
    def test_data_stream_exc(self):
        fut = helpers.create_future(self.loop)

        def gen():
            yield b"binary data"
            yield from fut

        req = ClientRequest("POST", "http://python.org/", data=gen(), loop=self.loop)
        self.assertTrue(req.chunked)
        self.assertTrue(inspect.isgenerator(req.body))
        self.assertEqual(req.headers["TRANSFER-ENCODING"], "chunked")

        @asyncio.coroutine
        def exc():
            yield from asyncio.sleep(0.01, loop=self.loop)
            fut.set_exception(ValueError)

        asyncio.async(exc(), loop=self.loop)

        resp = req.send(self.transport, self.protocol)
        resp._connection = self.connection
        self.loop.run_until_complete(req._writer)
        self.assertTrue(self.connection.close.called)
        self.assertTrue(self.protocol.set_exception.called)
        self.loop.run_until_complete(req.close())
Example #11
0
 def test_chunked_explicit_size(self, m_http):
     req = ClientRequest("get", "http://python.org/", chunked=1024, loop=self.loop)
     resp = req.send(self.transport, self.protocol)
     self.assertEqual("chunked", req.headers["TRANSFER-ENCODING"])
     m_http.Request.return_value.add_chunking_filter.assert_called_with(1024)
     self.loop.run_until_complete(req.close())
     resp.close()
Example #12
0
def test_content_type_auto_header_content_length_no_skip(loop):
    req = ClientRequest(
        "get", URL("http://python.org"), data=io.BytesIO(b"hey"), skip_auto_headers={"Content-Length"}, loop=loop
    )
    resp = req.send(mock.Mock(), mock.Mock())
    assert req.headers.get("CONTENT-LENGTH") == "3"
    resp.close()
Example #13
0
 def test_basic_auth(self):
     req = ClientRequest('get', 'http://python.org',
                         auth=aiohttp.helpers.BasicAuth('nkim', '1234'),
                         loop=self.loop)
     self.assertIn('AUTHORIZATION', req.headers)
     self.assertEqual('Basic bmtpbToxMjM0', req.headers['AUTHORIZATION'])
     self.loop.run_until_complete(req.close())
Example #14
0
 def test_headers_list(self):
     req = ClientRequest('get', 'http://python.org/',
                         headers=[('Content-Type', 'text/plain')],
                         loop=self.loop)
     self.assertIn('CONTENT-TYPE', req.headers)
     self.assertEqual(req.headers['CONTENT-TYPE'], 'text/plain')
     self.loop.run_until_complete(req.close())
Example #15
0
 def test_content_type_auto_header_content_length_no_skip(self):
     req = ClientRequest('get', 'http://python.org',
                         data=io.BytesIO(b'hey'),
                         skip_auto_headers={'CONTENT-LENGTH'},
                         loop=self.loop)
     req.send(self.transport, self.protocol)
     self.assertEqual(req.headers.get('CONTENT-LENGTH'), '3')
Example #16
0
    def test_https_connect_runtime_error(self, ClientRequestMock):
        loop_mock = _create_mocked_loop()
        proxy_req = ClientRequest('GET', 'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_resp._loop = loop_mock
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(start_mock, unittest.mock.Mock(status=200))

        connector = aiohttp.TCPConnector(loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        tr.get_extra_info.return_value = None
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest(
            'GET', 'https://www.python.org',
            proxy='http://proxy.example.com',
            loop=self.loop,
        )
        with self.assertRaisesRegex(
                RuntimeError, "Transport does not expose socket instance"):
            self.loop.run_until_complete(connector._create_connection(req))

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
Example #17
0
 def test_expect_100_continue_header(self):
     req = ClientRequest("get", "http://python.org/", headers={"expect": "100-continue"}, loop=self.loop)
     resp = req.send(self.transport, self.protocol)
     self.assertEqual("100-continue", req.headers["EXPECT"])
     self.assertIsNotNone(req._continue)
     req.terminate()
     resp.close()
Example #18
0
 def test_content_type_auto_header_content_length_no_skip(self):
     req = ClientRequest(
         "get", "http://python.org", data=io.BytesIO(b"hey"), skip_auto_headers={"CONTENT-LENGTH"}, loop=self.loop
     )
     resp = req.send(self.transport, self.protocol)
     self.assertEqual(req.headers.get("CONTENT-LENGTH"), "3")
     resp.close()
Example #19
0
    def test_https_connect_http_proxy_error(self, ClientRequestMock):
        loop_mock = unittest.mock.Mock()
        proxy_req = ClientRequest('GET', 'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_resp._loop = loop_mock
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(
            start_mock, unittest.mock.Mock(status=400, reason='bad request'))

        connector = aiohttp.ProxyConnector(
            'http://proxy.example.com', loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        tr.get_extra_info.return_value = None
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'https://www.python.org', loop=self.loop)
        with self.assertRaisesRegex(
                aiohttp.HttpProxyError, "400, message='bad request'"):
            self.loop.run_until_complete(connector._create_connection(req))

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
Example #20
0
    def test_https_connect(self, ClientRequestMock):
        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'))
        proxy_resp._loop = self.loop
        proxy_req.send = send_mock = mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = make_mocked_coro(mock.Mock(status=200))

        connector = aiohttp.TCPConnector(loop=self.loop)
        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))

        self.assertEqual(req.url.path, '/')
        self.assertEqual(proxy_req.method, 'CONNECT')
        self.assertEqual(proxy_req.url, URL('https://www.python.org'))
        tr.pause_reading.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())
Example #21
0
    def test_https_connect_http_proxy_error(self, ClientRequestMock):
        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'))
        proxy_resp._loop = self.loop
        proxy_req.send = send_mock = mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = make_mocked_coro(
            mock.Mock(status=400, reason='bad request'))

        connector = aiohttp.TCPConnector(loop=self.loop)
        connector = aiohttp.TCPConnector(loop=self.loop)
        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()
        tr.get_extra_info.return_value = None
        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,
        )
        with self.assertRaisesRegex(
                aiohttp.HttpProxyError, "400, message='bad request'"):
            self.loop.run_until_complete(connector._create_connection(req))

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
Example #22
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()
Example #23
0
    def test_https_connect(self, ClientRequestMock):
        loop_mock = unittest.mock.Mock()
        proxy_req = ClientRequest('GET', 'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_resp._loop = loop_mock
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(start_mock, unittest.mock.Mock(status=200))

        connector = aiohttp.ProxyConnector(
            'http://proxy.example.com', loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'https://www.python.org', loop=self.loop)
        self.loop.run_until_complete(connector._create_connection(req))

        self.assertEqual(req.path, '/')
        self.assertEqual(proxy_req.method, 'CONNECT')
        self.assertEqual(proxy_req.path, 'www.python.org:443')
        tr.pause_reading.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())
Example #24
0
def test_data_stream_exc(loop):
    fut = helpers.create_future(loop)

    def gen():
        yield b'binary data'
        yield from fut

    req = ClientRequest(
        'POST', URL('http://python.org/'), data=gen(), loop=loop)
    assert req.chunked
    assert inspect.isgenerator(req.body)
    assert req.headers['TRANSFER-ENCODING'] == 'chunked'

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(ValueError)

    helpers.ensure_future(exc(), loop=loop)

    protocol = mock.Mock()
    resp = req.send(mock.Mock(), protocol)
    connection = mock.Mock()
    resp._connection = connection
    yield from req._writer
    assert connection.close.called
    assert protocol.set_exception.called
    yield from req.close()
Example #25
0
def test_data_stream_exc_chain(loop):
    fut = helpers.create_future(loop)

    def gen():
        yield from fut

    req = ClientRequest('POST', URL('http://python.org/'),
                        data=gen(), loop=loop)

    inner_exc = ValueError()

    @asyncio.coroutine
    def exc():
        yield from asyncio.sleep(0.01, loop=loop)
        fut.set_exception(inner_exc)

    helpers.ensure_future(exc(), loop=loop)

    protocol = mock.Mock()
    resp = req.send(mock.Mock(), protocol)
    connection = mock.Mock()
    resp._connection = connection
    yield from req._writer
    assert connection.close.called
    assert protocol.set_exception.called
    outer_exc = protocol.set_exception.call_args[0][0]
    assert isinstance(outer_exc, aiohttp.ClientRequestError)
    assert inner_exc is outer_exc.__context__
    assert inner_exc is outer_exc.__cause__
    yield from req.close()
Example #26
0
    def test_data_stream_exc_chain(self):
        fut = helpers.create_future(self.loop)

        def gen():
            yield from fut

        req = ClientRequest("POST", "http://python.org/", data=gen(), loop=self.loop)

        inner_exc = ValueError()

        @asyncio.coroutine
        def exc():
            yield from asyncio.sleep(0.01, loop=self.loop)
            fut.set_exception(inner_exc)

        asyncio.async(exc(), loop=self.loop)

        resp = req.send(self.transport, self.protocol)
        resp._connection = self.connection
        self.loop.run_until_complete(req._writer)
        self.assertTrue(self.connection.close.called)
        self.assertTrue(self.protocol.set_exception.called)
        outer_exc = self.protocol.set_exception.call_args[0][0]
        self.assertIsInstance(outer_exc, aiohttp.ClientRequestError)
        self.assertIs(inner_exc, outer_exc.__context__)
        self.assertIs(inner_exc, outer_exc.__cause__)
        self.loop.run_until_complete(req.close())
Example #27
0
def test_content_type_skip_auto_header_bytes(loop):
    req = ClientRequest('post', URL('http://python.org'), data=b'hey you',
                        skip_auto_headers={'Content-Type'},
                        loop=loop)
    resp = req.send(mock.Mock(), mock.Mock())
    assert 'CONTENT-TYPE' not in req.headers
    resp.close()
Example #28
0
 def test_content_encoding_dont_set_headers_if_no_body(self, m_http):
     req = ClientRequest("get", "http://python.org/", compress="deflate", loop=self.loop)
     resp = req.send(self.transport, self.protocol)
     self.assertNotIn("TRANSFER-ENCODING", req.headers)
     self.assertNotIn("CONTENT-ENCODING", req.headers)
     self.loop.run_until_complete(req.close())
     resp.close()
Example #29
0
 def test_content_type_auto_header_form(self):
     req = ClientRequest('post', 'http://python.org', data={'hey': 'you'},
                         loop=self.loop)
     resp = req.send(self.transport, self.protocol)
     self.assertEqual('application/x-www-form-urlencoded',
                      req.headers.get('CONTENT-TYPE'))
     resp.close()
Example #30
0
def test_data_stream_continue(loop):
    def gen():
        yield b'binary data'
        return b' result'

    req = ClientRequest(
        'POST', URL('http://python.org/'), data=gen(),
        expect100=True, loop=loop)
    assert req.chunked
    assert inspect.isgenerator(req.body)

    def coro():
        yield from asyncio.sleep(0.0001, loop=loop)
        req._continue.set_result(1)

    helpers.ensure_future(coro(), loop=loop)

    transport = mock.Mock()
    resp = req.send(transport, mock.Mock())
    yield from req._writer
    assert transport.write.mock_calls[-2:] == [
        mock.call(b'12\r\nbinary data result\r\n'),
        mock.call(b'0\r\n\r\n')]
    yield from req.close()
    resp.close()
Example #31
0
def test_content_type_auto_header_get(loop):
    req = ClientRequest('get', URL('http://python.org'), loop=loop)
    resp = req.send(mock.Mock(), mock.Mock())
    assert 'CONTENT-TYPE' not in req.headers
    resp.close()
Example #32
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()
Example #33
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()
Example #34
0
    def test_https_connect_pass_ssl_context(self,
                                            ClientRequestMock: Any) -> 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))
        self.loop.start_tls = make_mocked_coro(mock.Mock())

        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.start_tls.assert_called_with(
            mock.ANY,
            mock.ANY,
            connector._make_ssl_context(True),
            server_hostname="www.python.org",
            ssl_handshake_timeout=mock.ANY,
        )

        self.assertEqual(req.url.path, "/")
        self.assertEqual(proxy_req.method, "CONNECT")
        self.assertEqual(proxy_req.url, URL("https://www.python.org"))

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
async def test_content_type_auto_header_get(loop: Any, conn: Any) -> None:
    req = ClientRequest("get", URL("http://python.org"), loop=loop)
    resp = await req.send(conn)
    assert "CONTENT-TYPE" not in req.headers
    resp.close()
Example #36
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)
Example #37
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())
async def test_no_content_length_head(loop, conn):
    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()
Example #39
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())
Example #40
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()
Example #41
0
    def test_https_auth(self, ClientRequestMock: Any) -> 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.loop.start_tls = make_mocked_coro(mock.Mock())

        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())
Example #42
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()
async def test_content_type_auto_header_get(loop, conn):
    req = ClientRequest('get', URL('http://python.org'), loop=loop)
    resp = await req.send(conn)
    assert 'CONTENT-TYPE' not in req.headers
    resp.close()
Example #44
0
 def maker(*args, **kwargs):
     nonlocal request
     request = ClientRequest(*args, loop=loop, **kwargs)
     return request
def test_terminate_without_writer(loop: Any) -> None:
    req = ClientRequest("get", URL("http://python.org"), loop=loop)
    assert req._writer is None

    req.terminate()
    assert req._writer is None
Example #46
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()
Example #47
0
 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: Any, conn: Any) -> 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()
Example #49
0
def test_connection_header(loop, conn):
    req = ClientRequest('get', URL('http://python.org'), loop=loop)
    req.keep_alive = mock.Mock()
    req.headers.clear()

    req.keep_alive.return_value = True
    req.version = (1, 1)
    req.headers.clear()
    req.send(conn)
    assert req.headers.get('CONNECTION') is None

    req.version = (1, 0)
    req.headers.clear()
    req.send(conn)
    assert req.headers.get('CONNECTION') == 'keep-alive'

    req.keep_alive.return_value = False
    req.version = (1, 1)
    req.headers.clear()
    req.send(conn)
    assert req.headers.get('CONNECTION') == 'close'
Example #50
0
def test_bad_fingerprint(loop):
    with pytest.raises(ValueError):
        ClientRequest('get',
                      URL('http://python.org'),
                      fingerprint=b'invalid',
                      loop=loop)
Example #51
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
Example #52
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)
Example #53
0
def test_default_loop(loop):
    asyncio.set_event_loop(loop)
    req = ClientRequest('get', URL('http://python.org/'))
    assert req.loop is loop
Example #54
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)
Example #55
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()
Example #56
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)
Example #57
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()
Example #58
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()
Example #59
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)
Example #60
0
    def test_host_header(self):
        req = ClientRequest('get', 'http://python.org/', loop=self.loop)
        self.assertEqual(req.headers['HOST'], 'python.org')
        self.loop.run_until_complete(req.close())

        req = ClientRequest('get', 'http://python.org:80/', loop=self.loop)
        self.assertEqual(req.headers['HOST'], 'python.org:80')
        self.loop.run_until_complete(req.close())

        req = ClientRequest('get', 'http://python.org:99/', loop=self.loop)
        self.assertEqual(req.headers['HOST'], 'python.org:99')
        self.loop.run_until_complete(req.close())

        req = ClientRequest('get',
                            'http://python.org/',
                            headers={'host': 'example.com'},
                            loop=self.loop)
        self.assertEqual(req.headers['HOST'], 'example.com')
        self.loop.run_until_complete(req.close())

        req = ClientRequest('get',
                            'http://python.org/',
                            headers={'host': 'example.com:99'},
                            loop=self.loop)
        self.assertEqual(req.headers['HOST'], 'example.com:99')
        self.loop.run_until_complete(req.close())