]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tests/utils/requests/requests.ts
Improve e2e tests
[github/Chocobozzz/PeerTube.git] / server / tests / utils / requests / requests.ts
index 52b7a4c29ca73e9d671010798666e5185abb5788..a9b1dff9a28e43a78f1290d4ccda236803028972 100644 (file)
@@ -1,16 +1,50 @@
 import * as request from 'supertest'
+import { buildAbsoluteFixturePath } from '../'
 
-function makeGetRequest (url: string, path: string) {
-  return request(url)
-    .get(path)
+function makeGetRequest (options: {
+  url: string,
+  path: string,
+  query?: any,
+  token?: string,
+  statusCodeExpected?: number
+}) {
+  if (!options.statusCodeExpected) options.statusCodeExpected = 400
+
+  const req = request(options.url)
+    .get(options.path)
     .set('Accept', 'application/json')
-    .expect(200)
+
+  if (options.token) req.set('Authorization', 'Bearer ' + options.token)
+  if (options.query) req.query(options.query)
+
+  return req
     .expect('Content-Type', /json/)
+    .expect(options.statusCodeExpected)
 }
 
-function makePostUploadRequest (options: {
+function makeDeleteRequest (options: {
   url: string,
   path: string,
+  token?: string,
+  statusCodeExpected?: number
+}) {
+  if (!options.statusCodeExpected) options.statusCodeExpected = 400
+
+  const req = request(options.url)
+    .delete(options.path)
+    .set('Accept', 'application/json')
+
+  if (options.token) req.set('Authorization', 'Bearer ' + options.token)
+
+  return req
+    .expect('Content-Type', /json/)
+    .expect(options.statusCodeExpected)
+}
+
+function makeUploadRequest (options: {
+  url: string,
+  method?: 'POST' | 'PUT',
+  path: string,
   token: string,
   fields: { [ fieldName: string ]: any },
   attaches: { [ attachName: string ]: any },
@@ -18,9 +52,14 @@ function makePostUploadRequest (options: {
 }) {
   if (!options.statusCodeExpected) options.statusCodeExpected = 400
 
-  const req = request(options.url)
-                .post(options.path)
-                .set('Accept', 'application/json')
+  let req: request.Test
+  if (options.method === 'PUT') {
+    req = request(options.url).put(options.path)
+  } else {
+    req = request(options.url).post(options.path)
+  }
+
+  req.set('Accept', 'application/json')
 
   if (options.token) req.set('Authorization', 'Bearer ' + options.token)
 
@@ -38,7 +77,7 @@ function makePostUploadRequest (options: {
 
   Object.keys(options.attaches).forEach(attach => {
     const value = options.attaches[attach]
-    req.attach(attach, value)
+    req.attach(attach, buildAbsoluteFixturePath(value))
   })
 
   return req.expect(options.statusCodeExpected)
@@ -48,9 +87,10 @@ function makePostBodyRequest (options: {
   url: string,
   path: string,
   token?: string,
-  fields: { [ fieldName: string ]: any },
+  fields?: { [ fieldName: string ]: any },
   statusCodeExpected?: number
 }) {
+  if (!options.fields) options.fields = {}
   if (!options.statusCodeExpected) options.statusCodeExpected = 400
 
   const req = request(options.url)
@@ -66,7 +106,7 @@ function makePostBodyRequest (options: {
 function makePutBodyRequest (options: {
   url: string,
   path: string,
-  token: string,
+  token?: string,
   fields: { [ fieldName: string ]: any },
   statusCodeExpected?: number
 }) {
@@ -86,7 +126,8 @@ function makePutBodyRequest (options: {
 
 export {
   makeGetRequest,
-  makePostUploadRequest,
+  makeUploadRequest,
   makePostBodyRequest,
-  makePutBodyRequest
+  makePutBodyRequest,
+  makeDeleteRequest
 }