aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/requests/requests.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-04-21 15:00:01 +0200
committerChocobozzz <chocobozzz@cpy.re>2023-05-09 08:57:34 +0200
commitd102de1b38f2877463529c3b27bd35ffef4fd8bf (patch)
tree31fa0bdf26ad7a2ee46d600d804a6f03260266c8 /shared/server-commands/requests/requests.ts
parent2fe978744e5b74eb824e4d79c1bb9b840169f125 (diff)
downloadPeerTube-d102de1b38f2877463529c3b27bd35ffef4fd8bf.tar.gz
PeerTube-d102de1b38f2877463529c3b27bd35ffef4fd8bf.tar.zst
PeerTube-d102de1b38f2877463529c3b27bd35ffef4fd8bf.zip
Add runner server tests
Diffstat (limited to 'shared/server-commands/requests/requests.ts')
-rw-r--r--shared/server-commands/requests/requests.ts37
1 files changed, 31 insertions, 6 deletions
diff --git a/shared/server-commands/requests/requests.ts b/shared/server-commands/requests/requests.ts
index cb0e1a5fb..96f67b4c7 100644
--- a/shared/server-commands/requests/requests.ts
+++ b/shared/server-commands/requests/requests.ts
@@ -10,6 +10,7 @@ export type CommonRequestParams = {
10 url: string 10 url: string
11 path?: string 11 path?: string
12 contentType?: string 12 contentType?: string
13 responseType?: string
13 range?: string 14 range?: string
14 redirects?: number 15 redirects?: number
15 accept?: string 16 accept?: string
@@ -27,16 +28,23 @@ function makeRawRequest (options: {
27 expectedStatus?: HttpStatusCode 28 expectedStatus?: HttpStatusCode
28 range?: string 29 range?: string
29 query?: { [ id: string ]: string } 30 query?: { [ id: string ]: string }
31 method?: 'GET' | 'POST'
30}) { 32}) {
31 const { host, protocol, pathname } = new URL(options.url) 33 const { host, protocol, pathname } = new URL(options.url)
32 34
33 return makeGetRequest({ 35 const reqOptions = {
34 url: `${protocol}//${host}`, 36 url: `${protocol}//${host}`,
35 path: pathname, 37 path: pathname,
36 contentType: undefined, 38 contentType: undefined,
37 39
38 ...pick(options, [ 'expectedStatus', 'range', 'token', 'query' ]) 40 ...pick(options, [ 'expectedStatus', 'range', 'token', 'query' ])
39 }) 41 }
42
43 if (options.method === 'POST') {
44 return makePostBodyRequest(reqOptions)
45 }
46
47 return makeGetRequest(reqOptions)
40} 48}
41 49
42function makeGetRequest (options: CommonRequestParams & { 50function makeGetRequest (options: CommonRequestParams & {
@@ -135,6 +143,8 @@ function decodeQueryString (path: string) {
135 return decode(path.split('?')[1]) 143 return decode(path.split('?')[1])
136} 144}
137 145
146// ---------------------------------------------------------------------------
147
138function unwrapBody <T> (test: request.Test): Promise<T> { 148function unwrapBody <T> (test: request.Test): Promise<T> {
139 return test.then(res => res.body) 149 return test.then(res => res.body)
140} 150}
@@ -149,7 +159,16 @@ function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> {
149 try { 159 try {
150 return JSON.parse(new TextDecoder().decode(res.body)) 160 return JSON.parse(new TextDecoder().decode(res.body))
151 } catch (err) { 161 } catch (err) {
152 console.error('Cannot decode JSON.', res.body) 162 console.error('Cannot decode JSON.', res.body instanceof Buffer ? res.body.toString() : res.body)
163 throw err
164 }
165 }
166
167 if (res.text) {
168 try {
169 return JSON.parse(res.text)
170 } catch (err) {
171 console.error('Cannot decode json', res.text)
153 throw err 172 throw err
154 } 173 }
155 } 174 }
@@ -184,6 +203,7 @@ export {
184 203
185function buildRequest (req: request.Test, options: CommonRequestParams) { 204function buildRequest (req: request.Test, options: CommonRequestParams) {
186 if (options.contentType) req.set('Accept', options.contentType) 205 if (options.contentType) req.set('Accept', options.contentType)
206 if (options.responseType) req.responseType(options.responseType)
187 if (options.token) req.set('Authorization', 'Bearer ' + options.token) 207 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
188 if (options.range) req.set('Range', options.range) 208 if (options.range) req.set('Range', options.range)
189 if (options.accept) req.set('Accept', options.accept) 209 if (options.accept) req.set('Accept', options.accept)
@@ -196,13 +216,18 @@ function buildRequest (req: request.Test, options: CommonRequestParams) {
196 req.set(name, options.headers[name]) 216 req.set(name, options.headers[name])
197 }) 217 })
198 218
199 return req.expect((res) => { 219 return req.expect(res => {
200 if (options.expectedStatus && res.status !== options.expectedStatus) { 220 if (options.expectedStatus && res.status !== options.expectedStatus) {
201 throw new Error(`Expected status ${options.expectedStatus}, got ${res.status}. ` + 221 const err = new Error(`Expected status ${options.expectedStatus}, got ${res.status}. ` +
202 `\nThe server responded: "${res.body?.error ?? res.text}".\n` + 222 `\nThe server responded: "${res.body?.error ?? res.text}".\n` +
203 'You may take a closer look at the logs. To see how to do so, check out this page: ' + 223 'You may take a closer look at the logs. To see how to do so, check out this page: ' +
204 'https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/development/tests.md#debug-server-logs') 224 'https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/development/tests.md#debug-server-logs');
225
226 (err as any).res = res
227
228 throw err
205 } 229 }
230
206 return res 231 return res
207 }) 232 })
208} 233}