1
0

test_errors.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright 2023 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import json
  15. from synapse.api.errors import LimitExceededError
  16. from tests import unittest
  17. class LimitExceededErrorTestCase(unittest.TestCase):
  18. def test_key_appears_in_context_but_not_error_dict(self) -> None:
  19. err = LimitExceededError("needle")
  20. serialised = json.dumps(err.error_dict(None))
  21. self.assertIn("needle", err.debug_context)
  22. self.assertNotIn("needle", serialised)
  23. # Create a sub-class to avoid mutating the class-level property.
  24. class LimitExceededErrorHeaders(LimitExceededError):
  25. include_retry_after_header = True
  26. def test_limit_exceeded_header(self) -> None:
  27. err = self.LimitExceededErrorHeaders(limiter_name="test", retry_after_ms=100)
  28. self.assertEqual(err.error_dict(None).get("retry_after_ms"), 100)
  29. assert err.headers is not None
  30. self.assertEqual(err.headers.get("Retry-After"), "1")
  31. def test_limit_exceeded_rounding(self) -> None:
  32. err = self.LimitExceededErrorHeaders(limiter_name="test", retry_after_ms=3001)
  33. self.assertEqual(err.error_dict(None).get("retry_after_ms"), 3001)
  34. assert err.headers is not None
  35. self.assertEqual(err.headers.get("Retry-After"), "4")