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 { HttpStatusCode } from '@shared/models'
7 import { buildAbsoluteFixturePath } from '../miscs/tests'
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 & {
33 const req = request(options.url).get(options.path)
36 return buildRequest(req, { contentType: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
39 function makeHTMLRequest (url: string, path: string) {
40 return makeGetRequest({
44 expectedStatus: HttpStatusCode.OK_200
48 function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) {
49 return makeGetRequest({
52 expectedStatus: expectedStatus,
53 accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8'
57 function makeDeleteRequest (options: CommonRequestParams) {
58 const req = request(options.url).delete(options.path)
60 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
63 function makeUploadRequest (options: CommonRequestParams & {
64 method?: 'POST' | 'PUT'
66 fields: { [ fieldName: string ]: any }
67 attaches?: { [ attachName: string ]: any | any[] }
69 let req = options.method === 'PUT'
70 ? request(options.url).put(options.path)
71 : request(options.url).post(options.path)
73 req = buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
75 buildFields(req, options.fields)
77 Object.keys(options.attaches || {}).forEach(attach => {
78 const value = options.attaches[attach]
80 if (Array.isArray(value)) {
81 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
83 req.attach(attach, buildAbsoluteFixturePath(value))
90 function makePostBodyRequest (options: CommonRequestParams & {
91 fields?: { [ fieldName: string ]: any }
93 const req = request(options.url).post(options.path)
96 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
99 function makePutBodyRequest (options: {
103 fields: { [ fieldName: string ]: any }
104 expectedStatus?: HttpStatusCode
106 const req = request(options.url).put(options.path)
107 .send(options.fields)
109 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
112 function decodeQueryString (path: string) {
113 return decode(path.split('?')[1])
116 function unwrapBody <T> (test: request.Test): Promise<T> {
117 return test.then(res => res.body)
120 function unwrapText (test: request.Test): Promise<string> {
121 return test.then(res => res.text)
124 function unwrapBodyOrDecodeToJSON <T> (test: request.Test): Promise<T> {
125 return test.then(res => {
126 if (res.body instanceof Buffer) {
127 return JSON.parse(new TextDecoder().decode(res.body))
134 function unwrapTextOrDecode (test: request.Test): Promise<string> {
135 return test.then(res => res.text || new TextDecoder().decode(res.body))
138 // ---------------------------------------------------------------------------
149 makeActivityPubGetRequest,
152 unwrapBodyOrDecodeToJSON,
156 // ---------------------------------------------------------------------------
158 function buildRequest (req: request.Test, options: CommonRequestParams) {
159 if (options.contentType) req.set('Accept', options.contentType)
160 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
161 if (options.range) req.set('Range', options.range)
162 if (options.accept) req.set('Accept', options.accept)
163 if (options.host) req.set('Host', options.host)
164 if (options.redirects) req.redirects(options.redirects)
165 if (options.expectedStatus) req.expect(options.expectedStatus)
166 if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor)
167 if (options.type) req.type(options.type)
169 Object.keys(options.headers || {}).forEach(name => {
170 req.set(name, options.headers[name])
176 function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) {
181 for (const key of Object.keys(fields)) {
182 if (namespace) formKey = `${namespace}[${key}]`
185 if (fields[key] === undefined) continue
187 if (Array.isArray(fields[key]) && fields[key].length === 0) {
192 if (fields[key] !== null && typeof fields[key] === 'object') {
193 buildFields(req, fields[key], formKey)
195 req.field(formKey, fields[key])