@@ -1742,6 +1742,37 @@ def test_receive_unexpected_frame(self):
1742
1742
with pytest .raises (ValueError ):
1743
1743
s .receive_frame (f )
1744
1744
1745
+ def test_can_receive_trailers (self ):
1746
+ headers = [('a' , 'b' ), ('c' , 'd' ), (':status' , '200' )]
1747
+ trailers = [('e' , 'f' ), ('g' , 'h' )]
1748
+
1749
+ s = Stream (1 , None , None , None , None , FixedDecoder (headers ), None )
1750
+ s .state = STATE_HALF_CLOSED_LOCAL
1751
+
1752
+ # Provide the first HEADERS frame.
1753
+ f = HeadersFrame (1 )
1754
+ f .data = b'hi there!'
1755
+ f .flags .add ('END_HEADERS' )
1756
+ s .receive_frame (f )
1757
+
1758
+ assert s .response_headers == headers
1759
+
1760
+ # Now, replace the dummy decoder to ensure we get a new header block.
1761
+ s ._decoder = FixedDecoder (trailers )
1762
+
1763
+ # Provide the trailers.
1764
+ f = HeadersFrame (1 )
1765
+ f .data = b'hi there again!'
1766
+ f .flags .add ('END_STREAM' )
1767
+ f .flags .add ('END_HEADERS' )
1768
+ s .receive_frame (f )
1769
+
1770
+ # Now, check the trailers.
1771
+ assert s .response_trailers == trailers
1772
+
1773
+ # Confirm we closed the stream.
1774
+ assert s .state == STATE_CLOSED
1775
+
1745
1776
1746
1777
class TestResponse (object ):
1747
1778
def test_status_is_stripped_from_headers (self ):
@@ -1900,6 +1931,13 @@ class NullEncoder(object):
1900
1931
def encode (headers ):
1901
1932
return '\n ' .join ("%s%s" % (name , val ) for name , val in headers )
1902
1933
1934
+ class FixedDecoder (object ):
1935
+ def __init__ (self , result ):
1936
+ self .result = result
1937
+
1938
+ def decode (self , headers ):
1939
+ return self .result
1940
+
1903
1941
class DummySocket (object ):
1904
1942
def __init__ (self ):
1905
1943
self .queue = []
0 commit comments