From c1bc8ee4783d6ce3102524e6c2a02b2f0f6aab6d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Jul 2021 10:21:35 +0200 Subject: Introduce feed command --- shared/extra-utils/feeds/feeds.ts | 55 ++++++++++++++++----------- shared/extra-utils/feeds/index.ts | 1 + shared/extra-utils/requests/requests.ts | 9 ++++- shared/extra-utils/server/servers.ts | 3 ++ shared/extra-utils/shared/abstract-command.ts | 28 ++++++++++++-- 5 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 shared/extra-utils/feeds/index.ts (limited to 'shared') 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 @@ -import * as request from 'supertest' + import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +import { AbstractCommand, OverrideCommandOptions } from '../shared' type FeedType = 'videos' | 'video-comments' | 'subscriptions' -function getXMLfeed (url: string, feed: FeedType, format?: string) { - const path = '/feeds/' + feed + '.xml' +export class FeedCommand extends AbstractCommand { - return request(url) - .get(path) - .query((format) ? { format: format } : {}) - .set('Accept', 'application/xml') - .expect(HttpStatusCode.OK_200) - .expect('Content-Type', /xml/) -} + getXML (options: OverrideCommandOptions & { + feed: FeedType + format?: string + }) { + const { feed, format } = options + const path = '/feeds/' + feed + '.xml' -function getJSONfeed (url: string, feed: FeedType, query: any = {}, statusCodeExpected = HttpStatusCode.OK_200) { - const path = '/feeds/' + feed + '.json' + return this.getRequestText({ + ...options, - return request(url) - .get(path) - .query(query) - .set('Accept', 'application/json') - .expect(statusCodeExpected) - .expect('Content-Type', /json/) -} + path, + query: format ? { format } : undefined, + accept: 'application/xml', + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + getJSON (options: OverrideCommandOptions & { + feed: FeedType + query?: { [ id: string ]: any } + }) { + const { feed, query } = options + const path = '/feeds/' + feed + '.json' -// --------------------------------------------------------------------------- + return this.getRequestText({ + ...options, -export { - getXMLfeed, - getJSONfeed + path, + query, + accept: 'application/json', + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } } 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) { return decode(path.split('?')[1]) } -function unwrap (test: request.Test): Promise { +function unwrapBody (test: request.Test): Promise { return test.then(res => res.body) } +function unwrapText (test: request.Test): Promise { + return test.then(res => res.text) +} + // --------------------------------------------------------------------------- export { @@ -198,6 +202,7 @@ export { makePutBodyRequest, makeDeleteRequest, makeRawRequest, - unwrap, + unwrapBody, + unwrapText, updateImageRequest } 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' import { BulkCommand } from '../bulk' import { CLICommand } from '../cli' import { CustomPagesCommand } from '../custom-pages' +import { FeedCommand } from '../feeds' import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' import { makeGetRequest } from '../requests/requests' @@ -67,6 +68,7 @@ interface ServerInfo { bulkCommand?: BulkCommand cliCommand?: CLICommand customPageCommand?: CustomPagesCommand + feedCommand?: FeedCommand } function parallelTests () { @@ -275,6 +277,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] server.bulkCommand = new BulkCommand(server) server.cliCommand = new CLICommand(server) server.customPageCommand = new CustomPagesCommand(server) + server.feedCommand = new FeedCommand(server) res(server) }) 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 @@ import { HttpStatusCode } from '@shared/core-utils' -import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests' +import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap, unwrapBody, unwrapText } from '../requests/requests' import { ServerInfo } from '../server/servers' export interface OverrideCommandOptions { @@ -12,6 +12,12 @@ interface CommonCommandOptions extends OverrideCommandOptions { defaultExpectedStatus: number } +interface GetCommandOptions extends CommonCommandOptions { + query?: { [ id: string ]: string } + contentType?: string + accept?: string +} + abstract class AbstractCommand { private expectedStatus: HttpStatusCode @@ -30,8 +36,12 @@ abstract class AbstractCommand { this.expectedStatus = status } - protected getRequestBody (options: CommonCommandOptions) { - return unwrap(makeGetRequest(this.buildCommonRequestOptions(options))) + protected getRequestBody (options: GetCommandOptions) { + return unwrapBody(this.getRequest(options)) + } + + protected getRequestText (options: GetCommandOptions) { + return unwrapText(this.getRequest(options)) } protected putBodyRequest (options: CommonCommandOptions & { @@ -68,6 +78,18 @@ abstract class AbstractCommand { statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus } } + + private getRequest (options: GetCommandOptions) { + const { query, contentType, accept } = options + + return makeGetRequest({ + ...this.buildCommonRequestOptions(options), + + query, + contentType, + accept + }) + } } export { -- cgit v1.2.3