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