]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-floating-promises */ | |
2 | ||
3 | import { decode } from 'querystring' | |
4 | import request from 'supertest' | |
5 | import { URL } from 'url' | |
6 | import { HttpStatusCode } from '@shared/models' | |
7 | import { buildAbsoluteFixturePath } from '../miscs/tests' | |
8 | ||
9 | export type CommonRequestParams = { | |
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 | |
22 | } | |
23 | ||
24 | function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: string) { | |
25 | const { host, protocol, pathname } = new URL(url) | |
26 | ||
27 | return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range }) | |
28 | } | |
29 | ||
30 | function makeGetRequest (options: CommonRequestParams & { | |
31 | query?: any | |
32 | rawQuery?: string | |
33 | }) { | |
34 | const req = request(options.url).get(options.path) | |
35 | ||
36 | if (options.query) req.query(options.query) | |
37 | if (options.rawQuery) req.query(options.rawQuery) | |
38 | ||
39 | return buildRequest(req, { contentType: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) | |
40 | } | |
41 | ||
42 | function makeHTMLRequest (url: string, path: string) { | |
43 | return makeGetRequest({ | |
44 | url, | |
45 | path, | |
46 | accept: 'text/html', | |
47 | expectedStatus: HttpStatusCode.OK_200 | |
48 | }) | |
49 | } | |
50 | ||
51 | function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) { | |
52 | return makeGetRequest({ | |
53 | url, | |
54 | path, | |
55 | expectedStatus: expectedStatus, | |
56 | accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8' | |
57 | }) | |
58 | } | |
59 | ||
60 | function makeDeleteRequest (options: CommonRequestParams & { | |
61 | query?: any | |
62 | rawQuery?: string | |
63 | }) { | |
64 | const req = request(options.url).delete(options.path) | |
65 | ||
66 | if (options.query) req.query(options.query) | |
67 | if (options.rawQuery) req.query(options.rawQuery) | |
68 | ||
69 | return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) | |
70 | } | |
71 | ||
72 | function makeUploadRequest (options: CommonRequestParams & { | |
73 | method?: 'POST' | 'PUT' | |
74 | ||
75 | fields: { [ fieldName: string ]: any } | |
76 | attaches?: { [ attachName: string ]: any | any[] } | |
77 | }) { | |
78 | let req = options.method === 'PUT' | |
79 | ? request(options.url).put(options.path) | |
80 | : request(options.url).post(options.path) | |
81 | ||
82 | req = buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) | |
83 | ||
84 | buildFields(req, options.fields) | |
85 | ||
86 | Object.keys(options.attaches || {}).forEach(attach => { | |
87 | const value = options.attaches[attach] | |
88 | ||
89 | if (Array.isArray(value)) { | |
90 | req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) | |
91 | } else { | |
92 | req.attach(attach, buildAbsoluteFixturePath(value)) | |
93 | } | |
94 | }) | |
95 | ||
96 | return req | |
97 | } | |
98 | ||
99 | function makePostBodyRequest (options: CommonRequestParams & { | |
100 | fields?: { [ fieldName: string ]: any } | |
101 | }) { | |
102 | const req = request(options.url).post(options.path) | |
103 | .send(options.fields) | |
104 | ||
105 | return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) | |
106 | } | |
107 | ||
108 | function makePutBodyRequest (options: { | |
109 | url: string | |
110 | path: string | |
111 | token?: string | |
112 | fields: { [ fieldName: string ]: any } | |
113 | expectedStatus?: HttpStatusCode | |
114 | }) { | |
115 | const req = request(options.url).put(options.path) | |
116 | .send(options.fields) | |
117 | ||
118 | return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) | |
119 | } | |
120 | ||
121 | function decodeQueryString (path: string) { | |
122 | return decode(path.split('?')[1]) | |
123 | } | |
124 | ||
125 | function unwrapBody <T> (test: request.Test): Promise<T> { | |
126 | return test.then(res => res.body) | |
127 | } | |
128 | ||
129 | function unwrapText (test: request.Test): Promise<string> { | |
130 | return test.then(res => res.text) | |
131 | } | |
132 | ||
133 | function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> { | |
134 | return test.then(res => { | |
135 | if (res.body instanceof Buffer) { | |
136 | return JSON.parse(new TextDecoder().decode(res.body)) | |
137 | } | |
138 | ||
139 | return res.body | |
140 | }) | |
141 | } | |
142 | ||
143 | function unwrapTextOrDecode (test: request.Test): Promise<string> { | |
144 | return test.then(res => res.text || new TextDecoder().decode(res.body)) | |
145 | } | |
146 | ||
147 | // --------------------------------------------------------------------------- | |
148 | ||
149 | export { | |
150 | makeHTMLRequest, | |
151 | makeGetRequest, | |
152 | decodeQueryString, | |
153 | makeUploadRequest, | |
154 | makePostBodyRequest, | |
155 | makePutBodyRequest, | |
156 | makeDeleteRequest, | |
157 | makeRawRequest, | |
158 | makeActivityPubGetRequest, | |
159 | unwrapBody, | |
160 | unwrapTextOrDecode, | |
161 | unwrapBodyOrDecodeToJSON, | |
162 | unwrapText | |
163 | } | |
164 | ||
165 | // --------------------------------------------------------------------------- | |
166 | ||
167 | function buildRequest (req: request.Test, options: CommonRequestParams) { | |
168 | if (options.contentType) req.set('Accept', options.contentType) | |
169 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
170 | if (options.range) req.set('Range', options.range) | |
171 | if (options.accept) req.set('Accept', options.accept) | |
172 | if (options.host) req.set('Host', options.host) | |
173 | if (options.redirects) req.redirects(options.redirects) | |
174 | if (options.expectedStatus) req.expect(options.expectedStatus) | |
175 | if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor) | |
176 | if (options.type) req.type(options.type) | |
177 | ||
178 | Object.keys(options.headers || {}).forEach(name => { | |
179 | req.set(name, options.headers[name]) | |
180 | }) | |
181 | ||
182 | return req | |
183 | } | |
184 | ||
185 | function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) { | |
186 | if (!fields) return | |
187 | ||
188 | let formKey: string | |
189 | ||
190 | for (const key of Object.keys(fields)) { | |
191 | if (namespace) formKey = `${namespace}[${key}]` | |
192 | else formKey = key | |
193 | ||
194 | if (fields[key] === undefined) continue | |
195 | ||
196 | if (Array.isArray(fields[key]) && fields[key].length === 0) { | |
197 | req.field(key, []) | |
198 | continue | |
199 | } | |
200 | ||
201 | if (fields[key] !== null && typeof fields[key] === 'object') { | |
202 | buildFields(req, fields[key], formKey) | |
203 | } else { | |
204 | req.field(formKey, fields[key]) | |
205 | } | |
206 | } | |
207 | } |