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