1 /* eslint-disable @typescript-eslint/no-floating-promises */
3 import { decode } from 'querystring'
4 import request from 'supertest'
5 import { URL } from 'url'
6 import { buildAbsoluteFixturePath } from '@shared/core-utils'
7 import { HttpStatusCode } from '@shared/models'
9 export type CommonRequestParams = {
18 headers?: { [ name: string ]: string }
20 xForwardedFor?: string
21 expectedStatus?: HttpStatusCode
24 function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: string) {
25 const { host, protocol, pathname } = new URL(url)
27 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range })
30 function makeGetRequest (options: CommonRequestParams & {
34 const req = request(options.url).get(options.path)
36 if (options.query) req.query(options.query)
37 if (options.rawQuery) req.query(options.rawQuery)
39 return buildRequest(req, { contentType: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
42 function makeHTMLRequest (url: string, path: string) {
43 return makeGetRequest({
47 expectedStatus: HttpStatusCode.OK_200
51 function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
52 return makeGetRequest({
55 expectedStatus: expectedStatus,
56 accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
60 function makeDeleteRequest (options: CommonRequestParams & {
64 const req = request(options.url).delete(options.path)
66 if (options.query) req.query(options.query)
67 if (options.rawQuery) req.query(options.rawQuery)
69 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
72 function makeUploadRequest (options: CommonRequestParams & {
73 method?: 'POST' | 'PUT'
75 fields: { [ fieldName: string ]: any }
76 attaches?: { [ attachName: string ]: any | any[] }
78 let req = options.method === 'PUT'
79 ? request(options.url).put(options.path)
80 : request(options.url).post(options.path)
82 req = buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
84 buildFields(req, options.fields)
86 Object.keys(options.attaches || {}).forEach(attach => {
87 const value = options.attaches[attach]
90 if (Array.isArray(value)) {
91 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
93 req.attach(attach, buildAbsoluteFixturePath(value))
100 function makePostBodyRequest (options: CommonRequestParams & {
101 fields?: { [ fieldName: string ]: any }
103 const req = request(options.url).post(options.path)
104 .send(options.fields)
106 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
109 function makePutBodyRequest (options: {
113 fields: { [ fieldName: string ]: any }
114 expectedStatus?: HttpStatusCode
116 const req = request(options.url).put(options.path)
117 .send(options.fields)
119 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
122 function decodeQueryString (path: string) {
123 return decode(path.split('?')[1])
126 function unwrapBody <T> (test: request.Test): Promise<T> {
127 return test.then(res => res.body)
130 function unwrapText (test: request.Test): Promise<string> {
131 return test.then(res => res.text)
134 function 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))
144 function unwrapTextOrDecode (test: request.Test): Promise<string> {
145 return test.then(res => res.text || new TextDecoder().decode(res.body))
148 // ---------------------------------------------------------------------------
159 makeActivityPubGetRequest,
162 unwrapBodyOrDecodeToJSON,
166 // ---------------------------------------------------------------------------
168 function 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)
179 Object.keys(options.headers || {}).forEach(name => {
180 req.set(name, options.headers[name])
186 function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) {
191 for (const key of Object.keys(fields)) {
192 if (namespace) formKey = `${namespace}[${key}]`
195 if (fields[key] === undefined) continue
197 if (Array.isArray(fields[key]) && fields[key].length === 0) {
202 if (fields[key] !== null && typeof fields[key] === 'object') {
203 buildFields(req, fields[key], formKey)
205 req.field(formKey, fields[key])