1 import { isAbsolute, join } from 'path'
2 import { root } from '../miscs/tests'
11 } from '../requests/requests'
12 import { ServerInfo } from '../server/servers'
14 export interface OverrideCommandOptions {
16 expectedStatus?: number
19 interface InternalCommonCommandOptions extends OverrideCommandOptions {
20 // Default to server.url
24 // If we automatically send the server token if the token is not provided
25 implicitToken: boolean
26 defaultExpectedStatus: number
29 interface InternalGetCommandOptions extends InternalCommonCommandOptions {
30 query?: { [ id: string ]: any }
38 abstract class AbstractCommand {
41 protected server: ServerInfo
46 protected getRequestBody <T> (options: InternalGetCommandOptions) {
47 return unwrapBody<T>(this.getRequest(options))
50 protected getRequestText (options: InternalGetCommandOptions) {
51 return unwrapText(this.getRequest(options))
54 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
55 const { url, range } = options
56 const { host, protocol, pathname } = new URL(url)
58 return this.getRequest({
61 token: this.buildCommonRequestToken(options),
62 defaultExpectedStatus: this.buildStatusCodeExpected(options),
64 url: `${protocol}//${host}`,
70 protected getRequest (options: InternalGetCommandOptions) {
71 const { redirects, query, contentType, accept, range, host } = options
73 return makeGetRequest({
74 ...this.buildCommonRequestOptions(options),
85 protected deleteRequest (options: InternalCommonCommandOptions) {
86 return makeDeleteRequest(this.buildCommonRequestOptions(options))
89 protected putBodyRequest (options: InternalCommonCommandOptions & {
90 fields?: { [ fieldName: string ]: any }
92 const { fields } = options
94 return makePutBodyRequest({
95 ...this.buildCommonRequestOptions(options),
101 protected postBodyRequest (options: InternalCommonCommandOptions & {
102 fields?: { [ fieldName: string ]: any }
103 headers?: { [ name: string ]: string }
105 xForwardedFor?: string
107 const { type, fields, xForwardedFor, headers } = options
109 return makePostBodyRequest({
110 ...this.buildCommonRequestOptions(options),
119 protected postUploadRequest (options: InternalCommonCommandOptions & {
120 fields?: { [ fieldName: string ]: any }
121 attaches?: { [ fieldName: string ]: any }
122 headers?: { [ name: string ]: string }
124 const { fields, attaches, headers } = options
126 return makeUploadRequest({
127 ...this.buildCommonRequestOptions(options),
136 protected putUploadRequest (options: InternalCommonCommandOptions & {
137 fields?: { [ fieldName: string ]: any }
138 attaches?: { [ fieldName: string ]: any }
139 headers?: { [ name: string ]: string }
141 const { fields, attaches, headers } = options
143 return makeUploadRequest({
144 ...this.buildCommonRequestOptions(options),
153 protected updateImageRequest (options: InternalCommonCommandOptions & {
158 if (isAbsolute(options.fixture)) {
159 filePath = options.fixture
161 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
164 return this.postUploadRequest({
168 attaches: { [options.fieldname]: filePath }
172 protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
173 const { url, path } = options
176 url: url ?? this.server.url,
179 token: this.buildCommonRequestToken(options),
180 statusCodeExpected: this.buildStatusCodeExpected(options)
184 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
185 const { token } = options
187 const fallbackToken = options.implicitToken
188 ? this.server.accessToken
191 return token !== undefined ? token : fallbackToken
194 protected buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
195 const { expectedStatus, defaultExpectedStatus } = options
197 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus