1
0
Fork 0
peertube/shared/extra-utils/requests/requests.ts

195 lines
5 KiB
TypeScript
Raw Normal View History

2020-01-31 10:56:52 -05:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
2017-09-04 15:21:47 -04:00
import * as request from 'supertest'
import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
import { isAbsolute, join } from 'path'
2020-01-31 10:56:52 -05:00
import { URL } from 'url'
2020-04-29 03:04:42 -04:00
import { decode } from 'querystring'
2019-01-29 02:37:25 -05:00
2019-02-21 11:19:16 -05:00
function get4KFileUrl () {
return 'https://download.cpy.re/peertube/4k_file.txt'
}
function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
2020-01-31 10:56:52 -05:00
const { host, protocol, pathname } = new URL(url)
2019-01-29 02:37:25 -05:00
return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
2019-01-29 02:37:25 -05:00
}
2017-09-04 15:21:47 -04:00
2017-12-28 08:29:57 -05:00
function makeGetRequest (options: {
2020-01-31 10:56:52 -05:00
url: string
path?: string
query?: any
token?: string
statusCodeExpected?: number
contentType?: string
range?: string
2020-04-29 03:04:42 -04:00
redirects?: number
2017-12-28 08:29:57 -05:00
}) {
if (!options.statusCodeExpected) options.statusCodeExpected = 400
2018-09-25 10:22:48 -04:00
if (options.contentType === undefined) options.contentType = 'application/json'
2017-12-28 08:29:57 -05:00
2019-01-29 02:37:25 -05:00
const req = request(options.url).get(options.path)
2017-12-28 08:29:57 -05:00
2018-09-25 10:22:48 -04:00
if (options.contentType) req.set('Accept', options.contentType)
2017-12-28 08:29:57 -05: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)
2020-04-29 03:04:42 -04:00
if (options.redirects) req.redirects(options.redirects)
2017-12-28 08:29:57 -05:00
2018-09-25 10:22:48 -04:00
return req.expect(options.statusCodeExpected)
2017-12-28 08:29:57 -05:00
}
function makeDeleteRequest (options: {
2020-01-31 10:56:52 -05:00
url: string
path: string
token?: string
2017-12-28 08:29:57 -05:00
statusCodeExpected?: number
}) {
if (!options.statusCodeExpected) options.statusCodeExpected = 400
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 15:21:47 -04:00
}
function makeUploadRequest (options: {
2020-01-31 10:56:52 -05:00
url: string
method?: 'POST' | 'PUT'
path: string
token?: string
fields: { [ fieldName: string ]: any }
attaches: { [ attachName: string ]: any | any[] }
2017-09-04 15:21:47 -04:00
statusCodeExpected?: number
}) {
if (!options.statusCodeExpected) options.statusCodeExpected = 400
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 15:21:47 -04:00
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
Object.keys(options.fields).forEach(field => {
const value = options.fields[field]
2019-03-05 04:58:44 -05:00
if (value === undefined) return
2017-09-04 15:21:47 -04:00
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
req.field(field + '[' + i + ']', value[i])
}
} else {
req.field(field, value)
}
})
Object.keys(options.attaches).forEach(attach => {
const value = options.attaches[attach]
2018-08-06 05:45:24 -04:00
if (Array.isArray(value)) {
req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
} else {
req.attach(attach, buildAbsoluteFixturePath(value))
}
2017-09-04 15:21:47 -04:00
})
return req.expect(options.statusCodeExpected)
}
function makePostBodyRequest (options: {
2020-01-31 10:56:52 -05:00
url: string
path: string
token?: string
fields?: { [ fieldName: string ]: any }
2017-09-04 15:21:47 -04:00
statusCodeExpected?: number
}) {
2017-12-28 08:29:57 -05:00
if (!options.fields) options.fields = {}
2017-09-04 15:21:47 -04:00
if (!options.statusCodeExpected) options.statusCodeExpected = 400
const req = request(options.url)
.post(options.path)
.set('Accept', 'application/json')
if (options.token) req.set('Authorization', 'Bearer ' + options.token)
return req.send(options.fields)
.expect(options.statusCodeExpected)
}
function makePutBodyRequest (options: {
2020-01-31 10:56:52 -05:00
url: string
path: string
token?: string
fields: { [ fieldName: string ]: any }
2017-09-04 15:21:47 -04:00
statusCodeExpected?: number
}) {
if (!options.statusCodeExpected) options.statusCodeExpected = 400
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(200)
}
function updateAvatarRequest (options: {
2020-01-31 10:56:52 -05:00
url: string
path: string
accessToken: string
fixture: 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: {},
attaches: { avatarfile: filePath },
statusCodeExpected: 200
})
}
2020-04-29 03:04:42 -04:00
function decodeQueryString (path: string) {
return decode(path.split('?')[1])
}
2017-09-04 15:21:47 -04:00
// ---------------------------------------------------------------------------
export {
2019-02-21 11:19:16 -05:00
get4KFileUrl,
makeHTMLRequest,
2017-09-04 15:21:47 -04:00
makeGetRequest,
2020-04-29 03:04:42 -04:00
decodeQueryString,
makeUploadRequest,
2017-09-04 15:21:47 -04:00
makePostBodyRequest,
2017-12-28 08:29:57 -05:00
makePutBodyRequest,
makeDeleteRequest,
2019-01-29 02:37:25 -05:00
makeRawRequest,
updateAvatarRequest
2017-09-04 15:21:47 -04:00
}