aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/requests/requests.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-16 10:42:24 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:18 +0200
commitc0e8b12e7fd554ba4d2ceb0c4900804c6a4c63ea (patch)
treebaf29753ac5d4598643e3bee719f8df0cc36c59d /shared/extra-utils/requests/requests.ts
parent08642a765ea514a00f159db898edf14c376fbe6c (diff)
downloadPeerTube-c0e8b12e7fd554ba4d2ceb0c4900804c6a4c63ea.tar.gz
PeerTube-c0e8b12e7fd554ba4d2ceb0c4900804c6a4c63ea.tar.zst
PeerTube-c0e8b12e7fd554ba4d2ceb0c4900804c6a4c63ea.zip
Refactor requests
Diffstat (limited to 'shared/extra-utils/requests/requests.ts')
-rw-r--r--shared/extra-utils/requests/requests.ts256
1 files changed, 100 insertions, 156 deletions
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts
index 60c9b938b..70f790222 100644
--- a/shared/extra-utils/requests/requests.ts
+++ b/shared/extra-utils/requests/requests.ts
@@ -1,111 +1,82 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ 1/* eslint-disable @typescript-eslint/no-floating-promises */
2 2
3import { isAbsolute, join } from 'path'
4import { decode } from 'querystring' 3import { decode } from 'querystring'
5import * as request from 'supertest' 4import * as request from 'supertest'
6import { URL } from 'url' 5import { URL } from 'url'
7import { HttpStatusCode } from '@shared/core-utils' 6import { HttpStatusCode } from '@shared/models'
8import { buildAbsoluteFixturePath, root } from '../miscs/tests' 7import { buildAbsoluteFixturePath } from '../miscs/tests'
9 8
10function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) { 9export type CommonRequestParams = {
11 const { host, protocol, pathname } = new URL(url)
12
13 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
14}
15
16function makeGetRequest (options: {
17 url: string 10 url: string
18 path?: string 11 path?: string
19 query?: any
20 token?: string
21 statusCodeExpected?: HttpStatusCode
22 contentType?: string 12 contentType?: string
23 range?: string 13 range?: string
24 redirects?: number 14 redirects?: number
25 accept?: string 15 accept?: string
26 host?: string 16 host?: string
27}) { 17 token?: string
28 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 18 headers?: { [ name: string ]: string }
29 if (options.contentType === undefined) options.contentType = 'application/json' 19 type?: string
30 20 xForwardedFor?: string
31 const req = request(options.url).get(options.path) 21 expectedStatus?: HttpStatusCode
22}
32 23
33 if (options.contentType) req.set('Accept', options.contentType) 24function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: string) {
34 if (options.token) req.set('Authorization', 'Bearer ' + options.token) 25 const { host, protocol, pathname } = new URL(url)
35 if (options.query) req.query(options.query)
36 if (options.range) req.set('Range', options.range)
37 if (options.accept) req.set('Accept', options.accept)
38 if (options.host) req.set('Host', options.host)
39 if (options.redirects) req.redirects(options.redirects)
40 26
41 return req.expect(options.statusCodeExpected) 27 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range })
42} 28}
43 29
44function makeDeleteRequest (options: { 30function makeGetRequest (options: CommonRequestParams & {
45 url: string 31 query?: any
46 path: string
47 token?: string
48 statusCodeExpected?: HttpStatusCode
49}) { 32}) {
50 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 33 const req = request(options.url).get(options.path)
34 .query(options.query)
51 35
52 const req = request(options.url) 36 return buildRequest(req, { contentType: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
53 .delete(options.path) 37}
54 .set('Accept', 'application/json')
55 38
56 if (options.token) req.set('Authorization', 'Bearer ' + options.token) 39function makeHTMLRequest (url: string, path: string) {
40 return makeGetRequest({
41 url,
42 path,
43 accept: 'text/html',
44 expectedStatus: HttpStatusCode.OK_200
45 })
46}
57 47
58 return req.expect(options.statusCodeExpected) 48function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
49 return makeGetRequest({
50 url,
51 path,
52 expectedStatus: expectedStatus,
53 accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
54 })
59} 55}
60 56
61function makeUploadRequest (options: { 57function makeDeleteRequest (options: CommonRequestParams) {
62 url: string 58 const req = request(options.url).delete(options.path)
59
60 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
61}
62
63function makeUploadRequest (options: CommonRequestParams & {
63 method?: 'POST' | 'PUT' 64 method?: 'POST' | 'PUT'
64 path: string
65 token?: string
66 65
67 fields: { [ fieldName: string ]: any } 66 fields: { [ fieldName: string ]: any }
68 attaches?: { [ attachName: string ]: any | any[] } 67 attaches?: { [ attachName: string ]: any | any[] }
69
70 headers?: { [ name: string ]: string }
71
72 statusCodeExpected?: HttpStatusCode
73}) { 68}) {
74 if (options.statusCodeExpected === undefined) { 69 let req = options.method === 'PUT'
75 options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 70 ? request(options.url).put(options.path)
76 } 71 : request(options.url).post(options.path)
77 72
78 let req: request.Test 73 req = buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
79 if (options.method === 'PUT') {
80 req = request(options.url).put(options.path)
81 } else {
82 req = request(options.url).post(options.path)
83 }
84 74
85 req.set('Accept', 'application/json') 75 buildFields(req, options.fields)
86
87 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
88
89 Object.keys(options.headers || {}).forEach(name => {
90 req.set(name, options.headers[name])
91 })
92
93 Object.keys(options.fields).forEach(field => {
94 const value = options.fields[field]
95
96 if (value === undefined) return
97
98 if (Array.isArray(value)) {
99 for (let i = 0; i < value.length; i++) {
100 req.field(field + '[' + i + ']', value[i])
101 }
102 } else {
103 req.field(field, value)
104 }
105 })
106 76
107 Object.keys(options.attaches || {}).forEach(attach => { 77 Object.keys(options.attaches || {}).forEach(attach => {
108 const value = options.attaches[attach] 78 const value = options.attaches[attach]
79
109 if (Array.isArray(value)) { 80 if (Array.isArray(value)) {
110 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) 81 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
111 } else { 82 } else {
@@ -113,40 +84,16 @@ function makeUploadRequest (options: {
113 } 84 }
114 }) 85 })
115 86
116 if (options.statusCodeExpected) {
117 req.expect(options.statusCodeExpected)
118 }
119
120 return req 87 return req
121} 88}
122 89
123function makePostBodyRequest (options: { 90function makePostBodyRequest (options: CommonRequestParams & {
124 url: string
125 path: string
126 token?: string
127 fields?: { [ fieldName: string ]: any } 91 fields?: { [ fieldName: string ]: any }
128 headers?: { [ name: string ]: string }
129 type?: string
130 xForwardedFor?: string
131 statusCodeExpected?: HttpStatusCode
132}) { 92}) {
133 if (!options.fields) options.fields = {} 93 const req = request(options.url).post(options.path)
134 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 94 .send(options.fields)
135
136 const req = request(options.url)
137 .post(options.path)
138 .set('Accept', 'application/json')
139 95
140 if (options.token) req.set('Authorization', 'Bearer ' + options.token) 96 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
141 if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor)
142 if (options.type) req.type(options.type)
143
144 Object.keys(options.headers || {}).forEach(name => {
145 req.set(name, options.headers[name])
146 })
147
148 return req.send(options.fields)
149 .expect(options.statusCodeExpected)
150} 97}
151 98
152function makePutBodyRequest (options: { 99function makePutBodyRequest (options: {
@@ -154,58 +101,12 @@ function makePutBodyRequest (options: {
154 path: string 101 path: string
155 token?: string 102 token?: string
156 fields: { [ fieldName: string ]: any } 103 fields: { [ fieldName: string ]: any }
157 statusCodeExpected?: HttpStatusCode 104 expectedStatus?: HttpStatusCode
158}) {
159 if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400
160
161 const req = request(options.url)
162 .put(options.path)
163 .set('Accept', 'application/json')
164
165 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
166
167 return req.send(options.fields)
168 .expect(options.statusCodeExpected)
169}
170
171function makeHTMLRequest (url: string, path: string) {
172 return request(url)
173 .get(path)
174 .set('Accept', 'text/html')
175 .expect(HttpStatusCode.OK_200)
176}
177
178function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
179 return makeGetRequest({
180 url,
181 path,
182 statusCodeExpected: expectedStatus,
183 accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
184 })
185}
186
187function updateImageRequest (options: {
188 url: string
189 path: string
190 accessToken: string
191 fixture: string
192 fieldname: string
193}) { 105}) {
194 let filePath = '' 106 const req = request(options.url).put(options.path)
195 if (isAbsolute(options.fixture)) { 107 .send(options.fields)
196 filePath = options.fixture
197 } else {
198 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
199 }
200 108
201 return makeUploadRequest({ 109 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
202 url: options.url,
203 path: options.path,
204 token: options.accessToken,
205 fields: {},
206 attaches: { [options.fieldname]: filePath },
207 statusCodeExpected: HttpStatusCode.OK_200
208 })
209} 110}
210 111
211function decodeQueryString (path: string) { 112function decodeQueryString (path: string) {
@@ -233,6 +134,49 @@ export {
233 makeRawRequest, 134 makeRawRequest,
234 makeActivityPubGetRequest, 135 makeActivityPubGetRequest,
235 unwrapBody, 136 unwrapBody,
236 unwrapText, 137 unwrapText
237 updateImageRequest 138}
139
140// ---------------------------------------------------------------------------
141
142function buildRequest (req: request.Test, options: CommonRequestParams) {
143 if (options.contentType) req.set('Accept', options.contentType)
144 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
145 if (options.range) req.set('Range', options.range)
146 if (options.accept) req.set('Accept', options.accept)
147 if (options.host) req.set('Host', options.host)
148 if (options.redirects) req.redirects(options.redirects)
149 if (options.expectedStatus) req.expect(options.expectedStatus)
150 if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor)
151 if (options.type) req.type(options.type)
152
153 Object.keys(options.headers || {}).forEach(name => {
154 req.set(name, options.headers[name])
155 })
156
157 return req
158}
159
160function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) {
161 if (!fields) return
162
163 let formKey: string
164
165 for (const key of Object.keys(fields)) {
166 if (namespace) formKey = `${namespace}[${key}]`
167 else formKey = key
168
169 if (fields[key] === undefined) continue
170
171 if (Array.isArray(fields[key]) && fields[key].length === 0) {
172 req.field(key, null)
173 continue
174 }
175
176 if (fields[key] !== null && typeof fields[key] === 'object') {
177 buildFields(req, fields[key], formKey)
178 } else {
179 req.field(formKey, fields[key])
180 }
181 }
238} 182}