1 import * as request from 'supertest'
2 import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
3 import { isAbsolute, join } from 'path'
4 import { parse } from 'url'
6 function get4KFileUrl () {
7 return 'https://download.cpy.re/peertube/4k_file.txt'
10 function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
11 const { host, protocol, pathname } = parse(url)
13 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
16 function makeGetRequest (options: {
21 statusCodeExpected?: number,
25 if (!options.statusCodeExpected) options.statusCodeExpected = 400
26 if (options.contentType === undefined) options.contentType = 'application/json'
28 const req = request(options.url).get(options.path)
30 if (options.contentType) req.set('Accept', options.contentType)
31 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
32 if (options.query) req.query(options.query)
33 if (options.range) req.set('Range', options.range)
35 return req.expect(options.statusCodeExpected)
38 function makeDeleteRequest (options: {
42 statusCodeExpected?: number
44 if (!options.statusCodeExpected) options.statusCodeExpected = 400
46 const req = request(options.url)
48 .set('Accept', 'application/json')
50 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
52 return req.expect(options.statusCodeExpected)
55 function makeUploadRequest (options: {
57 method?: 'POST' | 'PUT',
60 fields: { [ fieldName: string ]: any },
61 attaches: { [ attachName: string ]: any | any[] },
62 statusCodeExpected?: number
64 if (!options.statusCodeExpected) options.statusCodeExpected = 400
67 if (options.method === 'PUT') {
68 req = request(options.url).put(options.path)
70 req = request(options.url).post(options.path)
73 req.set('Accept', 'application/json')
75 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
77 Object.keys(options.fields).forEach(field => {
78 const value = options.fields[field]
80 if (value === undefined) return
82 if (Array.isArray(value)) {
83 for (let i = 0; i < value.length; i++) {
84 req.field(field + '[' + i + ']', value[i])
87 req.field(field, value)
91 Object.keys(options.attaches).forEach(attach => {
92 const value = options.attaches[attach]
93 if (Array.isArray(value)) {
94 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
96 req.attach(attach, buildAbsoluteFixturePath(value))
100 return req.expect(options.statusCodeExpected)
103 function makePostBodyRequest (options: {
107 fields?: { [ fieldName: string ]: any },
108 statusCodeExpected?: number
110 if (!options.fields) options.fields = {}
111 if (!options.statusCodeExpected) options.statusCodeExpected = 400
113 const req = request(options.url)
115 .set('Accept', 'application/json')
117 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
119 return req.send(options.fields)
120 .expect(options.statusCodeExpected)
123 function makePutBodyRequest (options: {
127 fields: { [ fieldName: string ]: any },
128 statusCodeExpected?: number
130 if (!options.statusCodeExpected) options.statusCodeExpected = 400
132 const req = request(options.url)
134 .set('Accept', 'application/json')
136 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
138 return req.send(options.fields)
139 .expect(options.statusCodeExpected)
142 function makeHTMLRequest (url: string, path: string) {
145 .set('Accept', 'text/html')
149 function updateAvatarRequest (options: {
156 if (isAbsolute(options.fixture)) {
157 filePath = options.fixture
159 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
162 return makeUploadRequest({
165 token: options.accessToken,
167 attaches: { avatarfile: filePath },
168 statusCodeExpected: 200
172 // ---------------------------------------------------------------------------