aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/requests/requests.ts
diff options
context:
space:
mode:
Diffstat (limited to 'shared/server-commands/requests/requests.ts')
-rw-r--r--shared/server-commands/requests/requests.ts208
1 files changed, 208 insertions, 0 deletions
diff --git a/shared/server-commands/requests/requests.ts b/shared/server-commands/requests/requests.ts
new file mode 100644
index 000000000..b6b9024ed
--- /dev/null
+++ b/shared/server-commands/requests/requests.ts
@@ -0,0 +1,208 @@
1/* eslint-disable @typescript-eslint/no-floating-promises */
2
3import { decode } from 'querystring'
4import request from 'supertest'
5import { URL } from 'url'
6import { HttpStatusCode } from '@shared/models'
7import { buildAbsoluteFixturePath } from '../miscs/tests'
8
9export 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
24function 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
30function 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
42function makeHTMLRequest (url: string, path: string) {
43 return makeGetRequest({
44 url,
45 path,
46 accept: 'text/html',
47 expectedStatus: HttpStatusCode.OK_200
48 })
49}
50
51function 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
60function 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
72function 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 if (!value) return
89
90 if (Array.isArray(value)) {
91 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
92 } else {
93 req.attach(attach, buildAbsoluteFixturePath(value))
94 }
95 })
96
97 return req
98}
99
100function makePostBodyRequest (options: CommonRequestParams & {
101 fields?: { [ fieldName: string ]: any }
102}) {
103 const req = request(options.url).post(options.path)
104 .send(options.fields)
105
106 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
107}
108
109function makePutBodyRequest (options: {
110 url: string
111 path: string
112 token?: string
113 fields: { [ fieldName: string ]: any }
114 expectedStatus?: HttpStatusCode
115}) {
116 const req = request(options.url).put(options.path)
117 .send(options.fields)
118
119 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
120}
121
122function decodeQueryString (path: string) {
123 return decode(path.split('?')[1])
124}
125
126function unwrapBody <T> (test: request.Test): Promise<T> {
127 return test.then(res => res.body)
128}
129
130function unwrapText (test: request.Test): Promise<string> {
131 return test.then(res => res.text)
132}
133
134function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> {
135 return test.then(res => {
136 if (res.body instanceof Buffer) {
137 return JSON.parse(new TextDecoder().decode(res.body))
138 }
139
140 return res.body
141 })
142}
143
144function unwrapTextOrDecode (test: request.Test): Promise<string> {
145 return test.then(res => res.text || new TextDecoder().decode(res.body))
146}
147
148// ---------------------------------------------------------------------------
149
150export {
151 makeHTMLRequest,
152 makeGetRequest,
153 decodeQueryString,
154 makeUploadRequest,
155 makePostBodyRequest,
156 makePutBodyRequest,
157 makeDeleteRequest,
158 makeRawRequest,
159 makeActivityPubGetRequest,
160 unwrapBody,
161 unwrapTextOrDecode,
162 unwrapBodyOrDecodeToJSON,
163 unwrapText
164}
165
166// ---------------------------------------------------------------------------
167
168function buildRequest (req: request.Test, options: CommonRequestParams) {
169 if (options.contentType) req.set('Accept', options.contentType)
170 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
171 if (options.range) req.set('Range', options.range)
172 if (options.accept) req.set('Accept', options.accept)
173 if (options.host) req.set('Host', options.host)
174 if (options.redirects) req.redirects(options.redirects)
175 if (options.expectedStatus) req.expect(options.expectedStatus)
176 if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor)
177 if (options.type) req.type(options.type)
178
179 Object.keys(options.headers || {}).forEach(name => {
180 req.set(name, options.headers[name])
181 })
182
183 return req
184}
185
186function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) {
187 if (!fields) return
188
189 let formKey: string
190
191 for (const key of Object.keys(fields)) {
192 if (namespace) formKey = `${namespace}[${key}]`
193 else formKey = key
194
195 if (fields[key] === undefined) continue
196
197 if (Array.isArray(fields[key]) && fields[key].length === 0) {
198 req.field(key, [])
199 continue
200 }
201
202 if (fields[key] !== null && typeof fields[key] === 'object') {
203 buildFields(req, fields[key], formKey)
204 } else {
205 req.field(formKey, fields[key])
206 }
207 }
208}