diff options
Diffstat (limited to 'shared/extra-utils')
-rw-r--r-- | shared/extra-utils/feeds/feeds.ts | 55 | ||||
-rw-r--r-- | shared/extra-utils/feeds/index.ts | 1 | ||||
-rw-r--r-- | shared/extra-utils/requests/requests.ts | 9 | ||||
-rw-r--r-- | shared/extra-utils/server/servers.ts | 3 | ||||
-rw-r--r-- | shared/extra-utils/shared/abstract-command.ts | 28 |
5 files changed, 68 insertions, 28 deletions
diff --git a/shared/extra-utils/feeds/feeds.ts b/shared/extra-utils/feeds/feeds.ts index ce0a98c6d..012ce6cfe 100644 --- a/shared/extra-utils/feeds/feeds.ts +++ b/shared/extra-utils/feeds/feeds.ts | |||
@@ -1,33 +1,42 @@ | |||
1 | import * as request from 'supertest' | 1 | |
2 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | 2 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
3 | 4 | ||
4 | type FeedType = 'videos' | 'video-comments' | 'subscriptions' | 5 | type FeedType = 'videos' | 'video-comments' | 'subscriptions' |
5 | 6 | ||
6 | function getXMLfeed (url: string, feed: FeedType, format?: string) { | 7 | export class FeedCommand extends AbstractCommand { |
7 | const path = '/feeds/' + feed + '.xml' | ||
8 | 8 | ||
9 | return request(url) | 9 | getXML (options: OverrideCommandOptions & { |
10 | .get(path) | 10 | feed: FeedType |
11 | .query((format) ? { format: format } : {}) | 11 | format?: string |
12 | .set('Accept', 'application/xml') | 12 | }) { |
13 | .expect(HttpStatusCode.OK_200) | 13 | const { feed, format } = options |
14 | .expect('Content-Type', /xml/) | 14 | const path = '/feeds/' + feed + '.xml' |
15 | } | ||
16 | 15 | ||
17 | function getJSONfeed (url: string, feed: FeedType, query: any = {}, statusCodeExpected = HttpStatusCode.OK_200) { | 16 | return this.getRequestText({ |
18 | const path = '/feeds/' + feed + '.json' | 17 | ...options, |
19 | 18 | ||
20 | return request(url) | 19 | path, |
21 | .get(path) | 20 | query: format ? { format } : undefined, |
22 | .query(query) | 21 | accept: 'application/xml', |
23 | .set('Accept', 'application/json') | 22 | defaultExpectedStatus: HttpStatusCode.OK_200 |
24 | .expect(statusCodeExpected) | 23 | }) |
25 | .expect('Content-Type', /json/) | 24 | } |
26 | } | 25 | |
26 | getJSON (options: OverrideCommandOptions & { | ||
27 | feed: FeedType | ||
28 | query?: { [ id: string ]: any } | ||
29 | }) { | ||
30 | const { feed, query } = options | ||
31 | const path = '/feeds/' + feed + '.json' | ||
27 | 32 | ||
28 | // --------------------------------------------------------------------------- | 33 | return this.getRequestText({ |
34 | ...options, | ||
29 | 35 | ||
30 | export { | 36 | path, |
31 | getXMLfeed, | 37 | query, |
32 | getJSONfeed | 38 | accept: 'application/json', |
39 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
40 | }) | ||
41 | } | ||
33 | } | 42 | } |
diff --git a/shared/extra-utils/feeds/index.ts b/shared/extra-utils/feeds/index.ts new file mode 100644 index 000000000..4634cb163 --- /dev/null +++ b/shared/extra-utils/feeds/index.ts | |||
@@ -0,0 +1 @@ | |||
export * from './feeds' | |||
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index eb59ca600..8c26a3699 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts | |||
@@ -182,10 +182,14 @@ function decodeQueryString (path: string) { | |||
182 | return decode(path.split('?')[1]) | 182 | return decode(path.split('?')[1]) |
183 | } | 183 | } |
184 | 184 | ||
185 | function unwrap <T> (test: request.Test): Promise<T> { | 185 | function unwrapBody <T> (test: request.Test): Promise<T> { |
186 | return test.then(res => res.body) | 186 | return test.then(res => res.body) |
187 | } | 187 | } |
188 | 188 | ||
189 | function unwrapText (test: request.Test): Promise<string> { | ||
190 | return test.then(res => res.text) | ||
191 | } | ||
192 | |||
189 | // --------------------------------------------------------------------------- | 193 | // --------------------------------------------------------------------------- |
190 | 194 | ||
191 | export { | 195 | export { |
@@ -198,6 +202,7 @@ export { | |||
198 | makePutBodyRequest, | 202 | makePutBodyRequest, |
199 | makeDeleteRequest, | 203 | makeDeleteRequest, |
200 | makeRawRequest, | 204 | makeRawRequest, |
201 | unwrap, | 205 | unwrapBody, |
206 | unwrapText, | ||
202 | updateImageRequest | 207 | updateImageRequest |
203 | } | 208 | } |
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index e07926065..b64c9eec6 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts | |||
@@ -9,6 +9,7 @@ import { VideoChannel } from '../../models/videos' | |||
9 | import { BulkCommand } from '../bulk' | 9 | import { BulkCommand } from '../bulk' |
10 | import { CLICommand } from '../cli' | 10 | import { CLICommand } from '../cli' |
11 | import { CustomPagesCommand } from '../custom-pages' | 11 | import { CustomPagesCommand } from '../custom-pages' |
12 | import { FeedCommand } from '../feeds' | ||
12 | import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' | 13 | import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' |
13 | import { makeGetRequest } from '../requests/requests' | 14 | import { makeGetRequest } from '../requests/requests' |
14 | 15 | ||
@@ -67,6 +68,7 @@ interface ServerInfo { | |||
67 | bulkCommand?: BulkCommand | 68 | bulkCommand?: BulkCommand |
68 | cliCommand?: CLICommand | 69 | cliCommand?: CLICommand |
69 | customPageCommand?: CustomPagesCommand | 70 | customPageCommand?: CustomPagesCommand |
71 | feedCommand?: FeedCommand | ||
70 | } | 72 | } |
71 | 73 | ||
72 | function parallelTests () { | 74 | function parallelTests () { |
@@ -275,6 +277,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] | |||
275 | server.bulkCommand = new BulkCommand(server) | 277 | server.bulkCommand = new BulkCommand(server) |
276 | server.cliCommand = new CLICommand(server) | 278 | server.cliCommand = new CLICommand(server) |
277 | server.customPageCommand = new CustomPagesCommand(server) | 279 | server.customPageCommand = new CustomPagesCommand(server) |
280 | server.feedCommand = new FeedCommand(server) | ||
278 | 281 | ||
279 | res(server) | 282 | res(server) |
280 | }) | 283 | }) |
diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts index d8eba373d..53c644124 100644 --- a/shared/extra-utils/shared/abstract-command.ts +++ b/shared/extra-utils/shared/abstract-command.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import { HttpStatusCode } from '@shared/core-utils' | 1 | import { HttpStatusCode } from '@shared/core-utils' |
2 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests' | 2 | import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap, unwrapBody, unwrapText } from '../requests/requests' |
3 | import { ServerInfo } from '../server/servers' | 3 | import { ServerInfo } from '../server/servers' |
4 | 4 | ||
5 | export interface OverrideCommandOptions { | 5 | export interface OverrideCommandOptions { |
@@ -12,6 +12,12 @@ interface CommonCommandOptions extends OverrideCommandOptions { | |||
12 | defaultExpectedStatus: number | 12 | defaultExpectedStatus: number |
13 | } | 13 | } |
14 | 14 | ||
15 | interface GetCommandOptions extends CommonCommandOptions { | ||
16 | query?: { [ id: string ]: string } | ||
17 | contentType?: string | ||
18 | accept?: string | ||
19 | } | ||
20 | |||
15 | abstract class AbstractCommand { | 21 | abstract class AbstractCommand { |
16 | 22 | ||
17 | private expectedStatus: HttpStatusCode | 23 | private expectedStatus: HttpStatusCode |
@@ -30,8 +36,12 @@ abstract class AbstractCommand { | |||
30 | this.expectedStatus = status | 36 | this.expectedStatus = status |
31 | } | 37 | } |
32 | 38 | ||
33 | protected getRequestBody <T> (options: CommonCommandOptions) { | 39 | protected getRequestBody <T> (options: GetCommandOptions) { |
34 | return unwrap<T>(makeGetRequest(this.buildCommonRequestOptions(options))) | 40 | return unwrapBody<T>(this.getRequest(options)) |
41 | } | ||
42 | |||
43 | protected getRequestText (options: GetCommandOptions) { | ||
44 | return unwrapText(this.getRequest(options)) | ||
35 | } | 45 | } |
36 | 46 | ||
37 | protected putBodyRequest (options: CommonCommandOptions & { | 47 | protected putBodyRequest (options: CommonCommandOptions & { |
@@ -68,6 +78,18 @@ abstract class AbstractCommand { | |||
68 | statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus | 78 | statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus |
69 | } | 79 | } |
70 | } | 80 | } |
81 | |||
82 | private getRequest (options: GetCommandOptions) { | ||
83 | const { query, contentType, accept } = options | ||
84 | |||
85 | return makeGetRequest({ | ||
86 | ...this.buildCommonRequestOptions(options), | ||
87 | |||
88 | query, | ||
89 | contentType, | ||
90 | accept | ||
91 | }) | ||
92 | } | ||
71 | } | 93 | } |
72 | 94 | ||
73 | export { | 95 | export { |