aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/requests/requests.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/extra-utils/requests/requests.ts')
-rw-r--r--shared/extra-utils/requests/requests.ts184
1 files changed, 184 insertions, 0 deletions
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts
new file mode 100644
index 000000000..3532fb429
--- /dev/null
+++ b/shared/extra-utils/requests/requests.ts
@@ -0,0 +1,184 @@
1import * as request from 'supertest'
2import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
3import { isAbsolute, join } from 'path'
4import { parse } from 'url'
5
6function get4KFileUrl () {
7 return 'https://download.cpy.re/peertube/4k_file.txt'
8}
9
10function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
11 const { host, protocol, pathname } = parse(url)
12
13 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
14}
15
16function makeGetRequest (options: {
17 url: string,
18 path?: string,
19 query?: any,
20 token?: string,
21 statusCodeExpected?: number,
22 contentType?: string,
23 range?: string
24}) {
25 if (!options.statusCodeExpected) options.statusCodeExpected = 400
26 if (options.contentType === undefined) options.contentType = 'application/json'
27
28 const req = request(options.url).get(options.path)
29
30 if (options.contentType) req.set('Accept', options.contentType)
31 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
32 if (options.query) req.query(options.query)
33 if (options.range) req.set('Range', options.range)
34
35 return req.expect(options.statusCodeExpected)
36}
37
38function makeDeleteRequest (options: {
39 url: string,
40 path: string,
41 token?: string,
42 statusCodeExpected?: number
43}) {
44 if (!options.statusCodeExpected) options.statusCodeExpected = 400
45
46 const req = request(options.url)
47 .delete(options.path)
48 .set('Accept', 'application/json')
49
50 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
51
52 return req.expect(options.statusCodeExpected)
53}
54
55function makeUploadRequest (options: {
56 url: string,
57 method?: 'POST' | 'PUT',
58 path: string,
59 token?: string,
60 fields: { [ fieldName: string ]: any },
61 attaches: { [ attachName: string ]: any | any[] },
62 statusCodeExpected?: number
63}) {
64 if (!options.statusCodeExpected) options.statusCodeExpected = 400
65
66 let req: request.Test
67 if (options.method === 'PUT') {
68 req = request(options.url).put(options.path)
69 } else {
70 req = request(options.url).post(options.path)
71 }
72
73 req.set('Accept', 'application/json')
74
75 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
76
77 Object.keys(options.fields).forEach(field => {
78 const value = options.fields[field]
79
80 if (value === undefined) return
81
82 if (Array.isArray(value)) {
83 for (let i = 0; i < value.length; i++) {
84 req.field(field + '[' + i + ']', value[i])
85 }
86 } else {
87 req.field(field, value)
88 }
89 })
90
91 Object.keys(options.attaches).forEach(attach => {
92 const value = options.attaches[attach]
93 if (Array.isArray(value)) {
94 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
95 } else {
96 req.attach(attach, buildAbsoluteFixturePath(value))
97 }
98 })
99
100 return req.expect(options.statusCodeExpected)
101}
102
103function makePostBodyRequest (options: {
104 url: string,
105 path: string,
106 token?: string,
107 fields?: { [ fieldName: string ]: any },
108 statusCodeExpected?: number
109}) {
110 if (!options.fields) options.fields = {}
111 if (!options.statusCodeExpected) options.statusCodeExpected = 400
112
113 const req = request(options.url)
114 .post(options.path)
115 .set('Accept', 'application/json')
116
117 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
118
119 return req.send(options.fields)
120 .expect(options.statusCodeExpected)
121}
122
123function makePutBodyRequest (options: {
124 url: string,
125 path: string,
126 token?: string,
127 fields: { [ fieldName: string ]: any },
128 statusCodeExpected?: number
129}) {
130 if (!options.statusCodeExpected) options.statusCodeExpected = 400
131
132 const req = request(options.url)
133 .put(options.path)
134 .set('Accept', 'application/json')
135
136 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
137
138 return req.send(options.fields)
139 .expect(options.statusCodeExpected)
140}
141
142function makeHTMLRequest (url: string, path: string) {
143 return request(url)
144 .get(path)
145 .set('Accept', 'text/html')
146 .expect(200)
147}
148
149function updateAvatarRequest (options: {
150 url: string,
151 path: string,
152 accessToken: string,
153 fixture: string
154}) {
155 let filePath = ''
156 if (isAbsolute(options.fixture)) {
157 filePath = options.fixture
158 } else {
159 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
160 }
161
162 return makeUploadRequest({
163 url: options.url,
164 path: options.path,
165 token: options.accessToken,
166 fields: {},
167 attaches: { avatarfile: filePath },
168 statusCodeExpected: 200
169 })
170}
171
172// ---------------------------------------------------------------------------
173
174export {
175 get4KFileUrl,
176 makeHTMLRequest,
177 makeGetRequest,
178 makeUploadRequest,
179 makePostBodyRequest,
180 makePutBodyRequest,
181 makeDeleteRequest,
182 makeRawRequest,
183 updateAvatarRequest
184}