@@ -1742,6 +1742,37 @@ def test_receive_unexpected_frame(self):
17421742 with pytest .raises (ValueError ):
17431743 s .receive_frame (f )
17441744
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+
17451776
17461777class TestResponse (object ):
17471778 def test_status_is_stripped_from_headers (self ):
@@ -1900,6 +1931,13 @@ class NullEncoder(object):
19001931 def encode (headers ):
19011932 return '\n ' .join ("%s%s" % (name , val ) for name , val in headers )
19021933
1934+ class FixedDecoder (object ):
1935+ def __init__ (self , result ):
1936+ self .result = result
1937+
1938+ def decode (self , headers ):
1939+ return self .result
1940+
19031941class DummySocket (object ):
19041942 def __init__ (self ):
19051943 self .queue = []
0 commit comments