732
733
server._serve_one_request(SampleRequest('x'))
733
734
self.assertTrue(server.finished)
736
def test_socket_stream_incomplete_request(self):
737
"""The medium should still construct the right protocol version even if
738
the initial read only reads part of the request.
740
Specifically, it should correctly read the protocol version line even
741
if the partial read doesn't end in a newline. An older, naive
742
implementation of _get_line in the server used to have a bug in that
745
incomplete_request_bytes = protocol.REQUEST_VERSION_TWO + 'hel'
746
rest_of_request_bytes = 'lo\n'
747
expected_response = (
748
protocol.RESPONSE_VERSION_TWO + 'success\nok\x012\n')
749
server_sock, client_sock = self.portable_socket_pair()
750
server = medium.SmartServerSocketStreamMedium(
752
client_sock.sendall(incomplete_request_bytes)
753
server_protocol = server._build_protocol()
754
client_sock.sendall(rest_of_request_bytes)
755
server._serve_one_request(server_protocol)
757
self.assertEqual(expected_response, client_sock.recv(50),
758
"Not a version 2 response to 'hello' request.")
759
self.assertEqual('', client_sock.recv(1))
761
def test_pipe_stream_incomplete_request(self):
762
"""The medium should still construct the right protocol version even if
763
the initial read only reads part of the request.
765
Specifically, it should correctly read the protocol version line even
766
if the partial read doesn't end in a newline. An older, naive
767
implementation of _get_line in the server used to have a bug in that
770
incomplete_request_bytes = protocol.REQUEST_VERSION_TWO + 'hel'
771
rest_of_request_bytes = 'lo\n'
772
expected_response = (
773
protocol.RESPONSE_VERSION_TWO + 'success\nok\x012\n')
774
# Make a pair of pipes, to and from the server
775
to_server, to_server_w = os.pipe()
776
from_server_r, from_server = os.pipe()
777
to_server = os.fdopen(to_server, 'r', 0)
778
to_server_w = os.fdopen(to_server_w, 'w', 0)
779
from_server_r = os.fdopen(from_server_r, 'r', 0)
780
from_server = os.fdopen(from_server, 'w', 0)
781
server = medium.SmartServerPipeStreamMedium(
782
to_server, from_server, None)
783
# Like test_socket_stream_incomplete_request, write an incomplete
784
# request (that does not end in '\n') and build a protocol from it.
785
to_server_w.write(incomplete_request_bytes)
786
server_protocol = server._build_protocol()
787
# Send the rest of the request, and finish serving it.
788
to_server_w.write(rest_of_request_bytes)
789
server._serve_one_request(server_protocol)
792
self.assertEqual(expected_response, from_server_r.read(),
793
"Not a version 2 response to 'hello' request.")
794
self.assertEqual('', from_server_r.read(1))
795
from_server_r.close()
735
798
def test_pipe_like_stream_with_two_requests(self):
736
799
# If two requests are read in one go, then two calls to
737
800
# _serve_one_request should still process both of them as if they had