]>
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 | }) { | |
30 | if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 | |
31 | if (options.contentType === undefined) options.contentType = 'application/json' | |
32 | ||
33 | const req = request(options.url).get(options.path) | |
34 | ||
35 | if (options.contentType) req.set('Accept', options.contentType) | |
36 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
37 | if (options.query) req.query(options.query) | |
38 | if (options.range) req.set('Range', options.range) | |
39 | if (options.redirects) req.redirects(options.redirects) | |
40 | ||
41 | return req.expect(options.statusCodeExpected) | |
42 | } | |
43 | ||
44 | function makeDeleteRequest (options: { | |
45 | url: string | |
46 | path: string | |
47 | token?: string | |
48 | statusCodeExpected?: HttpStatusCode | |
49 | }) { | |
50 | if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 | |
51 | ||
52 | const req = request(options.url) | |
53 | .delete(options.path) | |
54 | .set('Accept', 'application/json') | |
55 | ||
56 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
57 | ||
58 | return req.expect(options.statusCodeExpected) | |
59 | } | |
60 | ||
61 | function makeUploadRequest (options: { | |
62 | url: string | |
63 | method?: 'POST' | 'PUT' | |
64 | path: string | |
65 | token?: string | |
66 | fields: { [ fieldName: string ]: any } | |
67 | attaches?: { [ attachName: string ]: any | any[] } | |
68 | statusCodeExpected?: HttpStatusCode | |
69 | }) { | |
70 | if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 | |
71 | ||
72 | let req: request.Test | |
73 | if (options.method === 'PUT') { | |
74 | req = request(options.url).put(options.path) | |
75 | } else { | |
76 | req = request(options.url).post(options.path) | |
77 | } | |
78 | ||
79 | req.set('Accept', 'application/json') | |
80 | ||
81 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
82 | ||
83 | Object.keys(options.fields).forEach(field => { | |
84 | const value = options.fields[field] | |
85 | ||
86 | if (value === undefined) return | |
87 | ||
88 | if (Array.isArray(value)) { | |
89 | for (let i = 0; i < value.length; i++) { | |
90 | req.field(field + '[' + i + ']', value[i]) | |
91 | } | |
92 | } else { | |
93 | req.field(field, value) | |
94 | } | |
95 | }) | |
96 | ||
97 | Object.keys(options.attaches || {}).forEach(attach => { | |
98 | const value = options.attaches[attach] | |
99 | if (Array.isArray(value)) { | |
100 | req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) | |
101 | } else { | |
102 | req.attach(attach, buildAbsoluteFixturePath(value)) | |
103 | } | |
104 | }) | |
105 | ||
106 | return req.expect(options.statusCodeExpected) | |
107 | } | |
108 | ||
109 | function makePostBodyRequest (options: { | |
110 | url: string | |
111 | path: string | |
112 | token?: string | |
113 | fields?: { [ fieldName: string ]: any } | |
114 | statusCodeExpected?: HttpStatusCode | |
115 | }) { | |
116 | if (!options.fields) options.fields = {} | |
117 | if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 | |
118 | ||
119 | const req = request(options.url) | |
120 | .post(options.path) | |
121 | .set('Accept', 'application/json') | |
122 | ||
123 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
124 | ||
125 | return req.send(options.fields) | |
126 | .expect(options.statusCodeExpected) | |
127 | } | |
128 | ||
129 | function makePutBodyRequest (options: { | |
130 | url: string | |
131 | path: string | |
132 | token?: string | |
133 | fields: { [ fieldName: string ]: any } | |
134 | statusCodeExpected?: HttpStatusCode | |
135 | }) { | |
136 | if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 | |
137 | ||
138 | const req = request(options.url) | |
139 | .put(options.path) | |
140 | .set('Accept', 'application/json') | |
141 | ||
142 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
143 | ||
144 | return req.send(options.fields) | |
145 | .expect(options.statusCodeExpected) | |
146 | } | |
147 | ||
148 | function makeHTMLRequest (url: string, path: string) { | |
149 | return request(url) | |
150 | .get(path) | |
151 | .set('Accept', 'text/html') | |
152 | .expect(HttpStatusCode.OK_200) | |
153 | } | |
154 | ||
155 | function updateImageRequest (options: { | |
156 | url: string | |
157 | path: string | |
158 | accessToken: string | |
159 | fixture: string | |
160 | fieldname: string | |
161 | }) { | |
162 | let filePath = '' | |
163 | if (isAbsolute(options.fixture)) { | |
164 | filePath = options.fixture | |
165 | } else { | |
166 | filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture) | |
167 | } | |
168 | ||
169 | return makeUploadRequest({ | |
170 | url: options.url, | |
171 | path: options.path, | |
172 | token: options.accessToken, | |
173 | fields: {}, | |
174 | attaches: { [options.fieldname]: filePath }, | |
175 | statusCodeExpected: HttpStatusCode.OK_200 | |
176 | }) | |
177 | } | |
178 | ||
179 | function decodeQueryString (path: string) { | |
180 | return decode(path.split('?')[1]) | |
181 | } | |
182 | ||
183 | // --------------------------------------------------------------------------- | |
184 | ||
185 | export { | |
186 | get4KFileUrl, | |
187 | makeHTMLRequest, | |
188 | makeGetRequest, | |
189 | decodeQueryString, | |
190 | makeUploadRequest, | |
191 | makePostBodyRequest, | |
192 | makePutBodyRequest, | |
193 | makeDeleteRequest, | |
194 | makeRawRequest, | |
195 | updateImageRequest | |
196 | } |