]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/requests/requests.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / requests / requests.ts
1 /* eslint-disable @typescript-eslint/no-floating-promises */
2
3 import { decode } from 'querystring'
4 import * as request from 'supertest'
5 import { URL } from 'url'
6 import { HttpStatusCode } from '@shared/models'
7 import { buildAbsoluteFixturePath } from '../miscs/tests'
8
9 export 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
24 function 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
30 function 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
39 function makeHTMLRequest (url: string, path: string) {
40 return makeGetRequest({
41 url,
42 path,
43 accept: 'text/html',
44 expectedStatus: HttpStatusCode.OK_200
45 })
46 }
47
48 function 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
57 function 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
63 function 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
90 function 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
99 function 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
112 function decodeQueryString (path: string) {
113 return decode(path.split('?')[1])
114 }
115
116 function unwrapBody <T> (test: request.Test): Promise<T> {
117 return test.then(res => res.body)
118 }
119
120 function unwrapText (test: request.Test): Promise<string> {
121 return test.then(res => res.text)
122 }
123
124 // ---------------------------------------------------------------------------
125
126 export {
127 makeHTMLRequest,
128 makeGetRequest,
129 decodeQueryString,
130 makeUploadRequest,
131 makePostBodyRequest,
132 makePutBodyRequest,
133 makeDeleteRequest,
134 makeRawRequest,
135 makeActivityPubGetRequest,
136 unwrapBody,
137 unwrapText
138 }
139
140 // ---------------------------------------------------------------------------
141
142 function buildRequest (req: request.Test, options: CommonRequestParams) {
143 if (options.contentType) req.set('Accept', options.contentType)
144 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
145 if (options.range) req.set('Range', options.range)
146 if (options.accept) req.set('Accept', options.accept)
147 if (options.host) req.set('Host', options.host)
148 if (options.redirects) req.redirects(options.redirects)
149 if (options.expectedStatus) req.expect(options.expectedStatus)
150 if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor)
151 if (options.type) req.type(options.type)
152
153 Object.keys(options.headers || {}).forEach(name => {
154 req.set(name, options.headers[name])
155 })
156
157 return req
158 }
159
160 function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) {
161 if (!fields) return
162
163 let formKey: string
164
165 for (const key of Object.keys(fields)) {
166 if (namespace) formKey = `${namespace}[${key}]`
167 else formKey = key
168
169 if (fields[key] === undefined) continue
170
171 if (Array.isArray(fields[key]) && fields[key].length === 0) {
172 req.field(key, null)
173 continue
174 }
175
176 if (fields[key] !== null && typeof fields[key] === 'object') {
177 buildFields(req, fields[key], formKey)
178 } else {
179 req.field(formKey, fields[key])
180 }
181 }
182 }