]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/extra-utils/requests/requests.ts
Move AP request in requests file
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / requests / requests.ts
index 8b5cddf4a339010699dfec911cf26d5402f840fb..3fbaa31d61863fd20db6fa04ba45e31a23dda6a8 100644 (file)
@@ -1,11 +1,11 @@
 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
 
-import * as request from 'supertest'
-import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
 import { isAbsolute, join } from 'path'
-import { URL } from 'url'
 import { decode } from 'querystring'
+import * as request from 'supertest'
+import { URL } from 'url'
 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
+import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
 
 function get4KFileUrl () {
   return 'https://download.cpy.re/peertube/4k_file.txt'
@@ -26,6 +26,7 @@ function makeGetRequest (options: {
   contentType?: string
   range?: string
   redirects?: number
+  accept?: string
 }) {
   if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
   if (options.contentType === undefined) options.contentType = 'application/json'
@@ -36,6 +37,7 @@ function makeGetRequest (options: {
   if (options.token) req.set('Authorization', 'Bearer ' + options.token)
   if (options.query) req.query(options.query)
   if (options.range) req.set('Range', options.range)
+  if (options.accept) req.set('Accept', options.accept)
   if (options.redirects) req.redirects(options.redirects)
 
   return req.expect(options.statusCodeExpected)
@@ -152,6 +154,15 @@ function makeHTMLRequest (url: string, path: string) {
     .expect(HttpStatusCode.OK_200)
 }
 
+function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
+  return makeGetRequest({
+    url,
+    path,
+    statusCodeExpected: expectedStatus,
+    accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
+  })
+}
+
 function updateImageRequest (options: {
   url: string
   path: string
@@ -180,6 +191,14 @@ function decodeQueryString (path: string) {
   return decode(path.split('?')[1])
 }
 
+function unwrapBody <T> (test: request.Test): Promise<T> {
+  return test.then(res => res.body)
+}
+
+function unwrapText (test: request.Test): Promise<string> {
+  return test.then(res => res.text)
+}
+
 // ---------------------------------------------------------------------------
 
 export {
@@ -192,5 +211,8 @@ export {
   makePutBodyRequest,
   makeDeleteRequest,
   makeRawRequest,
+  makeActivityPubGetRequest,
+  unwrapBody,
+  unwrapText,
   updateImageRequest
 }