import { isAbsolute, join } from 'path'
-import { HttpStatusCode } from '@shared/core-utils'
-import { root } from '../miscs/miscs'
+import { root } from '../miscs/tests'
import {
makeDeleteRequest,
makeGetRequest,
unwrapBody,
unwrapText
} from '../requests/requests'
-import { ServerInfo } from '../server/servers'
+import { PeerTubeServer } from '../server/server'
export interface OverrideCommandOptions {
token?: string
// If we automatically send the server token if the token is not provided
implicitToken: boolean
defaultExpectedStatus: number
-}
-interface InternalGetCommandOptions extends InternalCommonCommandOptions {
- query?: { [ id: string ]: any }
+ // Common optional request parameters
contentType?: string
accept?: string
redirects?: number
range?: string
+ host?: string
+ headers?: { [ name: string ]: string }
+ requestType?: string
+ xForwardedFor?: string
}
-abstract class AbstractCommand {
+interface InternalGetCommandOptions extends InternalCommonCommandOptions {
+ query?: { [ id: string ]: any }
+}
- private expectedStatus: HttpStatusCode
+interface InternalDeleteCommandOptions extends InternalCommonCommandOptions {
+ query?: { [ id: string ]: any }
+ rawQuery?: string
+}
+
+abstract class AbstractCommand {
constructor (
- protected server: ServerInfo
+ protected server: PeerTubeServer
) {
}
- setServer (server: ServerInfo) {
- this.server = server
- }
-
- setExpectedStatus (status: HttpStatusCode) {
- this.expectedStatus = status
- }
-
protected getRequestBody <T> (options: InternalGetCommandOptions) {
return unwrapBody<T>(this.getRequest(options))
}
...options,
token: this.buildCommonRequestToken(options),
- defaultExpectedStatus: this.buildStatusCodeExpected(options),
+ defaultExpectedStatus: this.buildExpectedStatus(options),
url: `${protocol}//${host}`,
path: pathname,
}
protected getRequest (options: InternalGetCommandOptions) {
- const { redirects, query, contentType, accept, range } = options
+ const { query } = options
return makeGetRequest({
...this.buildCommonRequestOptions(options),
- redirects,
- query,
- contentType,
- range,
- accept
+ query
})
}
- protected deleteRequest (options: InternalCommonCommandOptions) {
- return makeDeleteRequest(this.buildCommonRequestOptions(options))
+ protected deleteRequest (options: InternalDeleteCommandOptions) {
+ const { query, rawQuery } = options
+
+ return makeDeleteRequest({
+ ...this.buildCommonRequestOptions(options),
+
+ query,
+ rawQuery
+ })
}
protected putBodyRequest (options: InternalCommonCommandOptions & {
protected postUploadRequest (options: InternalCommonCommandOptions & {
fields?: { [ fieldName: string ]: any }
- attaches?: any
+ attaches?: { [ fieldName: string ]: any }
}) {
const { fields, attaches } = options
protected putUploadRequest (options: InternalCommonCommandOptions & {
fields?: { [ fieldName: string ]: any }
- attaches?: any
+ attaches?: { [ fieldName: string ]: any }
}) {
const { fields, attaches } = options
fixture: string
fieldname: string
}) {
- let filePath = ''
- if (isAbsolute(options.fixture)) {
- filePath = options.fixture
- } else {
- filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
- }
+ const filePath = isAbsolute(options.fixture)
+ ? options.fixture
+ : join(root(), 'server', 'tests', 'fixtures', options.fixture)
return this.postUploadRequest({
...options,
})
}
- private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
- const { url, path } = options
+ protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
+ const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor } = options
return {
url: url ?? this.server.url,
path,
token: this.buildCommonRequestToken(options),
- statusCodeExpected: this.buildStatusCodeExpected(options)
+ expectedStatus: this.buildExpectedStatus(options),
+
+ redirects,
+ contentType,
+ range,
+ host,
+ accept,
+ headers,
+ type: requestType,
+ xForwardedFor
}
}
- private buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
+ protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
const { token } = options
const fallbackToken = options.implicitToken
return token !== undefined ? token : fallbackToken
}
- private buildStatusCodeExpected (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
+ protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
const { expectedStatus, defaultExpectedStatus } = options
- return expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
+ return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
}
}