1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import * as request from 'supertest'
4 import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
5 import { isAbsolute, join } from 'path'
6 import { URL } from 'url'
8 function get4KFileUrl () {
9 return 'https://download.cpy.re/peertube/4k_file.txt'
12 function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
13 const { host, protocol, pathname } = new URL(url)
15 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
18 function makeGetRequest (options: {
23 statusCodeExpected?: number
27 if (!options.statusCodeExpected) options.statusCodeExpected = 400
28 if (options.contentType === undefined) options.contentType = 'application/json'
30 const req = request(options.url).get(options.path)
32 if (options.contentType) req.set('Accept', options.contentType)
33 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
34 if (options.query) req.query(options.query)
35 if (options.range) req.set('Range', options.range)
37 return req.expect(options.statusCodeExpected)
40 function makeDeleteRequest (options: {
44 statusCodeExpected?: number
46 if (!options.statusCodeExpected) options.statusCodeExpected = 400
48 const req = request(options.url)
50 .set('Accept', 'application/json')
52 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
54 return req.expect(options.statusCodeExpected)
57 function makeUploadRequest (options: {
59 method?: 'POST' | 'PUT'
62 fields: { [ fieldName: string ]: any }
63 attaches: { [ attachName: string ]: any | any[] }
64 statusCodeExpected?: number
66 if (!options.statusCodeExpected) options.statusCodeExpected = 400
69 if (options.method === 'PUT') {
70 req = request(options.url).put(options.path)
72 req = request(options.url).post(options.path)
75 req.set('Accept', 'application/json')
77 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
79 Object.keys(options.fields).forEach(field => {
80 const value = options.fields[field]
82 if (value === undefined) return
84 if (Array.isArray(value)) {
85 for (let i = 0; i < value.length; i++) {
86 req.field(field + '[' + i + ']', value[i])
89 req.field(field, value)
93 Object.keys(options.attaches).forEach(attach => {
94 const value = options.attaches[attach]
95 if (Array.isArray(value)) {
96 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
98 req.attach(attach, buildAbsoluteFixturePath(value))
102 return req.expect(options.statusCodeExpected)
105 function makePostBodyRequest (options: {
109 fields?: { [ fieldName: string ]: any }
110 statusCodeExpected?: number
112 if (!options.fields) options.fields = {}
113 if (!options.statusCodeExpected) options.statusCodeExpected = 400
115 const req = request(options.url)
117 .set('Accept', 'application/json')
119 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
121 return req.send(options.fields)
122 .expect(options.statusCodeExpected)
125 function makePutBodyRequest (options: {
129 fields: { [ fieldName: string ]: any }
130 statusCodeExpected?: number
132 if (!options.statusCodeExpected) options.statusCodeExpected = 400
134 const req = request(options.url)
136 .set('Accept', 'application/json')
138 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
140 return req.send(options.fields)
141 .expect(options.statusCodeExpected)
144 function makeHTMLRequest (url: string, path: string) {
147 .set('Accept', 'text/html')
151 function updateAvatarRequest (options: {
158 if (isAbsolute(options.fixture)) {
159 filePath = options.fixture
161 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
164 return makeUploadRequest({
167 token: options.accessToken,
169 attaches: { avatarfile: filePath },
170 statusCodeExpected: 200
174 // ---------------------------------------------------------------------------