]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/server-commands/shared/abstract-command.ts
Add runner server tests
[github/Chocobozzz/PeerTube.git] / shared / server-commands / shared / abstract-command.ts
CommitLineData
a5461888 1import { isAbsolute, join } from 'path'
c55e3d72 2import { root } from '@shared/core-utils'
a1637fa1
C
3import {
4 makeDeleteRequest,
5 makeGetRequest,
6 makePostBodyRequest,
7 makePutBodyRequest,
8 makeUploadRequest,
9 unwrapBody,
10 unwrapText
11} from '../requests/requests'
254d3579 12import { PeerTubeServer } from '../server/server'
a6a79eae 13
e8bd7ce7 14export interface OverrideCommandOptions {
a6a79eae
C
15 token?: string
16 expectedStatus?: number
17}
18
a1637fa1 19interface InternalCommonCommandOptions extends OverrideCommandOptions {
57f879a5
C
20 // Default to server.url
21 url?: string
22
e8bd7ce7 23 path: string
a1637fa1
C
24 // If we automatically send the server token if the token is not provided
25 implicitToken: boolean
e8bd7ce7 26 defaultExpectedStatus: number
e8bd7ce7 27
c0e8b12e 28 // Common optional request parameters
c1bc8ee4
C
29 contentType?: string
30 accept?: string
ae2abfd3 31 redirects?: number
57f879a5 32 range?: string
41d1d075 33 host?: string
c0e8b12e
C
34 headers?: { [ name: string ]: string }
35 requestType?: string
d102de1b 36 responseType?: string
c0e8b12e
C
37 xForwardedFor?: string
38}
39
40interface InternalGetCommandOptions extends InternalCommonCommandOptions {
41 query?: { [ id: string ]: any }
c1bc8ee4
C
42}
43
790c2837
C
44interface InternalDeleteCommandOptions extends InternalCommonCommandOptions {
45 query?: { [ id: string ]: any }
46 rawQuery?: string
47}
48
a6a79eae
C
49abstract class AbstractCommand {
50
a6a79eae 51 constructor (
254d3579 52 protected server: PeerTubeServer
a6a79eae
C
53 ) {
54
55 }
56
a1637fa1 57 protected getRequestBody <T> (options: InternalGetCommandOptions) {
c1bc8ee4
C
58 return unwrapBody<T>(this.getRequest(options))
59 }
60
a1637fa1 61 protected getRequestText (options: InternalGetCommandOptions) {
c1bc8ee4 62 return unwrapText(this.getRequest(options))
e8bd7ce7
C
63 }
64
57f879a5
C
65 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
66 const { url, range } = options
67 const { host, protocol, pathname } = new URL(url)
68
69 return this.getRequest({
70 ...options,
71
72 token: this.buildCommonRequestToken(options),
c0e8b12e 73 defaultExpectedStatus: this.buildExpectedStatus(options),
57f879a5
C
74
75 url: `${protocol}//${host}`,
76 path: pathname,
77 range
78 })
79 }
80
a1637fa1 81 protected getRequest (options: InternalGetCommandOptions) {
c0e8b12e 82 const { query } = options
ae2abfd3
C
83
84 return makeGetRequest({
85 ...this.buildCommonRequestOptions(options),
86
c0e8b12e 87 query
ae2abfd3
C
88 })
89 }
90
790c2837
C
91 protected deleteRequest (options: InternalDeleteCommandOptions) {
92 const { query, rawQuery } = options
93
94 return makeDeleteRequest({
95 ...this.buildCommonRequestOptions(options),
96
97 query,
98 rawQuery
99 })
0c1a77e9
C
100 }
101
a1637fa1 102 protected putBodyRequest (options: InternalCommonCommandOptions & {
e8bd7ce7
C
103 fields?: { [ fieldName: string ]: any }
104 }) {
105 const { fields } = options
106
107 return makePutBodyRequest({
108 ...this.buildCommonRequestOptions(options),
109
110 fields
111 })
112 }
113
a1637fa1 114 protected postBodyRequest (options: InternalCommonCommandOptions & {
a6a79eae
C
115 fields?: { [ fieldName: string ]: any }
116 }) {
c0e8b12e 117 const { fields } = options
a6a79eae
C
118
119 return makePostBodyRequest({
e8bd7ce7
C
120 ...this.buildCommonRequestOptions(options),
121
c0e8b12e 122 fields
e8bd7ce7
C
123 })
124 }
125
a1637fa1 126 protected postUploadRequest (options: InternalCommonCommandOptions & {
4f219914 127 fields?: { [ fieldName: string ]: any }
d23dd9fb 128 attaches?: { [ fieldName: string ]: any }
4f219914 129 }) {
c0e8b12e 130 const { fields, attaches } = options
4f219914
C
131
132 return makeUploadRequest({
133 ...this.buildCommonRequestOptions(options),
134
135 method: 'POST',
136 fields,
c0e8b12e 137 attaches
4f219914
C
138 })
139 }
140
a1637fa1 141 protected putUploadRequest (options: InternalCommonCommandOptions & {
4f219914 142 fields?: { [ fieldName: string ]: any }
d23dd9fb 143 attaches?: { [ fieldName: string ]: any }
4f219914 144 }) {
c0e8b12e 145 const { fields, attaches } = options
4f219914
C
146
147 return makeUploadRequest({
148 ...this.buildCommonRequestOptions(options),
149
150 method: 'PUT',
151 fields,
152 attaches
153 })
154 }
155
a5461888
C
156 protected updateImageRequest (options: InternalCommonCommandOptions & {
157 fixture: string
158 fieldname: string
159 }) {
c0e8b12e
C
160 const filePath = isAbsolute(options.fixture)
161 ? options.fixture
162 : join(root(), 'server', 'tests', 'fixtures', options.fixture)
a5461888
C
163
164 return this.postUploadRequest({
165 ...options,
166
167 fields: {},
168 attaches: { [options.fieldname]: filePath }
169 })
170 }
171
d23dd9fb 172 protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
d102de1b 173 const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor, responseType } = options
57f879a5
C
174
175 return {
dd0ebb71 176 url: url ?? this.server.url,
57f879a5
C
177 path,
178
179 token: this.buildCommonRequestToken(options),
c0e8b12e
C
180 expectedStatus: this.buildExpectedStatus(options),
181
182 redirects,
183 contentType,
184 range,
185 host,
186 accept,
187 headers,
188 type: requestType,
d102de1b 189 responseType,
c0e8b12e 190 xForwardedFor
57f879a5
C
191 }
192 }
193
d23dd9fb 194 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
57f879a5 195 const { token } = options
e8bd7ce7 196
a1637fa1
C
197 const fallbackToken = options.implicitToken
198 ? this.server.accessToken
199 : undefined
200
57f879a5
C
201 return token !== undefined ? token : fallbackToken
202 }
23a3a882 203
c0e8b12e 204 protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
57f879a5 205 const { expectedStatus, defaultExpectedStatus } = options
23a3a882 206
d23dd9fb 207 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
a6a79eae
C
208 }
209}
210
211export {
212 AbstractCommand
213}