示例#1
0
 def setUp(self):
     self.wsgid = Wsgid()
     self.sample_headers = {
         'METHOD': 'GET',
         'VERSION': 'HTTP/1.1',
         'PATTERN': '/root',
         'URI': '/more/path/',
         'PATH': '/more/path',
         'QUERY': 'a=1&b=4&d=4',
         'host': 'localhost',
         'content-length': '42',
         'content-type': 'text/plain',
         'x-forwarded-for': '127.0.0.1'
     }
示例#2
0
    def test_protect_against_exception_on_file_removal(self):
        with patch('zmq.Context'):
            with patch('os.unlink') as mock_unlink:
                mock_unlink.side_effect = OSError("File does not exist")

                def _serve_request(wsgid, m2message):
                    with patch.object(wsgid, '_create_wsgi_environ'):
                        wsgid._create_wsgi_environ.return_value = {}
                        with patch("__builtin__.open") as mock_open:
                            wsgid._call_wsgi_app(message, Mock())

                wsgid = Wsgid(app=Mock(return_value=['body response']))
                wsgid.log = Mock()
                message = self._create_fake_m2message('/uploads/m2.84Yet4')
                _serve_request(wsgid, message)
                self.assertEquals(1, wsgid.log.exception.call_count)
示例#3
0
    def test_remove_async_file_after_failed_request(self):
        # Even if the request failed, wsgid must remove the temporary file.
        with patch('zmq.Context'):
            with patch('os.unlink') as mock_unlink:

                def _serve_request(wsgid, m2message):
                    with patch.object(wsgid, '_create_wsgi_environ'):
                        wsgid._create_wsgi_environ.return_value = {}
                        with patch("__builtin__.open") as mock_open:
                            wsgid._call_wsgi_app(message, Mock())

                wsgid = Wsgid(app=Mock(side_effect=Exception("Failed")))
                wsgid.log = Mock()
                message = self._create_fake_m2message('/uploads/m2.84Yet4')
                _serve_request(wsgid, message)
                mock_unlink.assert_called_with('/uploads/m2.84Yet4')
示例#4
0
    def test_remove_async_file_after_request_finishes_ok(self):
        # Since mongrel2 does not remove the originial temp file, wsgid
        # must remove it after the request was successfully (or not) handled.
        with patch('zmq.Context'):
            with patch('os.unlink') as mock_unlink:

                def _serve_request(wsgid, m2message):
                    with patch.object(wsgid, '_create_wsgi_environ'):
                        wsgid._create_wsgi_environ.return_value = {}
                        with patch("__builtin__.open") as mock_open:
                            wsgid._call_wsgi_app(message, Mock())

                wsgid = Wsgid(app=Mock(return_value=['body response']))

                message = self._create_fake_m2message('/uploads/m2.84Yet4')
                _serve_request(wsgid, message)
                mock_unlink.assert_called_with('/uploads/m2.84Yet4')
示例#5
0
    def test_do_not_try_to_remove_if_not_upload_request(self):
        with patch('zmq.Context'):
            with patch('os.unlink') as mock_unlink:

                def _serve_request(wsgid, m2message):
                    with patch.object(wsgid, '_create_wsgi_environ'):
                        wsgid._create_wsgi_environ.return_value = {}
                        with patch("__builtin__.open") as mock_open:
                            wsgid._call_wsgi_app(message, Mock())

                wsgid = Wsgid(app=Mock(return_value=['body response']))
                wsgid.log = Mock()
                message = Mock()
                message.headers = []  #It's not an upload message
                message.client_id = 'uuid'
                message.server_id = '1'
                message.is_upload_done.return_value = False
                _serve_request(wsgid, message)
                self.assertEquals(0, mock_unlink.call_count)
示例#6
0
    def test_join_m2_chroot_to_async_upload_path(self):
        # The value in x-mongrel2-upload-{start,done} should be prepended with the
        # value of --m2-chroot, passed on the command line
        with patch('zmq.Context'):

            def _serve_request(wsgid, m2message, expected_final_path):
                with patch.object(wsgid, '_create_wsgi_environ'):
                    wsgid._create_wsgi_environ.return_value = {}
                    with patch("__builtin__.open") as mock_open:
                        with patch('os.unlink'):
                            wsgid._call_wsgi_app(message, Mock())
                            self.assertEquals(1, mock_open.call_count)
                            mock_open.assert_called_with(expected_final_path)

            self._reparse_options('--mongrel2-chroot=/var/mongrel2')
            wsgid = Wsgid(app=Mock(return_value=['body response']))

            message = self._create_fake_m2message('/uploads/m2.84Yet4')
            _serve_request(wsgid, message, '/var/mongrel2/uploads/m2.84Yet4')
            self._reparse_options()
            _serve_request(wsgid, message, '/uploads/m2.84Yet4')
示例#7
0
 def setUp(self):
     self.wsgid = Wsgid()
     self.sample_uuid = 'bb3ce668-4528-11e0-94e3-001fe149503a'
     self.sample_conn_id = '42'