@@ -5515,3 +5515,46 @@ async def handler(request: web.Request) -> web.Response:
5515
5515
5516
5516
finally :
5517
5517
await asyncio .to_thread (f .close )
5518
+
5519
+
5520
+ async def test_stream_reader_total_raw_bytes (aiohttp_client : AiohttpClient ) -> None :
5521
+ """Test whether StreamReader.total_raw_bytes returns the number of bytes downloaded"""
5522
+ source_data = b"@dKal^pH>1h|YW1:c2J$" * 4096
5523
+
5524
+ async def handler (request : web .Request ) -> web .Response :
5525
+ response = web .Response (body = source_data )
5526
+ response .enable_compression ()
5527
+ return response
5528
+
5529
+ app = web .Application ()
5530
+ app .router .add_get ("/" , handler )
5531
+
5532
+ client = await aiohttp_client (app )
5533
+
5534
+ # Check for decompressed data
5535
+ async with client .get (
5536
+ "/" , headers = {"Accept-Encoding" : "gzip" }, auto_decompress = True
5537
+ ) as resp :
5538
+ assert resp .headers ["Content-Encoding" ] == "gzip"
5539
+ assert int (resp .headers ["Content-Length" ]) < len (source_data )
5540
+ data = await resp .content .read ()
5541
+ assert len (data ) == len (source_data )
5542
+ assert resp .content .total_raw_bytes == int (resp .headers ["Content-Length" ])
5543
+
5544
+ # Check for compressed data
5545
+ async with client .get (
5546
+ "/" , headers = {"Accept-Encoding" : "gzip" }, auto_decompress = False
5547
+ ) as resp :
5548
+ assert resp .headers ["Content-Encoding" ] == "gzip"
5549
+ data = await resp .content .read ()
5550
+ assert resp .content .total_raw_bytes == len (data )
5551
+ assert resp .content .total_raw_bytes == int (resp .headers ["Content-Length" ])
5552
+
5553
+ # Check for non-compressed data
5554
+ async with client .get (
5555
+ "/" , headers = {"Accept-Encoding" : "identity" }, auto_decompress = True
5556
+ ) as resp :
5557
+ assert "Content-Encoding" not in resp .headers
5558
+ data = await resp .content .read ()
5559
+ assert resp .content .total_raw_bytes == len (data )
5560
+ assert resp .content .total_raw_bytes == int (resp .headers ["Content-Length" ])
0 commit comments