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
path?: string
contentType?: string
+ responseType?: string
range?: string
redirects?: number
accept?: string
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)
+
+ const reqOptions = {
+ url: `${protocol}//${host}`,
+ path: pathname,
+ contentType: undefined,
+
+ ...pick(options, [ 'expectedStatus', 'range', 'token', 'query' ])
+ }
+
+ if (options.method === 'POST') {
+ return makePostBodyRequest(reqOptions)
+ }
- return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range })
+ return makeGetRequest(reqOptions)
}
function makeGetRequest (options: CommonRequestParams & {
return makeGetRequest({
url,
path,
- expectedStatus: expectedStatus,
+ expectedStatus,
accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
})
}
return decode(path.split('?')[1])
}
+// ---------------------------------------------------------------------------
+
function unwrapBody <T> (test: request.Test): Promise<T> {
return test.then(res => res.body)
}
function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> {
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
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)
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)
req.set(name, options.headers[name])
})
- return req
+ return req.expect(res => {
+ if (options.expectedStatus && res.status !== options.expectedStatus) {
+ 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');
+
+ (err as any).res = res
+
+ throw err
+ }
+
+ return res
+ })
}
function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) {