]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - shared/extra-utils/requests/requests.ts
More robust optimize transcoding job
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / requests / requests.ts
... / ...
CommitLineData
1/* eslint-disable @typescript-eslint/no-floating-promises */
2
3import { decode } from 'querystring'
4import * as request from 'supertest'
5import { URL } from 'url'
6import { HttpStatusCode } from '@shared/models'
7import { buildAbsoluteFixturePath } from '../miscs/tests'
8
9export type CommonRequestParams = {
10 url: string
11 path?: string
12 contentType?: string
13 range?: string
14 redirects?: number
15 accept?: string
16 host?: string
17 token?: string
18 headers?: { [ name: string ]: string }
19 type?: string
20 xForwardedFor?: string
21 expectedStatus?: HttpStatusCode
22}
23
24function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: string) {
25 const { host, protocol, pathname } = new URL(url)
26
27 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range })
28}
29
30function makeGetRequest (options: CommonRequestParams & {
31 query?: any
32}) {
33 const req = request(options.url).get(options.path)
34 .query(options.query)
35
36 return buildRequest(req, { contentType: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
37}
38
39function makeHTMLRequest (url: string, path: string) {
40 return makeGetRequest({
41 url,
42 path,
43 accept: 'text/html',
44 expectedStatus: HttpStatusCode.OK_200
45 })
46}
47
48function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
49 return makeGetRequest({
50 url,
51 path,
52 expectedStatus: expectedStatus,
53 accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
54 })
55}
56
57function makeDeleteRequest (options: CommonRequestParams) {
58 const req = request(options.url).delete(options.path)
59
60 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
61}
62
63function makeUploadRequest (options: CommonRequestParams & {
64 method?: 'POST' | 'PUT'
65
66 fields: { [ fieldName: string ]: any }
67 attaches?: { [ attachName: string ]: any | any[] }
68}) {
69 let req = options.method === 'PUT'
70 ? request(options.url).put(options.path)
71 : request(options.url).post(options.path)
72
73 req = buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
74
75 buildFields(req, options.fields)
76
77 Object.keys(options.attaches || {}).forEach(attach => {
78 const value = options.attaches[attach]
79
80 if (Array.isArray(value)) {
81 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
82 } else {
83 req.attach(attach, buildAbsoluteFixturePath(value))
84 }
85 })
86
87 return req
88}
89
90function makePostBodyRequest (options: CommonRequestParams & {
91 fields?: { [ fieldName: string ]: any }
92}) {
93 const req = request(options.url).post(options.path)
94 .send(options.fields)
95
96 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
97}
98
99function makePutBodyRequest (options: {
100 url: string
101 path: string
102 token?: string
103 fields: { [ fieldName: string ]: any }
104 expectedStatus?: HttpStatusCode
105}) {
106 const req = request(options.url).put(options.path)
107 .send(options.fields)
108
109 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
110}
111
112function decodeQueryString (path: string) {
113 return decode(path.split('?')[1])
114}
115
116function unwrapBody <T> (test: request.Test): Promise<T> {
117 return test.then(res => res.body)
118}
119
120function unwrapText (test: request.Test): Promise<string> {
121 return test.then(res => res.text)
122}
123
124function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> {
125 return test.then(res => {
126 if (res.body instanceof Buffer) {
127 return JSON.parse(new TextDecoder().decode(res.body))
128 }
129
130 return res.body
131 })
132}
133
134function unwrapTextOrDecode (test: request.Test): Promise<string> {
135 return test.then(res => res.text || new TextDecoder().decode(res.body))
136}
137
138// ---------------------------------------------------------------------------
139
140export {
141 makeHTMLRequest,
142 makeGetRequest,
143 decodeQueryString,
144 makeUploadRequest,
145 makePostBodyRequest,
146 makePutBodyRequest,
147 makeDeleteRequest,
148 makeRawRequest,
149 makeActivityPubGetRequest,
150 unwrapBody,
151 unwrapTextOrDecode,
152 unwrapBodyOrDecodeToJSON,
153 unwrapText
154}
155
156// ---------------------------------------------------------------------------
157
158function buildRequest (req: request.Test, options: CommonRequestParams) {
159 if (options.contentType) req.set('Accept', options.contentType)
160 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
161 if (options.range) req.set('Range', options.range)
162 if (options.accept) req.set('Accept', options.accept)
163 if (options.host) req.set('Host', options.host)
164 if (options.redirects) req.redirects(options.redirects)
165 if (options.expectedStatus) req.expect(options.expectedStatus)
166 if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor)
167 if (options.type) req.type(options.type)
168
169 Object.keys(options.headers || {}).forEach(name => {
170 req.set(name, options.headers[name])
171 })
172
173 return req
174}
175
176function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) {
177 if (!fields) return
178
179 let formKey: string
180
181 for (const key of Object.keys(fields)) {
182 if (namespace) formKey = `${namespace}[${key}]`
183 else formKey = key
184
185 if (fields[key] === undefined) continue
186
187 if (Array.isArray(fields[key]) && fields[key].length === 0) {
188 req.field(key, null)
189 continue
190 }
191
192 if (fields[key] !== null && typeof fields[key] === 'object') {
193 buildFields(req, fields[key], formKey)
194 } else {
195 req.field(formKey, fields[key])
196 }
197 }
198}