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