示例#1
0
    def test_get_and_remove_conn(self):
        """
        Each requested connection must be closed by calling 'remove_connection'
        when the server doesn't support persistent HTTP Connections
        """
        kah = self.kahdler
        host = self.host
        conn = self.conn
        req = self.req

        req.get_host = MagicMock(return_value=host)
        req.get_full_url = MagicMock(return_value='test_full_url')

        # Override KeepAliveHandler._start_transaction
        kah._start_transaction = MagicMock(return_value=None)

        conn_factory = kah.get_connection

        # Mock conn's getresponse()
        resp = HTTPResponse(socket.socket())
        resp.will_close = True
        resp.read = MagicMock(return_value='Response body')
        conn.getresponse = MagicMock(return_value=resp)

        # The connection mgr
        conn_mgr_mock = Mock()
        conn_mgr_mock.get_available_connection = MagicMock(return_value=conn)
        conn_mgr_mock.remove_connection = MagicMock(return_value=None)

        # Replace with mocked out ConnMgr.
        kah._cm = conn_mgr_mock
        kah.do_open(req)

        ## Verify ##
        kah._start_transaction.assert_called_once_with(conn, req)
        conn_mgr_mock.get_available_connection.assert_called_once_with(req,
                                                                       conn_factory)
        conn_mgr_mock.remove_connection.assert_called_once_with(conn,
                                                                host,
                                                                reason='will close')
示例#2
0
    def test_get_and_remove_conn(self):
        """
        Each requested connection must be closed by calling 'remove_connection'
        when the server doesn't support persistent HTTP Connections
        """
        kah = self.kahdler
        host = self.host
        conn = self.conn
        req = self.req

        req.get_host = MagicMock(return_value=host)
        req.get_full_url = MagicMock(return_value='test_full_url')

        # Override KeepAliveHandler._start_transaction
        kah._start_transaction = MagicMock(return_value=None)

        conn_factory = kah.get_connection

        # Mock conn's getresponse()
        resp = HTTPResponse(socket.socket())
        resp.will_close = True
        resp.read = MagicMock(return_value='Response body')
        conn.getresponse = MagicMock(return_value=resp)

        # The connection mgr
        conn_mgr_mock = Mock()
        conn_mgr_mock.get_available_connection = MagicMock(return_value=conn)
        conn_mgr_mock.remove_connection = MagicMock(return_value=None)

        # Replace with mocked out ConnMgr.
        kah._cm = conn_mgr_mock
        kah.do_open(req)

        ## Verify ##
        kah._start_transaction.assert_called_once_with(conn, req)
        conn_mgr_mock.get_available_connection.assert_called_once_with(
            req, conn_factory)
        conn_mgr_mock.remove_connection.assert_called_once_with(
            conn, host, reason='will close')