]>
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 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
9 | ||
10 | function get4KFileUrl () { | |
11 | return 'https://download.cpy.re/peertube/4k_file.txt' | |
12 | } | |
13 | ||
14 | function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) { | |
15 | const { host, protocol, pathname } = new URL(url) | |
16 | ||
17 | return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range }) | |
18 | } | |
19 | ||
20 | function makeGetRequest (options: { | |
21 | url: string | |
22 | path?: string | |
23 | query?: any | |
24 | token?: string | |
25 | statusCodeExpected?: HttpStatusCode | |
26 | contentType?: string | |
27 | range?: string | |
28 | redirects?: number | |
29 | accept?: string | |
30 | }) { | |
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) | |
35 | ||
36 | if (options.contentType) req.set('Accept', options.contentType) | |
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 | } | |
45 | ||
46 | function makeDeleteRequest (options: { | |
47 | url: string | |
48 | path: string | |
49 | token?: string | |
50 | statusCodeExpected?: HttpStatusCode | |
51 | }) { | |
52 | if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 | |
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 | ||
60 | return req.expect(options.statusCodeExpected) | |
61 | } | |
62 | ||
63 | function makeUploadRequest (options: { | |
64 | url: string | |
65 | method?: 'POST' | 'PUT' | |
66 | path: string | |
67 | token?: string | |
68 | fields: { [ fieldName: string ]: any } | |
69 | attaches?: { [ attachName: string ]: any | any[] } | |
70 | statusCodeExpected?: HttpStatusCode | |
71 | }) { | |
72 | if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 | |
73 | ||
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') | |
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 | ||
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 | ||
99 | Object.keys(options.attaches || {}).forEach(attach => { | |
100 | const value = options.attaches[attach] | |
101 | if (Array.isArray(value)) { | |
102 | req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) | |
103 | } else { | |
104 | req.attach(attach, buildAbsoluteFixturePath(value)) | |
105 | } | |
106 | }) | |
107 | ||
108 | return req.expect(options.statusCodeExpected) | |
109 | } | |
110 | ||
111 | function makePostBodyRequest (options: { | |
112 | url: string | |
113 | path: string | |
114 | token?: string | |
115 | fields?: { [ fieldName: string ]: any } | |
116 | statusCodeExpected?: HttpStatusCode | |
117 | }) { | |
118 | if (!options.fields) options.fields = {} | |
119 | if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 | |
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 | ||
131 | function makePutBodyRequest (options: { | |
132 | url: string | |
133 | path: string | |
134 | token?: string | |
135 | fields: { [ fieldName: string ]: any } | |
136 | statusCodeExpected?: HttpStatusCode | |
137 | }) { | |
138 | if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 | |
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 | ||
150 | function makeHTMLRequest (url: string, path: string) { | |
151 | return request(url) | |
152 | .get(path) | |
153 | .set('Accept', 'text/html') | |
154 | .expect(HttpStatusCode.OK_200) | |
155 | } | |
156 | ||
157 | function updateImageRequest (options: { | |
158 | url: string | |
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 | } | |
180 | ||
181 | function decodeQueryString (path: string) { | |
182 | return decode(path.split('?')[1]) | |
183 | } | |
184 | ||
185 | // --------------------------------------------------------------------------- | |
186 | ||
187 | export { | |
188 | get4KFileUrl, | |
189 | makeHTMLRequest, | |
190 | makeGetRequest, | |
191 | decodeQueryString, | |
192 | makeUploadRequest, | |
193 | makePostBodyRequest, | |
194 | makePutBodyRequest, | |
195 | makeDeleteRequest, | |
196 | makeRawRequest, | |
197 | updateImageRequest | |
198 | } |