From cbaace39a98f83cede796756e375d85ab745fc33 Mon Sep 17 00:00:00 2001 From: Marcin Czenko Date: Sun, 23 Mar 2025 01:43:59 +0100 Subject: [PATCH] fixing problems after rebasing --- tests/integration/codexclient.nim | 22 +++++++++++++++++--- tests/integration/testbittorrent.nim | 30 ++++++++++++++++------------ 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/tests/integration/codexclient.nim b/tests/integration/codexclient.nim index 23a6d83d..c3cdf2e1 100644 --- a/tests/integration/codexclient.nim +++ b/tests/integration/codexclient.nim @@ -137,8 +137,13 @@ proc uploadTorrent*( ] let response = await client.post(client.baseurl & "/torrent", body = contents, headers = headers) - assert response.status == 200 - MultiHash.init((await response.body).hexToSeqByte).mapFailure + if not response.status == 200: + return failure($response.status) + let body = await response.body + let bytesRes = catch(body.hexToSeqByte) + without bytes =? bytesRes, err: + return failure(err) + MultiHash.init(bytes).mapFailure proc uploadTorrent*( client: CodexClient, bytes: seq[byte], filename = string.none @@ -180,6 +185,17 @@ proc downloadNoStream*( success await response.body +proc downloadTorrent*( + client: CodexClient, infoHash: MultiHash +): Future[?!string] {.async: (raises: [CancelledError, HttpError]).} = + let response = + await client.get(client.baseurl & "/torrent/" & infoHash.hex & "/network/stream") + + if response.status != 200: + return failure($response.status) + + success await response.body + proc downloadManifestOnly*( client: CodexClient, cid: Cid ): Future[?!string] {.async: (raises: [CancelledError, HttpError]).} = @@ -193,7 +209,7 @@ proc downloadManifestOnly*( proc downloadTorrentManifestOnly*( client: CodexClient, infoHash: MultiHash -): Future[?!RestTorrentContent] = +): Future[?!RestTorrentContent] {.async: (raises: [CancelledError, HttpError]).} = let response = await client.get(client.baseurl & "/torrent/" & infoHash.hex & "/network/manifest") diff --git a/tests/integration/testbittorrent.nim b/tests/integration/testbittorrent.nim index e04df3d2..23b5d085 100644 --- a/tests/integration/testbittorrent.nim +++ b/tests/integration/testbittorrent.nim @@ -57,8 +57,8 @@ proc createInfoDictionaryForContent( twonodessuite "BitTorrent API": test "uploading and downloading the content", twoNodesConfig: let exampleContent = exampleString(100) - let infoHash = client1.uploadTorrent(exampleContent).tryGet - let downloadedContent = client2.downloadTorrent(infoHash).tryGet + let infoHash = (await client1.uploadTorrent(exampleContent)).tryGet + let downloadedContent = (await client2.downloadTorrent(infoHash)).tryGet check downloadedContent == exampleContent test "uploading and downloading the content (exactly one piece long)", twoNodesConfig: @@ -67,8 +67,8 @@ twonodessuite "BitTorrent API": blocks = numOfBlocksPerPiece, blockSize = BitTorrentBlockSize.int ) - let infoHash = client1.uploadTorrent(bytes).tryGet - let downloadedContent = client2.downloadTorrent(infoHash).tryGet + let infoHash = (await client1.uploadTorrent(bytes)).tryGet + let downloadedContent = (await client2.downloadTorrent(infoHash)).tryGet check downloadedContent.toBytes == bytes test "uploading and downloading the content (exactly two pieces long)", twoNodesConfig: @@ -77,8 +77,8 @@ twonodessuite "BitTorrent API": blocks = numOfBlocksPerPiece * 2, blockSize = BitTorrentBlockSize.int ) - let infoHash = client1.uploadTorrent(bytes).tryGet - let downloadedContent = client2.downloadTorrent(infoHash).tryGet + let infoHash = (await client1.uploadTorrent(bytes)).tryGet + let downloadedContent = (await client2.downloadTorrent(infoHash)).tryGet check downloadedContent.toBytes == bytes # use with debugging to see the content @@ -90,24 +90,28 @@ twonodessuite "BitTorrent API": test "retrieving torrent manifest for given info hash", twoNodesConfig: let exampleFileName = "example.txt" let exampleContent = exampleString(100) - let infoHash = client1.uploadTorrent( - contents = exampleContent, - filename = some exampleFileName, - contentType = "text/plain", + let infoHash = ( + await client1.uploadTorrent( + contents = exampleContent, + filename = some exampleFileName, + contentType = "text/plain", + ) ).tryGet let expectedInfo = createInfoDictionaryForContent( content = exampleContent.toBytes, name = some exampleFileName ).tryGet - let restTorrentContent = client2.downloadTorrentManifestOnly(infoHash).tryGet + let restTorrentContent = + (await client2.downloadTorrentManifestOnly(infoHash)).tryGet let torrentManifest = restTorrentContent.torrentManifest let info = torrentManifest.info check info == expectedInfo - let response = - client2.downloadManifestOnly(cid = torrentManifest.codexManifestCid).tryGet + let response = ( + await client2.downloadManifestOnly(cid = torrentManifest.codexManifestCid) + ).tryGet let restContent = RestContent.fromJson(response).tryGet