From 2134384169e9f0d279dcf836fb0581820ced1c80 Mon Sep 17 00:00:00 2001 From: jomae Date: Tue, 15 Oct 2024 01:21:12 +0000 Subject: [PATCH] 1.6.1dev: fix diff view to send `bytes` instance and `TypeError` not raises when `term` parameter is missing (closes #13794) git-svn-id: http://trac.edgewall.org/intertrac/log:/branches/1.6-stable@17859 af82e41b-90c4-0310-8c96-b1721e28e2e2 --- trac/versioncontrol/web_ui/changeset.py | 4 +-- trac/versioncontrol/web_ui/tests/changeset.py | 29 ++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/trac/versioncontrol/web_ui/changeset.py b/trac/versioncontrol/web_ui/changeset.py index 874b4a93a0..30fdc860e7 100644 --- a/trac/versioncontrol/web_ui/changeset.py +++ b/trac/versioncontrol/web_ui/changeset.py @@ -1173,7 +1173,7 @@ def process_request(self, req): rm = RepositoryManager(self.env) if req.is_xhr: - dirname, prefix = posixpath.split(req.args.get('term')) + dirname, prefix = posixpath.split(req.args.get('term') or '') prefix = prefix.lower() reponame, repos, path = rm.get_repository_by_path(dirname) # an entry is a (isdir, name, path) tuple @@ -1196,7 +1196,7 @@ def kind_order(entry): for isdir, name, path in sorted(entries, key=kind_order) if name.lower().startswith(prefix)] - content = to_json(paths) + content = to_json(paths).encode('utf-8') req.send(content, 'application/json', 200) # -- retrieve arguments diff --git a/trac/versioncontrol/web_ui/tests/changeset.py b/trac/versioncontrol/web_ui/tests/changeset.py index b48179d339..f3d512dabc 100644 --- a/trac/versioncontrol/web_ui/tests/changeset.py +++ b/trac/versioncontrol/web_ui/tests/changeset.py @@ -15,7 +15,8 @@ from trac.core import TracError from trac.test import EnvironmentStub, MockRequest, makeSuite -from trac.versioncontrol.web_ui.changeset import ChangesetModule +from trac.versioncontrol.web_ui.changeset import AnyDiffModule, ChangesetModule +from trac.web.api import RequestDone class ChangesetModuleTestCase(unittest.TestCase): @@ -30,9 +31,35 @@ def test_default_repository_not_configured(self): self.assertRaises(TracError, self.cm.process_request, req) +class AnyDiffModuleTestCase(unittest.TestCase): + + def setUp(self): + self.env = EnvironmentStub() + self.mod = AnyDiffModule(self.env) + + def test_normal(self): + req = MockRequest(self.env, path_info='/diff', args={'term': '/'}) + req.environ['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' + self.assertTrue(self.mod.match_request(req)) + self.assertRaises(RequestDone, self.mod.process_request, req) + self.assertEqual(b'[]', req.response_sent.getvalue()) + self.assertEqual('application/json;charset=utf-8', + req.headers_sent.get('Content-Type')) + + def test_without_term(self): + req = MockRequest(self.env, path_info='/diff') + req.environ['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' + self.assertTrue(self.mod.match_request(req)) + self.assertRaises(RequestDone, self.mod.process_request, req) + self.assertEqual(b'[]', req.response_sent.getvalue()) + self.assertEqual('application/json;charset=utf-8', + req.headers_sent.get('Content-Type')) + + def test_suite(): suite = unittest.TestSuite() suite.addTest(makeSuite(ChangesetModuleTestCase)) + suite.addTest(makeSuite(AnyDiffModuleTestCase)) return suite