Update integration test to check bytes downloaded

This required being able to download and inspect bytes from the response body, which seemed not possible using std/httpclient. However, using chronos' HttpClient, we are able to get bytes and compare against uploaded data successfully.
This commit is contained in:
Eric 2024-06-14 13:07:12 +10:00
parent 33eb176430
commit f4ba5844d6
No known key found for this signature in database
2 changed files with 28 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import std/strutils
from pkg/libp2p import Cid, `$`, init
import pkg/stint
import pkg/questionable/results
import pkg/chronos/apps/http/[httpserver, shttpserver, httpclient]
import pkg/codex/logutils
import pkg/codex/rest/json
import pkg/codex/purchasing
@ -15,9 +16,16 @@ export purchasing
type CodexClient* = ref object
http: HttpClient
baseurl: string
session: HttpSessionRef
type CodexClientError* = object of CatchableError
proc new*(_: type CodexClient, baseurl: string): CodexClient =
CodexClient(http: newHttpClient(), baseurl: baseurl)
CodexClient(
http: newHttpClient(),
baseurl: baseurl,
session: HttpSessionRef.new({HttpClientFlag.Http11Pipeline})
)
proc info*(client: CodexClient): ?!JsonNode =
let url = client.baseurl & "/debug/info"
@ -45,6 +53,23 @@ proc download*(client: CodexClient, cid: Cid, local = false): ?!string =
success response.body
proc downloadBytes*(
client: CodexClient,
cid: Cid,
local = false): Future[?!seq[byte]] {.async.} =
let uri = parseUri(
client.baseurl & "/data/" & $cid &
(if local: "" else: "/network")
)
let (status, bytes) = await client.session.fetch(uri)
if status != 200:
return failure("fetch failed with status " & $status)
success bytes
proc list*(client: CodexClient): ?!RestContentList =
let url = client.baseurl & "/data"
let response = client.http.get(url)

View File

@ -58,8 +58,8 @@ marketplacesuite "EC bug":
let request = await marketplace.getRequest(requestId.get)
let cidFromRequest = Cid.init(request.content.cid).get()
let downloaded = clientApi.download(cidFromRequest, local = true)
let downloaded = await clientApi.downloadBytes(cidFromRequest, local = true)
check downloaded.isOk
check downloaded.get == data
check downloaded.get.toHex == data
await subscription.unsubscribe()