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 & {
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]
89 if (Array.isArray(value)) {
90 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
92 req.attach(attach, buildAbsoluteFixturePath(value))
99 function makePostBodyRequest (options: CommonRequestParams & {
100 fields?: { [ fieldName: string ]: any }
102 const req = request(options.url).post(options.path)
103 .send(options.fields)
105 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
108 function makePutBodyRequest (options: {
112 fields: { [ fieldName: string ]: any }
113 expectedStatus?: HttpStatusCode
115 const req = request(options.url).put(options.path)
116 .send(options.fields)
118 return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options })
121 function decodeQueryString (path: string) {
122 return decode(path.split('?')[1])
125 function unwrapBody <T> (test: request.Test): Promise<T> {
126 return test.then(res => res.body)
129 function unwrapText (test: request.Test): Promise<string> {
130 return test.then(res => res.text)
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))
143 function unwrapTextOrDecode (test: request.Test): Promise<string> {
144 return test.then(res => res.text || new TextDecoder().decode(res.body))
147 // ---------------------------------------------------------------------------
158 makeActivityPubGetRequest,
161 unwrapBodyOrDecodeToJSON,
165 // ---------------------------------------------------------------------------
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)
178 Object.keys(options.headers || {}).forEach(name => {
179 req.set(name, options.headers[name])
185 function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) {
190 for (const key of Object.keys(fields)) {
191 if (namespace) formKey = `${namespace}[${key}]`
194 if (fields[key] === undefined) continue
196 if (Array.isArray(fields[key]) && fields[key].length === 0) {
201 if (fields[key] !== null && typeof fields[key] === 'object') {
202 buildFields(req, fields[key], formKey)
204 req.field(formKey, fields[key])