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