]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/requests/requests.ts
Move AP request in requests file
[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
4bbfc6c6 3import { isAbsolute, join } from 'path'
9107d791 4import { decode } from 'querystring'
2d1ad5b9
C
5import * as request from 'supertest'
6import { URL } from 'url'
2d53be02 7import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
2d1ad5b9 8import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
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
2d1ad5b9
C
157function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
158 return makeGetRequest({
159 url,
160 path,
161 statusCodeExpected: expectedStatus,
162 accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
163 })
164}
165
213e30ef 166function updateImageRequest (options: {
a1587156
C
167 url: string
168 path: string
169 accessToken: string
4bbfc6c6 170 fixture: string
213e30ef 171 fieldname: string
4bbfc6c6
C
172}) {
173 let filePath = ''
174 if (isAbsolute(options.fixture)) {
175 filePath = options.fixture
176 } else {
2a8c5d0a 177 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
4bbfc6c6
C
178 }
179
180 return makeUploadRequest({
181 url: options.url,
182 path: options.path,
183 token: options.accessToken,
184 fields: {},
213e30ef 185 attaches: { [options.fieldname]: filePath },
2d53be02 186 statusCodeExpected: HttpStatusCode.OK_200
4bbfc6c6
C
187 })
188}
189
9107d791
C
190function decodeQueryString (path: string) {
191 return decode(path.split('?')[1])
192}
193
c1bc8ee4 194function unwrapBody <T> (test: request.Test): Promise<T> {
e8bd7ce7
C
195 return test.then(res => res.body)
196}
197
c1bc8ee4
C
198function unwrapText (test: request.Test): Promise<string> {
199 return test.then(res => res.text)
200}
201
0e1dc3e7
C
202// ---------------------------------------------------------------------------
203
204export {
bfe2ef6b 205 get4KFileUrl,
e032aec9 206 makeHTMLRequest,
0e1dc3e7 207 makeGetRequest,
9107d791 208 decodeQueryString,
ac81d1a0 209 makeUploadRequest,
0e1dc3e7 210 makePostBodyRequest,
eec63bbc 211 makePutBodyRequest,
4bbfc6c6 212 makeDeleteRequest,
09209296 213 makeRawRequest,
2d1ad5b9 214 makeActivityPubGetRequest,
c1bc8ee4
C
215 unwrapBody,
216 unwrapText,
213e30ef 217 updateImageRequest
0e1dc3e7 218}