1
0

test_client.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2021 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. from io import BytesIO
  15. from mock import Mock
  16. from twisted.python.failure import Failure
  17. from twisted.web.client import ResponseDone
  18. from synapse.http.client import BodyExceededMaxSize, read_body_with_max_size
  19. from tests.unittest import TestCase
  20. class ReadBodyWithMaxSizeTests(TestCase):
  21. def setUp(self):
  22. """Start reading the body, returns the response, result and proto"""
  23. self.response = Mock()
  24. self.result = BytesIO()
  25. self.deferred = read_body_with_max_size(self.response, self.result, 6)
  26. # Fish the protocol out of the response.
  27. self.protocol = self.response.deliverBody.call_args[0][0]
  28. self.protocol.transport = Mock()
  29. def _cleanup_error(self):
  30. """Ensure that the error in the Deferred is handled gracefully."""
  31. called = [False]
  32. def errback(f):
  33. called[0] = True
  34. self.deferred.addErrback(errback)
  35. self.assertTrue(called[0])
  36. def test_no_error(self):
  37. """A response that is NOT too large."""
  38. # Start sending data.
  39. self.protocol.dataReceived(b"12345")
  40. # Close the connection.
  41. self.protocol.connectionLost(Failure(ResponseDone()))
  42. self.assertEqual(self.result.getvalue(), b"12345")
  43. self.assertEqual(self.deferred.result, 5)
  44. def test_too_large(self):
  45. """A response which is too large raises an exception."""
  46. # Start sending data.
  47. self.protocol.dataReceived(b"1234567890")
  48. # Close the connection.
  49. self.protocol.connectionLost(Failure(ResponseDone()))
  50. self.assertEqual(self.result.getvalue(), b"1234567890")
  51. self.assertIsInstance(self.deferred.result, Failure)
  52. self.assertIsInstance(self.deferred.result.value, BodyExceededMaxSize)
  53. self._cleanup_error()
  54. def test_multiple_packets(self):
  55. """Data should be accummulated through mutliple packets."""
  56. # Start sending data.
  57. self.protocol.dataReceived(b"12")
  58. self.protocol.dataReceived(b"34")
  59. # Close the connection.
  60. self.protocol.connectionLost(Failure(ResponseDone()))
  61. self.assertEqual(self.result.getvalue(), b"1234")
  62. self.assertEqual(self.deferred.result, 4)
  63. def test_additional_data(self):
  64. """A connection can receive data after being closed."""
  65. # Start sending data.
  66. self.protocol.dataReceived(b"1234567890")
  67. self.assertIsInstance(self.deferred.result, Failure)
  68. self.assertIsInstance(self.deferred.result.value, BodyExceededMaxSize)
  69. self.protocol.transport.loseConnection.assert_called_once()
  70. # More data might have come in.
  71. self.protocol.dataReceived(b"1234567890")
  72. # Close the connection.
  73. self.protocol.connectionLost(Failure(ResponseDone()))
  74. self.assertEqual(self.result.getvalue(), b"1234567890")
  75. self.assertIsInstance(self.deferred.result, Failure)
  76. self.assertIsInstance(self.deferred.result.value, BodyExceededMaxSize)
  77. self._cleanup_error()