def test_incomplete_request(self): transport = FakeTransport() wsgi_app = wsgi.SmartWSGIApp(transport) def make_request(transport, write_func, bytes, root_client_path): request = IncompleteRequest(transport, write_func) request.accept_bytes(bytes) self.request = request return request wsgi_app.make_request = make_request fake_input = StringIO('incomplete request') environ = self.build_environ({ 'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(fake_input.getvalue()), 'wsgi.input': fake_input, 'bzrlib.relpath': 'foo/bar', }) iterable = wsgi_app(environ, self.start_response) response = self.read_response(iterable) self.assertEqual('200 OK', self.status) self.assertEqual('error\x01incomplete request\n', response)
def test_jail_root(self): """The WSGI HPSS glue allows access to the whole WSGI backing transport, regardless of which HTTP path the request was delivered to. """ # make a branch in a shared repo self.make_repository('repo', shared=True) branch = self.make_bzrdir('repo/branch').create_branch() # serve the repo via bzr+http WSGI wsgi_app = wsgi.SmartWSGIApp(self.get_transport()) # send a request to /repo/branch that will have to access /repo. environ = self.make_hpss_wsgi_request('/repo/branch', 'BzrDir.open_branchV2', '.') iterable = wsgi_app(environ, self.start_response) response_bytes = self.read_response(iterable) self.assertEqual('200 OK', self.status) # expect a successful response, rather than a jail break error from bzrlib.tests.test_smart_transport import LoggingMessageHandler message_handler = LoggingMessageHandler() decoder = protocol.ProtocolThreeDecoder(message_handler, expect_version_marker=True) decoder.accept_bytes(response_bytes) self.assertTrue( ('structure', ('branch', branch._format.network_name())) in message_handler.event_log)
def test_http_get_rejected(self): # GET requests are rejected. app = wsgi.SmartWSGIApp(FakeTransport()) environ = self.build_environ({'REQUEST_METHOD': 'GET'}) iterable = app(environ, self.start_response) self.read_response(iterable) self.assertEqual('405 Method not allowed', self.status) self.assertTrue(('Allow', 'POST') in self.headers)
def test_protocol_version_detection_one(self): # SmartWSGIApp detects requests that don't start with # REQUEST_VERSION_TWO as version one. transport = memory.MemoryTransport() wsgi_app = wsgi.SmartWSGIApp(transport) fake_input = StringIO('hello\n') environ = self.build_environ({ 'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(fake_input.getvalue()), 'wsgi.input': fake_input, 'bzrlib.relpath': 'foo', }) iterable = wsgi_app(environ, self.start_response) response = self.read_response(iterable) self.assertEqual('200 OK', self.status) # Expect a version 1-encoded response. self.assertEqual('ok\x012\n', response)
def test_smart_wsgi_app_uses_given_relpath(self): # The SmartWSGIApp should use the "bzrlib.relpath" field from the # WSGI environ to clone from its backing transport to get a specific # transport for this request. transport = FakeTransport() wsgi_app = wsgi.SmartWSGIApp(transport) wsgi_app.backing_transport = transport wsgi_app.make_request = self._fake_make_request fake_input = StringIO('fake request') environ = self.build_environ({ 'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(fake_input.getvalue()), 'wsgi.input': fake_input, 'bzrlib.relpath': 'foo/bar', }) iterable = wsgi_app(environ, self.start_response) response = self.read_response(iterable) self.assertEqual([('clone', 'foo/bar/')], transport.calls)
def test_smart_wsgi_app_request_and_response(self): # SmartWSGIApp reads the smart request from the 'wsgi.input' file-like # object in the environ dict, and returns the response via the iterable # returned to the WSGI handler. transport = memory.MemoryTransport() transport.put_bytes('foo', 'some bytes') wsgi_app = wsgi.SmartWSGIApp(transport) wsgi_app.make_request = self._fake_make_request fake_input = StringIO('fake request') environ = self.build_environ({ 'REQUEST_METHOD': 'POST', 'CONTENT_LENGTH': len(fake_input.getvalue()), 'wsgi.input': fake_input, 'bzrlib.relpath': 'foo', }) iterable = wsgi_app(environ, self.start_response) response = self.read_response(iterable) self.assertEqual('200 OK', self.status) self.assertEqual('got bytes: fake request', response)
def test_construct(self): app = wsgi.SmartWSGIApp(FakeTransport()) self.assertIsInstance(app.backing_transport, chroot.ChrootTransport)