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