]> 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 61167f212db424cfab522c6f478d79fa5f25eb6d..3fbaa31d61863fd20db6fa04ba45e31a23dda6a8 100644 (file)
@@ -1,15 +1,17 @@
 /* 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 { 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'
 }
 
-function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
+function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) {
   const { host, protocol, pathname } = new URL(url)
 
   return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
@@ -20,11 +22,13 @@ function makeGetRequest (options: {
   path?: string
   query?: any
   token?: string
-  statusCodeExpected?: number
+  statusCodeExpected?: HttpStatusCode
   contentType?: string
   range?: string
+  redirects?: number
+  accept?: string
 }) {
-  if (!options.statusCodeExpected) options.statusCodeExpected = 400
+  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
   if (options.contentType === undefined) options.contentType = 'application/json'
 
   const req = request(options.url).get(options.path)
@@ -33,6 +37,8 @@ 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)
 }
@@ -41,9 +47,9 @@ function makeDeleteRequest (options: {
   url: string
   path: string
   token?: string
-  statusCodeExpected?: number
+  statusCodeExpected?: HttpStatusCode
 }) {
-  if (!options.statusCodeExpected) options.statusCodeExpected = 400
+  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
 
   const req = request(options.url)
     .delete(options.path)
@@ -60,10 +66,10 @@ function makeUploadRequest (options: {
   path: string
   token?: string
   fields: { [ fieldName: string ]: any }
-  attaches: { [ attachName: string ]: any | any[] }
-  statusCodeExpected?: number
+  attaches?: { [ attachName: string ]: any | any[] }
+  statusCodeExpected?: HttpStatusCode
 }) {
-  if (!options.statusCodeExpected) options.statusCodeExpected = 400
+  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
 
   let req: request.Test
   if (options.method === 'PUT') {
@@ -90,7 +96,7 @@ function makeUploadRequest (options: {
     }
   })
 
-  Object.keys(options.attaches).forEach(attach => {
+  Object.keys(options.attaches || {}).forEach(attach => {
     const value = options.attaches[attach]
     if (Array.isArray(value)) {
       req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
@@ -107,10 +113,10 @@ function makePostBodyRequest (options: {
   path: string
   token?: string
   fields?: { [ fieldName: string ]: any }
-  statusCodeExpected?: number
+  statusCodeExpected?: HttpStatusCode
 }) {
   if (!options.fields) options.fields = {}
-  if (!options.statusCodeExpected) options.statusCodeExpected = 400
+  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
 
   const req = request(options.url)
                 .post(options.path)
@@ -127,9 +133,9 @@ function makePutBodyRequest (options: {
   path: string
   token?: string
   fields: { [ fieldName: string ]: any }
-  statusCodeExpected?: number
+  statusCodeExpected?: HttpStatusCode
 }) {
-  if (!options.statusCodeExpected) options.statusCodeExpected = 400
+  if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
 
   const req = request(options.url)
                 .put(options.path)
@@ -145,14 +151,24 @@ function makeHTMLRequest (url: string, path: string) {
   return request(url)
     .get(path)
     .set('Accept', 'text/html')
-    .expect(200)
+    .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 updateAvatarRequest (options: {
+function updateImageRequest (options: {
   url: string
   path: string
   accessToken: string
   fixture: string
+  fieldname: string
 }) {
   let filePath = ''
   if (isAbsolute(options.fixture)) {
@@ -166,21 +182,37 @@ function updateAvatarRequest (options: {
     path: options.path,
     token: options.accessToken,
     fields: {},
-    attaches: { avatarfile: filePath },
-    statusCodeExpected: 200
+    attaches: { [options.fieldname]: filePath },
+    statusCodeExpected: HttpStatusCode.OK_200
   })
 }
 
+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 {
   get4KFileUrl,
   makeHTMLRequest,
   makeGetRequest,
+  decodeQueryString,
   makeUploadRequest,
   makePostBodyRequest,
   makePutBodyRequest,
   makeDeleteRequest,
   makeRawRequest,
-  updateAvatarRequest
+  makeActivityPubGetRequest,
+  unwrapBody,
+  unwrapText,
+  updateImageRequest
 }