]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/requests/requests.ts
Add check params live tests
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / requests / requests.ts
CommitLineData
a1587156
C
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
2
0e1dc3e7 3import * as request from 'supertest'
2a8c5d0a 4import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
4bbfc6c6 5import { isAbsolute, join } from 'path'
a1587156 6import { URL } from 'url'
9107d791 7import { decode } from 'querystring'
09209296 8
bfe2ef6b
C
9function get4KFileUrl () {
10 return 'https://download.cpy.re/peertube/4k_file.txt'
11}
12
4c280004 13function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
a1587156 14 const { host, protocol, pathname } = new URL(url)
09209296 15
4c280004 16 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
09209296 17}
0e1dc3e7 18
eec63bbc 19function makeGetRequest (options: {
a1587156
C
20 url: string
21 path?: string
22 query?: any
23 token?: string
24 statusCodeExpected?: number
25 contentType?: string
4c280004 26 range?: string
9107d791 27 redirects?: number
eec63bbc
C
28}) {
29 if (!options.statusCodeExpected) options.statusCodeExpected = 400
ebdb6124 30 if (options.contentType === undefined) options.contentType = 'application/json'
eec63bbc 31
09209296 32 const req = request(options.url).get(options.path)
eec63bbc 33
ebdb6124 34 if (options.contentType) req.set('Accept', options.contentType)
eec63bbc
C
35 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
36 if (options.query) req.query(options.query)
4c280004 37 if (options.range) req.set('Range', options.range)
9107d791 38 if (options.redirects) req.redirects(options.redirects)
eec63bbc 39
ebdb6124 40 return req.expect(options.statusCodeExpected)
eec63bbc
C
41}
42
43function makeDeleteRequest (options: {
a1587156
C
44 url: string
45 path: string
46 token?: string
eec63bbc
C
47 statusCodeExpected?: number
48}) {
49 if (!options.statusCodeExpected) options.statusCodeExpected = 400
50
51 const req = request(options.url)
52 .delete(options.path)
53 .set('Accept', 'application/json')
54
55 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
56
7ad9b984 57 return req.expect(options.statusCodeExpected)
0e1dc3e7
C
58}
59
ac81d1a0 60function makeUploadRequest (options: {
a1587156
C
61 url: string
62 method?: 'POST' | 'PUT'
63 path: string
64 token?: string
65 fields: { [ fieldName: string ]: any }
77e9f859 66 attaches?: { [ attachName: string ]: any | any[] }
0e1dc3e7
C
67 statusCodeExpected?: number
68}) {
69 if (!options.statusCodeExpected) options.statusCodeExpected = 400
70
ac81d1a0
C
71 let req: request.Test
72 if (options.method === 'PUT') {
73 req = request(options.url).put(options.path)
74 } else {
75 req = request(options.url).post(options.path)
76 }
77
78 req.set('Accept', 'application/json')
0e1dc3e7
C
79
80 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
81
82 Object.keys(options.fields).forEach(field => {
83 const value = options.fields[field]
84
df0b219d
C
85 if (value === undefined) return
86
0e1dc3e7
C
87 if (Array.isArray(value)) {
88 for (let i = 0; i < value.length; i++) {
89 req.field(field + '[' + i + ']', value[i])
90 }
91 } else {
92 req.field(field, value)
93 }
94 })
95
77e9f859 96 Object.keys(options.attaches || {}).forEach(attach => {
0e1dc3e7 97 const value = options.attaches[attach]
2769e297
C
98 if (Array.isArray(value)) {
99 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
100 } else {
101 req.attach(attach, buildAbsoluteFixturePath(value))
102 }
0e1dc3e7
C
103 })
104
105 return req.expect(options.statusCodeExpected)
106}
107
108function makePostBodyRequest (options: {
a1587156
C
109 url: string
110 path: string
111 token?: string
112 fields?: { [ fieldName: string ]: any }
0e1dc3e7
C
113 statusCodeExpected?: number
114}) {
eec63bbc 115 if (!options.fields) options.fields = {}
0e1dc3e7
C
116 if (!options.statusCodeExpected) options.statusCodeExpected = 400
117
118 const req = request(options.url)
119 .post(options.path)
120 .set('Accept', 'application/json')
121
122 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
123
124 return req.send(options.fields)
125 .expect(options.statusCodeExpected)
126}
127
128function makePutBodyRequest (options: {
a1587156
C
129 url: string
130 path: string
131 token?: string
132 fields: { [ fieldName: string ]: any }
0e1dc3e7
C
133 statusCodeExpected?: number
134}) {
135 if (!options.statusCodeExpected) options.statusCodeExpected = 400
136
137 const req = request(options.url)
138 .put(options.path)
139 .set('Accept', 'application/json')
140
141 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
142
143 return req.send(options.fields)
144 .expect(options.statusCodeExpected)
145}
146
e032aec9
C
147function makeHTMLRequest (url: string, path: string) {
148 return request(url)
149 .get(path)
150 .set('Accept', 'text/html')
151 .expect(200)
152}
153
4bbfc6c6 154function updateAvatarRequest (options: {
a1587156
C
155 url: string
156 path: string
157 accessToken: string
4bbfc6c6
C
158 fixture: string
159}) {
160 let filePath = ''
161 if (isAbsolute(options.fixture)) {
162 filePath = options.fixture
163 } else {
2a8c5d0a 164 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
4bbfc6c6
C
165 }
166
167 return makeUploadRequest({
168 url: options.url,
169 path: options.path,
170 token: options.accessToken,
171 fields: {},
172 attaches: { avatarfile: filePath },
173 statusCodeExpected: 200
174 })
175}
176
9107d791
C
177function decodeQueryString (path: string) {
178 return decode(path.split('?')[1])
179}
180
0e1dc3e7
C
181// ---------------------------------------------------------------------------
182
183export {
bfe2ef6b 184 get4KFileUrl,
e032aec9 185 makeHTMLRequest,
0e1dc3e7 186 makeGetRequest,
9107d791 187 decodeQueryString,
ac81d1a0 188 makeUploadRequest,
0e1dc3e7 189 makePostBodyRequest,
eec63bbc 190 makePutBodyRequest,
4bbfc6c6 191 makeDeleteRequest,
09209296 192 makeRawRequest,
4bbfc6c6 193 updateAvatarRequest
0e1dc3e7 194}