2567
2567
output.getvalue())
2569
def test_call_writes_just_once(self):
2570
"""A bodyless request is written to the medium all at once."""
2571
medium_request = StubMediumRequest()
2572
encoder = protocol.ProtocolThreeRequester(medium_request)
2573
encoder.call('arg1', 'arg2', 'arg3')
2575
['accept_bytes', 'finished_writing'], medium_request.calls)
2577
def test_call_with_body_bytes_writes_just_once(self):
2578
"""A request with body bytes is written to the medium all at once."""
2579
medium_request = StubMediumRequest()
2580
encoder = protocol.ProtocolThreeRequester(medium_request)
2581
encoder.call_with_body_bytes(('arg', 'arg'), 'body bytes')
2583
['accept_bytes', 'finished_writing'], medium_request.calls)
2586
class StubMediumRequest(object):
2587
"""A stub medium request that tracks the number of times accept_bytes is
2592
def accept_bytes(self, bytes):
2593
self.calls.append('accept_bytes')
2594
def finished_writing(self):
2595
self.calls.append('finished_writing')
2570
2598
class TestResponseEncodingProtocolThree(tests.TestCase):
2620
class TestResponseEncoderBufferingProtocolThree(tests.TestCase):
2621
"""Tests for buffering of responses.
2623
We want to avoid doing many small writes when one would do, to avoid
2624
unnecessary network overhead.
2629
self.responder = protocol.ProtocolThreeResponder(self.writes.append)
2631
def assertWriteCount(self, expected_count):
2633
expected_count, len(self.writes),
2634
"Too many writes: %r" % (self.writes,))
2636
def test_send_error_writes_just_once(self):
2637
"""An error response is written to the medium all at once."""
2638
self.responder.send_error(Exception('An exception string.'))
2639
self.assertWriteCount(1)
2641
def test_send_response_writes_just_once(self):
2642
"""A normal response with no body is written to the medium all at once.
2644
response = _mod_request.SuccessfulSmartServerResponse(('arg', 'arg'))
2645
self.responder.send_response(response)
2646
self.assertWriteCount(1)
2648
def test_send_response_with_body_writes_just_once(self):
2649
"""A normal response with a monolithic body is written to the medium
2652
response = _mod_request.SuccessfulSmartServerResponse(
2653
('arg', 'arg'), body='body bytes')
2654
self.responder.send_response(response)
2655
self.assertWriteCount(1)
2657
def test_send_response_with_body_stream_writes_once_per_chunk(self):
2658
"""A normal response with a stream body is written to the medium
2659
writes to the medium once per chunk.
2661
# Construct a response with stream with 2 chunks in it.
2662
response = _mod_request.SuccessfulSmartServerResponse(
2663
('arg', 'arg'), body_stream=['chunk1', 'chunk2'])
2664
self.responder.send_response(response)
2665
# We will write 3 times: exactly once for each chunk, plus a final
2666
# write to end the response.
2667
self.assertWriteCount(3)
2592
2670
class TestSmartClientUnicode(tests.TestCase):
2593
2671
"""_SmartClient tests for unicode arguments.