44import os
55from binascii import hexlify
66
7+ import aiohttp .web
8+
79from lbry .schema import Claim
810from lbry .stream .background_downloader import BackgroundDownloader
911from lbry .stream .descriptor import StreamDescriptor
1012from lbry .testcase import CommandTestCase
1113from lbry .extras .daemon .components import TorrentSession , BACKGROUND_DOWNLOADER_COMPONENT
14+ from lbry .utils import aiohttp_request
1215from lbry .wallet import Transaction
1316from lbry .torrent .tracker import UDPTrackerServerProtocol
1417
@@ -51,6 +54,23 @@ async def initialize_torrent(self, tx_to_update=None):
5154 self .addCleanup (task .cancel )
5255 return tx , btih
5356
57+ async def assert_torrent_streaming_works (self , btih ):
58+ url = f'http://{ self .daemon .conf .streaming_host } :{ self .daemon .conf .streaming_port } /get/torrent'
59+ if self .daemon .streaming_runner .server is None :
60+ await self .daemon .streaming_runner .setup ()
61+ site = aiohttp .web .TCPSite (self .daemon .streaming_runner , self .daemon .conf .streaming_host ,
62+ self .daemon .conf .streaming_port )
63+ await site .start ()
64+ async with aiohttp_request ('get' , url ) as req :
65+ self .assertEqual (req .headers .get ('Content-Type' ), 'application/octet-stream' )
66+ content_range = req .headers .get ('Content-Range' )
67+ content_length = int (req .headers .get ('Content-Length' ))
68+ streamed_bytes = await req .content .read ()
69+ expected_size = self .seeder_session .get_size (btih )
70+ self .assertEqual (expected_size , len (streamed_bytes ))
71+ self .assertEqual (content_length , len (streamed_bytes ))
72+ self .assertEqual (f"bytes 0-{ expected_size - 1 } /{ expected_size } " , content_range )
73+
5474 @skipIf (TorrentSession is None , "libtorrent not installed" )
5575 async def test_download_torrent (self ):
5676 tx , btih = await self .initialize_torrent ()
@@ -61,6 +81,10 @@ async def test_download_torrent(self):
6181 self .assertItemCount (await self .daemon .jsonrpc_file_list (), 1 )
6282 self .assertEqual ((await self .daemon .jsonrpc_file_list ())['items' ][0 ].identifier , btih )
6383 self .assertIn (btih , self .client_session ._handles )
84+
85+ # stream over streaming API (full range of the largest file)
86+ await self .assert_torrent_streaming_works (btih )
87+
6488 tx , new_btih = await self .initialize_torrent (tx )
6589 self .assertNotEqual (btih , new_btih )
6690 # claim now points to another torrent, update to it
0 commit comments