X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=shared%2Fserver-commands%2Frequests%2Frequests.ts;h=cb0e1a5fbd920f3646f9f4c9d1a3b3022977e4b4;hb=4e4c23c5b8d55ab0aa48a7be8c53ec71d1d7e796;hp=b6b9024ed1460e14e93e1fff336df54d54063ade;hpb=bf54587a3e2ad9c2c186828f2a5682b91ee2cc00;p=github%2FChocobozzz%2FPeerTube.git diff --git a/shared/server-commands/requests/requests.ts b/shared/server-commands/requests/requests.ts index b6b9024ed..cb0e1a5fb 100644 --- a/shared/server-commands/requests/requests.ts +++ b/shared/server-commands/requests/requests.ts @@ -3,8 +3,8 @@ import { decode } from 'querystring' import request from 'supertest' import { URL } from 'url' +import { buildAbsoluteFixturePath, pick } from '@shared/core-utils' import { HttpStatusCode } from '@shared/models' -import { buildAbsoluteFixturePath } from '../miscs/tests' export type CommonRequestParams = { url: string @@ -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, expectedStatus, range }) + return makeGetRequest({ + url: `${protocol}//${host}`, + path: pathname, + contentType: undefined, + + ...pick(options, [ 'expectedStatus', 'range', 'token', 'query' ]) + }) } function makeGetRequest (options: CommonRequestParams & { @@ -52,7 +64,7 @@ function makeActivityPubGetRequest (url: string, path: string, expectedStatus = return makeGetRequest({ url, path, - expectedStatus: expectedStatus, + expectedStatus, accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8' }) } @@ -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 @@ -172,7 +189,6 @@ function buildRequest (req: request.Test, options: CommonRequestParams) { if (options.accept) req.set('Accept', options.accept) if (options.host) req.set('Host', options.host) if (options.redirects) req.redirects(options.redirects) - if (options.expectedStatus) req.expect(options.expectedStatus) if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor) if (options.type) req.type(options.type) @@ -180,7 +196,15 @@ function buildRequest (req: request.Test, options: CommonRequestParams) { req.set(name, options.headers[name]) }) - return req + return req.expect((res) => { + if (options.expectedStatus && res.status !== options.expectedStatus) { + throw 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') + } + return res + }) } function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) {