@@ -5586,3 +5586,46 @@ async def handler(request: web.Request) -> web.Response:
5586
5586
5587
5587
finally :
5588
5588
await asyncio .to_thread (f .close )
5589
+
5590
+
5591
+ async def test_stream_reader_total_raw_bytes (aiohttp_client : AiohttpClient ) -> None :
5592
+ """Test whether StreamReader.total_raw_bytes returns the number of bytes downloaded"""
5593
+ source_data = b"@dKal^pH>1h|YW1:c2J$" * 4096
5594
+
5595
+ async def handler (request : web .Request ) -> web .Response :
5596
+ response = web .Response (body = source_data )
5597
+ response .enable_compression ()
5598
+ return response
5599
+
5600
+ app = web .Application ()
5601
+ app .router .add_get ("/" , handler )
5602
+
5603
+ client = await aiohttp_client (app )
5604
+
5605
+ # Check for decompressed data
5606
+ async with client .get (
5607
+ "/" , headers = {"Accept-Encoding" : "gzip" }, auto_decompress = True
5608
+ ) as resp :
5609
+ assert resp .headers ["Content-Encoding" ] == "gzip"
5610
+ assert int (resp .headers ["Content-Length" ]) < len (source_data )
5611
+ data = await resp .content .read ()
5612
+ assert len (data ) == len (source_data )
5613
+ assert resp .content .total_raw_bytes == int (resp .headers ["Content-Length" ])
5614
+
5615
+ # Check for compressed data
5616
+ async with client .get (
5617
+ "/" , headers = {"Accept-Encoding" : "gzip" }, auto_decompress = False
5618
+ ) as resp :
5619
+ assert resp .headers ["Content-Encoding" ] == "gzip"
5620
+ data = await resp .content .read ()
5621
+ assert resp .content .total_raw_bytes == len (data )
5622
+ assert resp .content .total_raw_bytes == int (resp .headers ["Content-Length" ])
5623
+
5624
+ # Check for non-compressed data
5625
+ async with client .get (
5626
+ "/" , headers = {"Accept-Encoding" : "identity" }, auto_decompress = True
5627
+ ) as resp :
5628
+ assert "Content-Encoding" not in resp .headers
5629
+ data = await resp .content .read ()
5630
+ assert resp .content .total_raw_bytes == len (data )
5631
+ assert resp .content .total_raw_bytes == int (resp .headers ["Content-Length" ])
0 commit comments