]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/requests/requests.ts
Try to fix weird CI test crashes
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / requests / requests.ts
index 70f7902222d2fb795fe1670e9c2a6068c61f8daa..5a2b09dccf95d14981bd17feffce745235cd9526 100644 (file)
@@ -121,6 +121,20 @@ function unwrapText (test: request.Test): Promise<string> {
   return test.then(res => res.text)
 }
 
+function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> {
+  return test.then(res => {
+    if (res.body instanceof Buffer) {
+      return JSON.parse(new TextDecoder().decode(res.body))
+    }
+
+    return res.body
+  })
+}
+
+function unwrapTextOrDecode (test: request.Test): Promise<string> {
+  return test.then(res => res.text || new TextDecoder().decode(res.body))
+}
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -134,6 +148,8 @@ export {
   makeRawRequest,
   makeActivityPubGetRequest,
   unwrapBody,
+  unwrapTextOrDecode,
+  unwrapBodyOrDecodeToJSON,
   unwrapText
 }
 
@@ -169,7 +185,7 @@ function buildFields (req: request.Test, fields: { [ fieldName: string ]: any },
     if (fields[key] === undefined) continue
 
     if (Array.isArray(fields[key]) && fields[key].length === 0) {
-      req.field(key, null)
+      req.field(key, [])
       continue
     }