|
| 1 | +import hpack |
| 2 | +import hyperframe.frame |
| 3 | +import pytest |
| 4 | + |
| 5 | +import httpcore |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.anyio |
| 9 | +async def test_http2_connection_with_trailing_headers(): |
| 10 | + """ |
| 11 | + Test that trailing headers are correctly received and processed. |
| 12 | + """ |
| 13 | + origin = httpcore.Origin(b"https", b"example.com", 443) |
| 14 | + stream = httpcore.AsyncMockStream( |
| 15 | + [ |
| 16 | + hyperframe.frame.SettingsFrame().serialize(), |
| 17 | + hyperframe.frame.HeadersFrame( |
| 18 | + stream_id=1, |
| 19 | + data=hpack.Encoder().encode( |
| 20 | + [ |
| 21 | + (b":status", b"200"), |
| 22 | + (b"content-type", b"plain/text"), |
| 23 | + ] |
| 24 | + ), |
| 25 | + flags=["END_HEADERS"], |
| 26 | + ).serialize(), |
| 27 | + hyperframe.frame.DataFrame(stream_id=1, data=b"Hello, world!").serialize(), |
| 28 | + # Send trailing headers |
| 29 | + hyperframe.frame.HeadersFrame( |
| 30 | + stream_id=1, |
| 31 | + data=hpack.Encoder().encode( |
| 32 | + [ |
| 33 | + (b"x-trailer-1", b"trailer-value-1"), |
| 34 | + (b"x-trailer-2", b"trailer-value-2"), |
| 35 | + ] |
| 36 | + ), |
| 37 | + flags=["END_HEADERS", "END_STREAM"], |
| 38 | + ).serialize(), |
| 39 | + ] |
| 40 | + ) |
| 41 | + async with httpcore.AsyncHTTP2Connection( |
| 42 | + origin=origin, stream=stream, keepalive_expiry=5.0 |
| 43 | + ) as conn: |
| 44 | + response = await conn.request("GET", "https://example.com/") |
| 45 | + assert response.status == 200 |
| 46 | + assert response.content == b"Hello, world!" |
| 47 | + |
| 48 | + # Check that trailing headers are included in extensions |
| 49 | + assert "trailing_headers" in response.extensions |
| 50 | + assert response.extensions["trailing_headers"] == [ |
| 51 | + (b"x-trailer-1", b"trailer-value-1"), |
| 52 | + (b"x-trailer-2", b"trailer-value-2"), |
| 53 | + ] |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.anyio |
| 57 | +async def test_http2_connection_with_body_and_trailing_headers(): |
| 58 | + """ |
| 59 | + Test that trailing headers are correctly received and processed |
| 60 | + when reading the response body in chunks. |
| 61 | + """ |
| 62 | + origin = httpcore.Origin(b"https", b"example.com", 443) |
| 63 | + stream = httpcore.AsyncMockStream( |
| 64 | + [ |
| 65 | + hyperframe.frame.SettingsFrame().serialize(), |
| 66 | + hyperframe.frame.HeadersFrame( |
| 67 | + stream_id=1, |
| 68 | + data=hpack.Encoder().encode( |
| 69 | + [ |
| 70 | + (b":status", b"200"), |
| 71 | + (b"content-type", b"plain/text"), |
| 72 | + ] |
| 73 | + ), |
| 74 | + flags=["END_HEADERS"], |
| 75 | + ).serialize(), |
| 76 | + hyperframe.frame.DataFrame(stream_id=1, data=b"Hello, ").serialize(), |
| 77 | + hyperframe.frame.DataFrame(stream_id=1, data=b"world!").serialize(), |
| 78 | + # Send trailing headers |
| 79 | + hyperframe.frame.HeadersFrame( |
| 80 | + stream_id=1, |
| 81 | + data=hpack.Encoder().encode( |
| 82 | + [ |
| 83 | + (b"x-trailer-1", b"trailer-value-1"), |
| 84 | + (b"x-trailer-2", b"trailer-value-2"), |
| 85 | + ] |
| 86 | + ), |
| 87 | + flags=["END_HEADERS", "END_STREAM"], |
| 88 | + ).serialize(), |
| 89 | + ] |
| 90 | + ) |
| 91 | + |
| 92 | + async with httpcore.AsyncHTTP2Connection( |
| 93 | + origin=origin, stream=stream, keepalive_expiry=5.0 |
| 94 | + ) as conn: |
| 95 | + async with conn.stream("GET", "https://example.com/") as response: |
| 96 | + content = b"" |
| 97 | + async for chunk in response.aiter_stream(): |
| 98 | + content += chunk |
| 99 | + |
| 100 | + assert response.status == 200 |
| 101 | + assert content == b"Hello, world!" |
| 102 | + |
| 103 | + # Check that trailing headers are included in extensions |
| 104 | + assert "trailing_headers" in response.extensions |
| 105 | + assert response.extensions["trailing_headers"] == [ |
| 106 | + (b"x-trailer-1", b"trailer-value-1"), |
| 107 | + (b"x-trailer-2", b"trailer-value-2"), |
| 108 | + ] |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.anyio |
| 112 | +async def test_http2_connection_with_trailing_headers_pseudo_removed(): |
| 113 | + """ |
| 114 | + Test that pseudo-headers in trailing headers are correctly filtered out. |
| 115 | + """ |
| 116 | + origin = httpcore.Origin(b"https", b"example.com", 443) |
| 117 | + stream = httpcore.AsyncMockStream( |
| 118 | + [ |
| 119 | + hyperframe.frame.SettingsFrame().serialize(), |
| 120 | + hyperframe.frame.HeadersFrame( |
| 121 | + stream_id=1, |
| 122 | + data=hpack.Encoder().encode( |
| 123 | + [ |
| 124 | + (b":status", b"200"), |
| 125 | + (b"content-type", b"plain/text"), |
| 126 | + ] |
| 127 | + ), |
| 128 | + flags=["END_HEADERS"], |
| 129 | + ).serialize(), |
| 130 | + hyperframe.frame.DataFrame(stream_id=1, data=b"Hello, world!").serialize(), |
| 131 | + # Send trailing headers with a pseudo-header which should be filtered out |
| 132 | + hyperframe.frame.HeadersFrame( |
| 133 | + stream_id=1, |
| 134 | + data=hpack.Encoder().encode( |
| 135 | + [ |
| 136 | + (b":pseudo", b"should-be-filtered"), |
| 137 | + (b"x-trailer", b"trailer-value"), |
| 138 | + ] |
| 139 | + ), |
| 140 | + flags=["END_HEADERS", "END_STREAM"], |
| 141 | + ).serialize(), |
| 142 | + ] |
| 143 | + ) |
| 144 | + async with httpcore.AsyncHTTP2Connection( |
| 145 | + origin=origin, stream=stream, keepalive_expiry=5.0 |
| 146 | + ) as conn: |
| 147 | + response = await conn.request("GET", "https://example.com/") |
| 148 | + assert response.status == 200 |
| 149 | + assert response.content == b"Hello, world!" |
| 150 | + |
| 151 | + # Check that trailing headers are included in extensions but pseudo-headers are filtered |
| 152 | + assert "trailing_headers" in response.extensions |
| 153 | + assert len(response.extensions["trailing_headers"]) == 1 |
| 154 | + assert response.extensions["trailing_headers"] == [ |
| 155 | + (b"x-trailer", b"trailer-value"), |
| 156 | + ] |
0 commit comments