X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=shared%2Fserver-commands%2Frequests%2Frequests.ts;h=e3f1817f1b45c934599f9905b2a2e8fb7a028905;hb=fabe350bcac2d24c8524d4b6bb776dba7e374c8d;hp=85cbc9be939f807df0dc229253e08b4220fa7551;hpb=cbdbee807ddc202901fd61f1af4ca2988d2268b5;p=github%2FChocobozzz%2FPeerTube.git diff --git a/shared/server-commands/requests/requests.ts b/shared/server-commands/requests/requests.ts index 85cbc9be9..e3f1817f1 100644 --- a/shared/server-commands/requests/requests.ts +++ b/shared/server-commands/requests/requests.ts @@ -3,13 +3,14 @@ import { decode } from 'querystring' import request from 'supertest' import { URL } from 'url' -import { buildAbsoluteFixturePath } from '@shared/core-utils' +import { buildAbsoluteFixturePath, pick } from '@shared/core-utils' import { HttpStatusCode } from '@shared/models' export type CommonRequestParams = { url: string path?: string contentType?: string + responseType?: string range?: string redirects?: number accept?: string @@ -21,10 +22,29 @@ export type CommonRequestParams = { expectedStatus?: HttpStatusCode } -function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: string) { - const { host, protocol, pathname } = new URL(url) +function makeRawRequest (options: { + url: string + token?: string + expectedStatus?: HttpStatusCode + range?: string + query?: { [ id: string ]: string } + method?: 'GET' | 'POST' +}) { + const { host, protocol, pathname } = new URL(options.url) - return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range }) + const reqOptions = { + url: `${protocol}//${host}`, + path: pathname, + contentType: undefined, + + ...pick(options, [ 'expectedStatus', 'range', 'token', 'query' ]) + } + + if (options.method === 'POST') { + return makePostBodyRequest(reqOptions) + } + + return makeGetRequest(reqOptions) } function makeGetRequest (options: CommonRequestParams & { @@ -123,6 +143,8 @@ function decodeQueryString (path: string) { return decode(path.split('?')[1]) } +// --------------------------------------------------------------------------- + function unwrapBody (test: request.Test): Promise { return test.then(res => res.body) } @@ -134,7 +156,21 @@ function unwrapText (test: request.Test): Promise { function unwrapBodyOrDecodeToJSON (test: request.Test): Promise { return test.then(res => { if (res.body instanceof Buffer) { - return JSON.parse(new TextDecoder().decode(res.body)) + try { + return JSON.parse(new TextDecoder().decode(res.body)) + } catch (err) { + console.error('Cannot decode JSON.', { res, body: res.body instanceof Buffer ? res.body.toString() : res.body }) + throw err + } + } + + if (res.text) { + try { + return JSON.parse(res.text) + } catch (err) { + console.error('Cannot decode json', { res, text: res.text }) + throw err + } } return res.body @@ -167,6 +203,7 @@ export { function buildRequest (req: request.Test, options: CommonRequestParams) { if (options.contentType) req.set('Accept', options.contentType) + if (options.responseType) req.responseType(options.responseType) if (options.token) req.set('Authorization', 'Bearer ' + options.token) if (options.range) req.set('Range', options.range) if (options.accept) req.set('Accept', options.accept) @@ -179,13 +216,18 @@ function buildRequest (req: request.Test, options: CommonRequestParams) { req.set(name, options.headers[name]) }) - return req.expect((res) => { + return req.expect(res => { if (options.expectedStatus && res.status !== options.expectedStatus) { - throw new Error(`Expected status ${options.expectedStatus}, got ${res.status}. ` + - `\nThe server responded this error: "${res.body?.error ?? res.text}".\n` + + const err = new Error(`Expected status ${options.expectedStatus}, got ${res.status}. ` + + `\nThe server responded: "${res.body?.error ?? res.text}".\n` + 'You may take a closer look at the logs. To see how to do so, check out this page: ' + - 'https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/development/tests.md#debug-server-logs') + 'https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/development/tests.md#debug-server-logs'); + + (err as any).res = res + + throw err } + return res }) }