PeerTube/shared/extra-utils/requests/requests.ts

223 lines
6.0 KiB
TypeScript
Raw Normal View History

2020-01-31 16:56:52 +01:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
import { isAbsolute, join } from 'path'
2020-04-29 09:04:42 +02:00
import { decode } from 'querystring'
2021-07-06 15:33:39 +02:00
import * as request from 'supertest'
import { URL } from 'url'
2021-07-13 09:43:59 +02:00
import { HttpStatusCode } from '@shared/core-utils'
import { buildAbsoluteFixturePath, root } from '../miscs/tests'
2019-01-29 08:37:25 +01:00
2019-02-21 17:19:16 +01:00
function get4KFileUrl () {
return 'https://download.cpy.re/peertube/4k_file.txt'
}
function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) {
2020-01-31 16:56:52 +01:00
const { host, protocol, pathname } = new URL(url)
2019-01-29 08:37:25 +01:00
return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
2019-01-29 08:37:25 +01:00
}
2017-09-04 21:21:47 +02:00
2017-12-28 14:29:57 +01:00
function makeGetRequest (options: {
2020-01-31 16:56:52 +01:00
url: string
path?: string
query?: any
token?: string
statusCodeExpected?: HttpStatusCode
2020-01-31 16:56:52 +01:00
contentType?: string
range?: string
2020-04-29 09:04:42 +02:00
redirects?: number
accept?: string
2021-07-13 11:05:15 +02:00
host?: string
2017-12-28 14:29:57 +01:00
}) {
if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
2018-09-25 16:22:48 +02:00
if (options.contentType === undefined) options.contentType = 'application/json'
2017-12-28 14:29:57 +01:00
2019-01-29 08:37:25 +01:00
const req = request(options.url).get(options.path)
2017-12-28 14:29:57 +01:00
2018-09-25 16:22:48 +02:00
if (options.contentType) req.set('Accept', options.contentType)
2017-12-28 14:29:57 +01:00
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
if (options.query) req.query(options.query)
if (options.range) req.set('Range', options.range)
if (options.accept) req.set('Accept', options.accept)
2021-07-13 11:05:15 +02:00
if (options.host) req.set('Host', options.host)
2020-04-29 09:04:42 +02:00
if (options.redirects) req.redirects(options.redirects)
2017-12-28 14:29:57 +01:00
2018-09-25 16:22:48 +02:00
return req.expect(options.statusCodeExpected)
2017-12-28 14:29:57 +01:00
}
function makeDeleteRequest (options: {
2020-01-31 16:56:52 +01:00
url: string
path: string
token?: string
statusCodeExpected?: HttpStatusCode
2017-12-28 14:29:57 +01:00
}) {
if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
2017-12-28 14:29:57 +01:00
const req = request(options.url)
.delete(options.path)
.set('Accept', 'application/json')
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
return req.expect(options.statusCodeExpected)
2017-09-04 21:21:47 +02:00
}
function makeUploadRequest (options: {
2020-01-31 16:56:52 +01:00
url: string
method?: 'POST' | 'PUT'
path: string
token?: string
fields: { [ fieldName: string ]: any }
2020-10-30 15:09:00 +01:00
attaches?: { [ attachName: string ]: any | any[] }
statusCodeExpected?: HttpStatusCode
2017-09-04 21:21:47 +02:00
}) {
if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
2017-09-04 21:21:47 +02:00
let req: request.Test
if (options.method === 'PUT') {
req = request(options.url).put(options.path)
} else {
req = request(options.url).post(options.path)
}
req.set('Accept', 'application/json')
2017-09-04 21:21:47 +02:00
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
Object.keys(options.fields).forEach(field => {
const value = options.fields[field]
2019-03-05 10:58:44 +01:00
if (value === undefined) return
2017-09-04 21:21:47 +02:00
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
req.field(field + '[' + i + ']', value[i])
}
} else {
req.field(field, value)
}
})
2020-10-30 15:09:00 +01:00
Object.keys(options.attaches || {}).forEach(attach => {
2017-09-04 21:21:47 +02:00
const value = options.attaches[attach]
2018-08-06 11:45:24 +02:00
if (Array.isArray(value)) {
req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
} else {
req.attach(attach, buildAbsoluteFixturePath(value))
}
2017-09-04 21:21:47 +02:00
})
return req.expect(options.statusCodeExpected)
}
function makePostBodyRequest (options: {
2020-01-31 16:56:52 +01:00
url: string
path: string
token?: string
fields?: { [ fieldName: string ]: any }
2021-07-13 11:05:15 +02:00
type?: string
statusCodeExpected?: HttpStatusCode
2017-09-04 21:21:47 +02:00
}) {
2017-12-28 14:29:57 +01:00
if (!options.fields) options.fields = {}
if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
2017-09-04 21:21:47 +02:00
const req = request(options.url)
.post(options.path)
.set('Accept', 'application/json')
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
2021-07-13 11:05:15 +02:00
if (options.type) req.type(options.type)
2017-09-04 21:21:47 +02:00
return req.send(options.fields)
.expect(options.statusCodeExpected)
}
function makePutBodyRequest (options: {
2020-01-31 16:56:52 +01:00
url: string
path: string
token?: string
fields: { [ fieldName: string ]: any }
statusCodeExpected?: HttpStatusCode
2017-09-04 21:21:47 +02:00
}) {
if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
2017-09-04 21:21:47 +02:00
const req = request(options.url)
.put(options.path)
.set('Accept', 'application/json')
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
return req.send(options.fields)
.expect(options.statusCodeExpected)
}
function makeHTMLRequest (url: string, path: string) {
return request(url)
.get(path)
.set('Accept', 'text/html')
.expect(HttpStatusCode.OK_200)
}
2021-07-06 15:33:39 +02:00
function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
return makeGetRequest({
url,
path,
statusCodeExpected: expectedStatus,
accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
})
}
2021-04-07 10:36:13 +02:00
function updateImageRequest (options: {
2020-01-31 16:56:52 +01:00
url: string
path: string
accessToken: string
fixture: string
2021-04-07 10:36:13 +02:00
fieldname: string
}) {
let filePath = ''
if (isAbsolute(options.fixture)) {
filePath = options.fixture
} else {
filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
}
return makeUploadRequest({
url: options.url,
path: options.path,
token: options.accessToken,
fields: {},
2021-04-07 10:36:13 +02:00
attaches: { [options.fieldname]: filePath },
statusCodeExpected: HttpStatusCode.OK_200
})
}
2020-04-29 09:04:42 +02:00
function decodeQueryString (path: string) {
return decode(path.split('?')[1])
}
2021-07-06 10:21:35 +02:00
function unwrapBody <T> (test: request.Test): Promise<T> {
2021-07-06 09:55:05 +02:00
return test.then(res => res.body)
}
2021-07-06 10:21:35 +02:00
function unwrapText (test: request.Test): Promise<string> {
return test.then(res => res.text)
}
2017-09-04 21:21:47 +02:00
// ---------------------------------------------------------------------------
export {
2019-02-21 17:19:16 +01:00
get4KFileUrl,
makeHTMLRequest,
2017-09-04 21:21:47 +02:00
makeGetRequest,
2020-04-29 09:04:42 +02:00
decodeQueryString,
makeUploadRequest,
2017-09-04 21:21:47 +02:00
makePostBodyRequest,
2017-12-28 14:29:57 +01:00
makePutBodyRequest,
makeDeleteRequest,
2019-01-29 08:37:25 +01:00
makeRawRequest,
2021-07-06 15:33:39 +02:00
makeActivityPubGetRequest,
2021-07-06 10:21:35 +02:00
unwrapBody,
unwrapText,
2021-04-07 10:36:13 +02:00
updateImageRequest
2017-09-04 21:21:47 +02:00
}