]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/requests/requests.ts
Centralize test URLs
[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 { 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'
9
10 function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) {
11 const { host, protocol, pathname } = new URL(url)
12
13 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
14 }
15
16 function makeGetRequest (options: {
17 url: string
18 path?: string
19 query?: any
20 token?: string
21 statusCodeExpected?: HttpStatusCode
22 contentType?: string
23 range?: string
24 redirects?: number
25 accept?: string
26 host?: string
27 }) {
28 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
29 if (options.contentType === undefined) options.contentType = 'application/json'
30
31 const req = request(options.url).get(options.path)
32
33 if (options.contentType) req.set('Accept', options.contentType)
34 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
35 if (options.query) req.query(options.query)
36 if (options.range) req.set('Range', options.range)
37 if (options.accept) req.set('Accept', options.accept)
38 if (options.host) req.set('Host', options.host)
39 if (options.redirects) req.redirects(options.redirects)
40
41 return req.expect(options.statusCodeExpected)
42 }
43
44 function makeDeleteRequest (options: {
45 url: string
46 path: string
47 token?: string
48 statusCodeExpected?: HttpStatusCode
49 }) {
50 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
51
52 const req = request(options.url)
53 .delete(options.path)
54 .set('Accept', 'application/json')
55
56 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
57
58 return req.expect(options.statusCodeExpected)
59 }
60
61 function makeUploadRequest (options: {
62 url: string
63 method?: 'POST' | 'PUT'
64 path: string
65 token?: string
66
67 fields: { [ fieldName: string ]: any }
68 attaches?: { [ attachName: string ]: any | any[] }
69
70 headers?: { [ name: string ]: string }
71
72 statusCodeExpected?: HttpStatusCode
73 }) {
74 if (options.statusCodeExpected === undefined) {
75 options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
76 }
77
78 let req: request.Test
79 if (options.method === 'PUT') {
80 req = request(options.url).put(options.path)
81 } else {
82 req = request(options.url).post(options.path)
83 }
84
85 req.set('Accept', 'application/json')
86
87 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
88
89 Object.keys(options.headers || {}).forEach(name => {
90 req.set(name, options.headers[name])
91 })
92
93 Object.keys(options.fields).forEach(field => {
94 const value = options.fields[field]
95
96 if (value === undefined) return
97
98 if (Array.isArray(value)) {
99 for (let i = 0; i < value.length; i++) {
100 req.field(field + '[' + i + ']', value[i])
101 }
102 } else {
103 req.field(field, value)
104 }
105 })
106
107 Object.keys(options.attaches || {}).forEach(attach => {
108 const value = options.attaches[attach]
109 if (Array.isArray(value)) {
110 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
111 } else {
112 req.attach(attach, buildAbsoluteFixturePath(value))
113 }
114 })
115
116 if (options.statusCodeExpected) {
117 req.expect(options.statusCodeExpected)
118 }
119
120 return req
121 }
122
123 function makePostBodyRequest (options: {
124 url: string
125 path: string
126 token?: string
127 fields?: { [ fieldName: string ]: any }
128 headers?: { [ name: string ]: string }
129 type?: string
130 xForwardedFor?: string
131 statusCodeExpected?: HttpStatusCode
132 }) {
133 if (!options.fields) options.fields = {}
134 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
135
136 const req = request(options.url)
137 .post(options.path)
138 .set('Accept', 'application/json')
139
140 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
141 if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor)
142 if (options.type) req.type(options.type)
143
144 Object.keys(options.headers || {}).forEach(name => {
145 req.set(name, options.headers[name])
146 })
147
148 return req.send(options.fields)
149 .expect(options.statusCodeExpected)
150 }
151
152 function makePutBodyRequest (options: {
153 url: string
154 path: string
155 token?: string
156 fields: { [ fieldName: string ]: any }
157 statusCodeExpected?: HttpStatusCode
158 }) {
159 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
160
161 const req = request(options.url)
162 .put(options.path)
163 .set('Accept', 'application/json')
164
165 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
166
167 return req.send(options.fields)
168 .expect(options.statusCodeExpected)
169 }
170
171 function makeHTMLRequest (url: string, path: string) {
172 return request(url)
173 .get(path)
174 .set('Accept', 'text/html')
175 .expect(HttpStatusCode.OK_200)
176 }
177
178 function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
179 return makeGetRequest({
180 url,
181 path,
182 statusCodeExpected: expectedStatus,
183 accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
184 })
185 }
186
187 function updateImageRequest (options: {
188 url: string
189 path: string
190 accessToken: string
191 fixture: string
192 fieldname: string
193 }) {
194 let filePath = ''
195 if (isAbsolute(options.fixture)) {
196 filePath = options.fixture
197 } else {
198 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
199 }
200
201 return makeUploadRequest({
202 url: options.url,
203 path: options.path,
204 token: options.accessToken,
205 fields: {},
206 attaches: { [options.fieldname]: filePath },
207 statusCodeExpected: HttpStatusCode.OK_200
208 })
209 }
210
211 function decodeQueryString (path: string) {
212 return decode(path.split('?')[1])
213 }
214
215 function unwrapBody <T> (test: request.Test): Promise<T> {
216 return test.then(res => res.body)
217 }
218
219 function unwrapText (test: request.Test): Promise<string> {
220 return test.then(res => res.text)
221 }
222
223 // ---------------------------------------------------------------------------
224
225 export {
226 makeHTMLRequest,
227 makeGetRequest,
228 decodeQueryString,
229 makeUploadRequest,
230 makePostBodyRequest,
231 makePutBodyRequest,
232 makeDeleteRequest,
233 makeRawRequest,
234 makeActivityPubGetRequest,
235 unwrapBody,
236 unwrapText,
237 updateImageRequest
238 }