test_aiohttp.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # test_aiohttp.py -- Tests for the aiohttp HTTP server
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for the aiohttp Git HTTP server."""
  22. try:
  23. from aiohttp.test_utils import AioHTTPTestCase
  24. aiohttp_missing = False
  25. except ImportError:
  26. from unittest import TestCase as AioHTTPTestCase # type: ignore
  27. aiohttp_missing = True
  28. from dulwich.objects import Blob
  29. from dulwich.repo import MemoryRepo
  30. from dulwich.tests.utils import make_object
  31. from . import skipIf
  32. if not aiohttp_missing:
  33. from dulwich.aiohttp.server import create_repo_app
  34. @skipIf(aiohttp_missing, "aiohttp not available")
  35. class AiohttpAppTestCase(AioHTTPTestCase): # type: ignore
  36. """Test the aiohttp application."""
  37. def setUp(self):
  38. super().setUp()
  39. self.repo = MemoryRepo.init_bare([], {})
  40. self.blob = make_object(Blob, data=b"blob contents")
  41. self.repo.object_store.add_object(self.blob)
  42. self.repo.refs[b"refs/heads/master"] = self.blob.id
  43. def get_app(self):
  44. return create_repo_app(self.repo)
  45. async def test_get_info_refs_dumb(self):
  46. """Test GET /info/refs without service parameter (dumb protocol)."""
  47. resp = await self.client.request("GET", "/info/refs")
  48. self.assertEqual(resp.status, 200)
  49. self.assertEqual(resp.content_type, "text/plain")
  50. text = await resp.text()
  51. self.assertIn(self.blob.id.decode("ascii"), text)
  52. self.assertIn("refs/heads/master", text)
  53. async def test_get_info_refs_smart(self):
  54. """Test GET /info/refs?service=git-upload-pack (smart protocol)."""
  55. resp = await self.client.request("GET", "/info/refs?service=git-upload-pack")
  56. self.assertEqual(resp.status, 200)
  57. self.assertIn("git-upload-pack", resp.content_type)
  58. async def test_post_upload_pack(self):
  59. """Test POST /git-upload-pack."""
  60. # Simple test that the endpoint exists and accepts POST
  61. resp = await self.client.request(
  62. "POST",
  63. "/git-upload-pack",
  64. data=b"0000",
  65. headers={"Content-Type": "application/x-git-upload-pack-request"},
  66. )
  67. # Should respond with 200 even for invalid/minimal input
  68. self.assertEqual(resp.status, 200)
  69. self.assertIn("git-upload-pack", resp.content_type)
  70. @skipIf(aiohttp_missing, "aiohttp not available")
  71. class AiohttpDumbAppTestCase(AioHTTPTestCase): # type: ignore
  72. """Test the aiohttp application with dumb protocol."""
  73. def setUp(self):
  74. super().setUp()
  75. self.repo = MemoryRepo.init_bare([], {})
  76. self.repo._put_named_file("HEAD", b"ref: refs/heads/master\n")
  77. self.blob = make_object(Blob, data=b"blob contents")
  78. self.repo.object_store.add_object(self.blob)
  79. self.repo.refs[b"refs/heads/master"] = self.blob.id
  80. def get_app(self):
  81. return create_repo_app(self.repo, dumb=True)
  82. async def test_get_head(self):
  83. """Test GET /HEAD."""
  84. resp = await self.client.request("GET", "/HEAD")
  85. self.assertEqual(resp.status, 200)
  86. self.assertEqual(resp.content_type, "text/plain")
  87. text = await resp.text()
  88. self.assertEqual(text, "ref: refs/heads/master\n")
  89. async def test_get_info_packs(self):
  90. """Test GET /objects/info/packs."""
  91. resp = await self.client.request("GET", "/objects/info/packs")
  92. self.assertEqual(resp.status, 200)
  93. self.assertEqual(resp.content_type, "text/plain")
  94. async def test_get_loose_object(self):
  95. """Test GET /objects/{dir}/{file} for loose objects."""
  96. sha = self.blob.id.decode("ascii")
  97. dir_part = sha[:2]
  98. file_part = sha[2:]
  99. resp = await self.client.request("GET", f"/objects/{dir_part}/{file_part}")
  100. self.assertEqual(resp.status, 200)
  101. self.assertEqual(resp.content_type, "application/x-git-loose-object")
  102. body = await resp.read()
  103. self.assertEqual(body, self.blob.as_legacy_object())
  104. async def test_get_loose_object_not_found(self):
  105. """Test GET /objects/{dir}/{file} for non-existent object."""
  106. resp = await self.client.request("GET", "/objects/ab/cdef" + "0" * 36)
  107. self.assertEqual(resp.status, 404)