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'
7 import { decode } from 'querystring'
9 function get4KFileUrl () {
10 return 'https://download.cpy.re/peertube/4k_file.txt'
13 function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
14 const { host, protocol, pathname } = new URL(url)
16 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
19 function makeGetRequest (options: {
24 statusCodeExpected?: number
29 if (!options.statusCodeExpected) options.statusCodeExpected = 400
30 if (options.contentType === undefined) options.contentType = 'application/json'
32 const req = request(options.url).get(options.path)
34 if (options.contentType) req.set('Accept', options.contentType)
35 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
36 if (options.query) req.query(options.query)
37 if (options.range) req.set('Range', options.range)
38 if (options.redirects) req.redirects(options.redirects)
40 return req.expect(options.statusCodeExpected)
43 function makeDeleteRequest (options: {
47 statusCodeExpected?: number
49 if (!options.statusCodeExpected) options.statusCodeExpected = 400
51 const req = request(options.url)
53 .set('Accept', 'application/json')
55 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
57 return req.expect(options.statusCodeExpected)
60 function makeUploadRequest (options: {
62 method?: 'POST' | 'PUT'
65 fields: { [ fieldName: string ]: any }
66 attaches: { [ attachName: string ]: any | any[] }
67 statusCodeExpected?: number
69 if (!options.statusCodeExpected) options.statusCodeExpected = 400
72 if (options.method === 'PUT') {
73 req = request(options.url).put(options.path)
75 req = request(options.url).post(options.path)
78 req.set('Accept', 'application/json')
80 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
82 Object.keys(options.fields).forEach(field => {
83 const value = options.fields[field]
85 if (value === undefined) return
87 if (Array.isArray(value)) {
88 for (let i = 0; i < value.length; i++) {
89 req.field(field + '[' + i + ']', value[i])
92 req.field(field, value)
96 Object.keys(options.attaches).forEach(attach => {
97 const value = options.attaches[attach]
98 if (Array.isArray(value)) {
99 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
101 req.attach(attach, buildAbsoluteFixturePath(value))
105 return req.expect(options.statusCodeExpected)
108 function makePostBodyRequest (options: {
112 fields?: { [ fieldName: string ]: any }
113 statusCodeExpected?: number
115 if (!options.fields) options.fields = {}
116 if (!options.statusCodeExpected) options.statusCodeExpected = 400
118 const req = request(options.url)
120 .set('Accept', 'application/json')
122 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
124 return req.send(options.fields)
125 .expect(options.statusCodeExpected)
128 function makePutBodyRequest (options: {
132 fields: { [ fieldName: string ]: any }
133 statusCodeExpected?: number
135 if (!options.statusCodeExpected) options.statusCodeExpected = 400
137 const req = request(options.url)
139 .set('Accept', 'application/json')
141 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
143 return req.send(options.fields)
144 .expect(options.statusCodeExpected)
147 function makeHTMLRequest (url: string, path: string) {
150 .set('Accept', 'text/html')
154 function updateAvatarRequest (options: {
161 if (isAbsolute(options.fixture)) {
162 filePath = options.fixture
164 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
167 return makeUploadRequest({
170 token: options.accessToken,
172 attaches: { avatarfile: filePath },
173 statusCodeExpected: 200
177 function decodeQueryString (path: string) {
178 return decode(path.split('?')[1])
181 // ---------------------------------------------------------------------------