1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
3 import { isAbsolute, join } from 'path'
4 import { decode } from 'querystring'
5 import * as request from 'supertest'
6 import { URL } from 'url'
7 import { HttpStatusCode } from '@shared/core-utils'
8 import { buildAbsoluteFixturePath, root } from '../miscs/tests'
10 function get4KFileUrl () {
11 return 'https://download.cpy.re/peertube/4k_file.txt'
14 function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) {
15 const { host, protocol, pathname } = new URL(url)
17 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
20 function makeGetRequest (options: {
25 statusCodeExpected?: HttpStatusCode
32 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
33 if (options.contentType === undefined) options.contentType = 'application/json'
35 const req = request(options.url).get(options.path)
37 if (options.contentType) req.set('Accept', options.contentType)
38 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
39 if (options.query) req.query(options.query)
40 if (options.range) req.set('Range', options.range)
41 if (options.accept) req.set('Accept', options.accept)
42 if (options.host) req.set('Host', options.host)
43 if (options.redirects) req.redirects(options.redirects)
45 return req.expect(options.statusCodeExpected)
48 function makeDeleteRequest (options: {
52 statusCodeExpected?: HttpStatusCode
54 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
56 const req = request(options.url)
58 .set('Accept', 'application/json')
60 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
62 return req.expect(options.statusCodeExpected)
65 function makeUploadRequest (options: {
67 method?: 'POST' | 'PUT'
70 fields: { [ fieldName: string ]: any }
71 attaches?: { [ attachName: string ]: any | any[] }
72 statusCodeExpected?: HttpStatusCode
74 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
77 if (options.method === 'PUT') {
78 req = request(options.url).put(options.path)
80 req = request(options.url).post(options.path)
83 req.set('Accept', 'application/json')
85 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
87 Object.keys(options.fields).forEach(field => {
88 const value = options.fields[field]
90 if (value === undefined) return
92 if (Array.isArray(value)) {
93 for (let i = 0; i < value.length; i++) {
94 req.field(field + '[' + i + ']', value[i])
97 req.field(field, value)
101 Object.keys(options.attaches || {}).forEach(attach => {
102 const value = options.attaches[attach]
103 if (Array.isArray(value)) {
104 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
106 req.attach(attach, buildAbsoluteFixturePath(value))
110 return req.expect(options.statusCodeExpected)
113 function makePostBodyRequest (options: {
117 fields?: { [ fieldName: string ]: any }
119 statusCodeExpected?: HttpStatusCode
121 if (!options.fields) options.fields = {}
122 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
124 const req = request(options.url)
126 .set('Accept', 'application/json')
128 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
129 if (options.type) req.type(options.type)
131 return req.send(options.fields)
132 .expect(options.statusCodeExpected)
135 function makePutBodyRequest (options: {
139 fields: { [ fieldName: string ]: any }
140 statusCodeExpected?: HttpStatusCode
142 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
144 const req = request(options.url)
146 .set('Accept', 'application/json')
148 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
150 return req.send(options.fields)
151 .expect(options.statusCodeExpected)
154 function makeHTMLRequest (url: string, path: string) {
157 .set('Accept', 'text/html')
158 .expect(HttpStatusCode.OK_200)
161 function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
162 return makeGetRequest({
165 statusCodeExpected: expectedStatus,
166 accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
170 function updateImageRequest (options: {
178 if (isAbsolute(options.fixture)) {
179 filePath = options.fixture
181 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
184 return makeUploadRequest({
187 token: options.accessToken,
189 attaches: { [options.fieldname]: filePath },
190 statusCodeExpected: HttpStatusCode.OK_200
194 function decodeQueryString (path: string) {
195 return decode(path.split('?')[1])
198 function unwrapBody <T> (test: request.Test): Promise<T> {
199 return test.then(res => res.body)
202 function unwrapText (test: request.Test): Promise<string> {
203 return test.then(res => res.text)
206 // ---------------------------------------------------------------------------
218 makeActivityPubGetRequest,