]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/requests/requests.ts
Add overview of a user's actions in user-edit (#2558)
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / requests / requests.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
2
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
8 function get4KFileUrl () {
9 return 'https://download.cpy.re/peertube/4k_file.txt'
10 }
11
12 function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
13 const { host, protocol, pathname } = new URL(url)
14
15 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
16 }
17
18 function makeGetRequest (options: {
19 url: string
20 path?: string
21 query?: any
22 token?: string
23 statusCodeExpected?: number
24 contentType?: string
25 range?: string
26 }) {
27 if (!options.statusCodeExpected) options.statusCodeExpected = 400
28 if (options.contentType === undefined) options.contentType = 'application/json'
29
30 const req = request(options.url).get(options.path)
31
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)
36
37 return req.expect(options.statusCodeExpected)
38 }
39
40 function makeDeleteRequest (options: {
41 url: string
42 path: string
43 token?: string
44 statusCodeExpected?: number
45 }) {
46 if (!options.statusCodeExpected) options.statusCodeExpected = 400
47
48 const req = request(options.url)
49 .delete(options.path)
50 .set('Accept', 'application/json')
51
52 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
53
54 return req.expect(options.statusCodeExpected)
55 }
56
57 function makeUploadRequest (options: {
58 url: string
59 method?: 'POST' | 'PUT'
60 path: string
61 token?: string
62 fields: { [ fieldName: string ]: any }
63 attaches: { [ attachName: string ]: any | any[] }
64 statusCodeExpected?: number
65 }) {
66 if (!options.statusCodeExpected) options.statusCodeExpected = 400
67
68 let req: request.Test
69 if (options.method === 'PUT') {
70 req = request(options.url).put(options.path)
71 } else {
72 req = request(options.url).post(options.path)
73 }
74
75 req.set('Accept', 'application/json')
76
77 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
78
79 Object.keys(options.fields).forEach(field => {
80 const value = options.fields[field]
81
82 if (value === undefined) return
83
84 if (Array.isArray(value)) {
85 for (let i = 0; i < value.length; i++) {
86 req.field(field + '[' + i + ']', value[i])
87 }
88 } else {
89 req.field(field, value)
90 }
91 })
92
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])
97 } else {
98 req.attach(attach, buildAbsoluteFixturePath(value))
99 }
100 })
101
102 return req.expect(options.statusCodeExpected)
103 }
104
105 function makePostBodyRequest (options: {
106 url: string
107 path: string
108 token?: string
109 fields?: { [ fieldName: string ]: any }
110 statusCodeExpected?: number
111 }) {
112 if (!options.fields) options.fields = {}
113 if (!options.statusCodeExpected) options.statusCodeExpected = 400
114
115 const req = request(options.url)
116 .post(options.path)
117 .set('Accept', 'application/json')
118
119 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
120
121 return req.send(options.fields)
122 .expect(options.statusCodeExpected)
123 }
124
125 function makePutBodyRequest (options: {
126 url: string
127 path: string
128 token?: string
129 fields: { [ fieldName: string ]: any }
130 statusCodeExpected?: number
131 }) {
132 if (!options.statusCodeExpected) options.statusCodeExpected = 400
133
134 const req = request(options.url)
135 .put(options.path)
136 .set('Accept', 'application/json')
137
138 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
139
140 return req.send(options.fields)
141 .expect(options.statusCodeExpected)
142 }
143
144 function makeHTMLRequest (url: string, path: string) {
145 return request(url)
146 .get(path)
147 .set('Accept', 'text/html')
148 .expect(200)
149 }
150
151 function updateAvatarRequest (options: {
152 url: string
153 path: string
154 accessToken: string
155 fixture: string
156 }) {
157 let filePath = ''
158 if (isAbsolute(options.fixture)) {
159 filePath = options.fixture
160 } else {
161 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
162 }
163
164 return makeUploadRequest({
165 url: options.url,
166 path: options.path,
167 token: options.accessToken,
168 fields: {},
169 attaches: { avatarfile: filePath },
170 statusCodeExpected: 200
171 })
172 }
173
174 // ---------------------------------------------------------------------------
175
176 export {
177 get4KFileUrl,
178 makeHTMLRequest,
179 makeGetRequest,
180 makeUploadRequest,
181 makePostBodyRequest,
182 makePutBodyRequest,
183 makeDeleteRequest,
184 makeRawRequest,
185 updateAvatarRequest
186 }