X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=shared%2Fserver-commands%2Frequests%2Frequests.ts;h=dc9cf4e015a2bcea704ceec00896fcd98bfecda6;hb=e57a840ede1f0eadec209e58401008f232249300;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..dc9cf4e01 100644 --- a/shared/server-commands/requests/requests.ts +++ b/shared/server-commands/requests/requests.ts @@ -3,7 +3,7 @@ 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 = { @@ -21,10 +21,22 @@ 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 } +}) { + const { host, protocol, pathname } = new URL(options.url) + + return makeGetRequest({ + url: `${protocol}//${host}`, + path: pathname, + contentType: undefined, - return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range }) + ...pick(options, [ 'expectedStatus', 'range', 'token', 'query' ]) + }) } function makeGetRequest (options: CommonRequestParams & { @@ -134,7 +146,12 @@ 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) + throw err + } } return res.body