@@ -859,6 +859,82 @@ def socket_handler(listener):
859
859
860
860
self .tear_down ()
861
861
862
+ def test_upgrade (self ):
863
+ self .set_up (secure = False )
864
+
865
+ recv_event = threading .Event ()
866
+ wait_event = threading .Event ()
867
+
868
+ def socket_handler (listener ):
869
+ sock = listener .accept ()[0 ]
870
+
871
+ # First read the HTTP/1.1 request
872
+ data = b''
873
+ while not data .endswith (b'\r \n \r \n ' ):
874
+ data += sock .recv (65535 )
875
+
876
+ # Check it's an upgrade.
877
+ assert b'upgrade: h2c\r \n ' in data
878
+
879
+ # Send back an upgrade message.
880
+ data = (
881
+ b'HTTP/1.1 101 Switching Protocols\r \n '
882
+ b'Server: some-server\r \n '
883
+ b'Connection: upgrade\r \n '
884
+ b'Upgrade: h2c\r \n '
885
+ b'\r \n '
886
+ )
887
+ sock .sendall (data )
888
+
889
+ # We get a message for connection open, specifically the preamble.
890
+ receive_preamble (sock )
891
+
892
+ # Now, send the headers for the response. This response has a body.
893
+ f = build_headers_frame ([(':status' , '200' )])
894
+ f .stream_id = 1
895
+ sock .sendall (f .serialize ())
896
+
897
+ # Send the first two chunks.
898
+ f = DataFrame (1 )
899
+ f .data = b'hello'
900
+ sock .sendall (f .serialize ())
901
+ f = DataFrame (1 )
902
+ f .data = b'there'
903
+ sock .sendall (f .serialize ())
904
+
905
+ # Now, delay a bit. We want to wait a half a second before we send
906
+ # the next frame.
907
+ wait_event .wait (5 )
908
+ time .sleep (0.5 )
909
+ f = DataFrame (1 )
910
+ f .data = b'world'
911
+ f .flags .add ('END_STREAM' )
912
+ sock .sendall (f .serialize ())
913
+
914
+ # Wait for the message from the main thread.
915
+ recv_event .set ()
916
+ sock .close ()
917
+
918
+ self ._start_server (socket_handler )
919
+ conn = hyper .HTTPConnection (self .host , self .port , self .secure )
920
+ conn .request ('GET' , '/' )
921
+ resp = conn .get_response ()
922
+
923
+ # Confirm the status code.
924
+ assert resp .status == 200
925
+
926
+ first_chunk = resp .read (10 )
927
+ wait_event .set ()
928
+ second_chunk = resp .read (5 )
929
+
930
+ assert first_chunk == b'hellothere'
931
+ assert second_chunk == b'world'
932
+
933
+ # Awesome, we're done now.
934
+ recv_event .wait (5 )
935
+
936
+ self .tear_down ()
937
+
862
938
863
939
class TestRequestsAdapter (SocketLevelTest ):
864
940
# This uses HTTP/2.
0 commit comments