From e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Jul 2021 09:55:05 +0200 Subject: Introduce CustomPage command --- shared/extra-utils/requests/requests.ts | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 38e24d897..eb59ca600 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -182,6 +182,10 @@ function decodeQueryString (path: string) { return decode(path.split('?')[1]) } +function unwrap (test: request.Test): Promise { + return test.then(res => res.body) +} + // --------------------------------------------------------------------------- export { @@ -194,5 +198,6 @@ export { makePutBodyRequest, makeDeleteRequest, makeRawRequest, + unwrap, updateImageRequest } -- cgit v1.2.3 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/requests/requests.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'shared/extra-utils/requests') 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 } -- cgit v1.2.3 From 2d1ad5b96063d1e430ca99128a15e2e56cb614e0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Jul 2021 15:33:39 +0200 Subject: Move AP request in requests file --- shared/extra-utils/requests/requests.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 8c26a3699..3fbaa31d6 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -1,11 +1,11 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ -import * as request from 'supertest' -import { buildAbsoluteFixturePath, root } from '../miscs/miscs' import { isAbsolute, join } from 'path' -import { URL } from 'url' import { decode } from 'querystring' +import * as request from 'supertest' +import { URL } from 'url' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +import { buildAbsoluteFixturePath, root } from '../miscs/miscs' function get4KFileUrl () { return 'https://download.cpy.re/peertube/4k_file.txt' @@ -154,6 +154,15 @@ function makeHTMLRequest (url: string, path: string) { .expect(HttpStatusCode.OK_200) } +function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) { + return makeGetRequest({ + url, + path, + statusCodeExpected: expectedStatus, + accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8' + }) +} + function updateImageRequest (options: { url: string path: string @@ -202,6 +211,7 @@ export { makePutBodyRequest, makeDeleteRequest, makeRawRequest, + makeActivityPubGetRequest, unwrapBody, unwrapText, updateImageRequest -- cgit v1.2.3 From 4f2199144e428c16460750305f737b890c1ac322 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 8 Jul 2021 10:18:40 +0200 Subject: Introduce live command --- shared/extra-utils/requests/index.ts | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 shared/extra-utils/requests/index.ts (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/index.ts b/shared/extra-utils/requests/index.ts new file mode 100644 index 000000000..501163f92 --- /dev/null +++ b/shared/extra-utils/requests/index.ts @@ -0,0 +1,3 @@ +// Don't include activitypub that import stuff from server +export * from './check-api-params' +export * from './requests' -- cgit v1.2.3 From 6c5065a011b099618681a37bd77eaa7bd3db752e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 13 Jul 2021 09:43:59 +0200 Subject: Introduce server commands --- shared/extra-utils/requests/check-api-params.ts | 11 +++++------ shared/extra-utils/requests/requests.ts | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/check-api-params.ts b/shared/extra-utils/requests/check-api-params.ts index 7f5ff775c..7df63b004 100644 --- a/shared/extra-utils/requests/check-api-params.ts +++ b/shared/extra-utils/requests/check-api-params.ts @@ -1,13 +1,12 @@ +import { HttpStatusCode } from '@shared/core-utils' import { makeGetRequest } from './requests' -import { immutableAssign } from '../miscs/miscs' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' function checkBadStartPagination (url: string, path: string, token?: string, query = {}) { return makeGetRequest({ url, path, token, - query: immutableAssign(query, { start: 'hello' }), + query: { ...query, start: 'hello' }, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) } @@ -17,7 +16,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin url, path, token, - query: immutableAssign(query, { count: 'hello' }), + query: { ...query, count: 'hello' }, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) @@ -25,7 +24,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin url, path, token, - query: immutableAssign(query, { count: 2000 }), + query: { ...query, count: 2000 }, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) } @@ -35,7 +34,7 @@ function checkBadSortPagination (url: string, path: string, token?: string, quer url, path, token, - query: immutableAssign(query, { sort: 'hello' }), + query: { ...query, sort: 'hello' }, statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 }) } diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 3fbaa31d6..f9d112aca 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -4,8 +4,8 @@ import { isAbsolute, join } from 'path' import { decode } from 'querystring' import * as request from 'supertest' import { URL } from 'url' -import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' -import { buildAbsoluteFixturePath, root } from '../miscs/miscs' +import { HttpStatusCode } from '@shared/core-utils' +import { buildAbsoluteFixturePath, root } from '../miscs/tests' function get4KFileUrl () { return 'https://download.cpy.re/peertube/4k_file.txt' -- cgit v1.2.3 From 41d1d075011174e73dccb74006181a92a618d7b4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 13 Jul 2021 11:05:15 +0200 Subject: Introduce login command --- shared/extra-utils/requests/requests.ts | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index f9d112aca..c5ee63e05 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -27,6 +27,7 @@ function makeGetRequest (options: { range?: string redirects?: number accept?: string + host?: string }) { if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 if (options.contentType === undefined) options.contentType = 'application/json' @@ -38,6 +39,7 @@ function makeGetRequest (options: { if (options.query) req.query(options.query) if (options.range) req.set('Range', options.range) if (options.accept) req.set('Accept', options.accept) + if (options.host) req.set('Host', options.host) if (options.redirects) req.redirects(options.redirects) return req.expect(options.statusCodeExpected) @@ -113,6 +115,7 @@ function makePostBodyRequest (options: { path: string token?: string fields?: { [ fieldName: string ]: any } + type?: string statusCodeExpected?: HttpStatusCode }) { if (!options.fields) options.fields = {} @@ -123,6 +126,7 @@ function makePostBodyRequest (options: { .set('Accept', 'application/json') if (options.token) req.set('Authorization', 'Bearer ' + options.token) + if (options.type) req.type(options.type) return req.send(options.fields) .expect(options.statusCodeExpected) -- cgit v1.2.3 From d23dd9fbfc4d26026352c10f81d2795ceaf2908a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 15 Jul 2021 10:02:54 +0200 Subject: Introduce videos command --- shared/extra-utils/requests/requests.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index c5ee63e05..3f1ac6650 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -67,11 +67,17 @@ function makeUploadRequest (options: { method?: 'POST' | 'PUT' path: string token?: string + fields: { [ fieldName: string ]: any } attaches?: { [ attachName: string ]: any | any[] } + + headers?: { [ name: string ]: string } + statusCodeExpected?: HttpStatusCode }) { - if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 + if (options.statusCodeExpected === undefined) { + options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 + } let req: request.Test if (options.method === 'PUT') { @@ -84,6 +90,10 @@ function makeUploadRequest (options: { if (options.token) req.set('Authorization', 'Bearer ' + options.token) + Object.keys(options.headers || {}).forEach(name => { + req.set(name, options.headers[name]) + }) + Object.keys(options.fields).forEach(field => { const value = options.fields[field] @@ -107,7 +117,11 @@ function makeUploadRequest (options: { } }) - return req.expect(options.statusCodeExpected) + if (options.statusCodeExpected) { + req.expect(options.statusCodeExpected) + } + + return req } function makePostBodyRequest (options: { @@ -115,7 +129,9 @@ function makePostBodyRequest (options: { path: string token?: string fields?: { [ fieldName: string ]: any } + headers?: { [ name: string ]: string } type?: string + xForwardedFor?: string statusCodeExpected?: HttpStatusCode }) { if (!options.fields) options.fields = {} @@ -126,8 +142,13 @@ function makePostBodyRequest (options: { .set('Accept', 'application/json') if (options.token) req.set('Authorization', 'Bearer ' + options.token) + if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor) if (options.type) req.type(options.type) + Object.keys(options.headers || {}).forEach(name => { + req.set(name, options.headers[name]) + }) + return req.send(options.fields) .expect(options.statusCodeExpected) } -- cgit v1.2.3 From 59bbcced37005dd511daca9bd58ae2998cb931b1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 10:19:16 +0200 Subject: Centralize test URLs --- shared/extra-utils/requests/requests.ts | 5 ----- 1 file changed, 5 deletions(-) (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 3f1ac6650..60c9b938b 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -7,10 +7,6 @@ import { URL } from 'url' import { HttpStatusCode } from '@shared/core-utils' import { buildAbsoluteFixturePath, root } from '../miscs/tests' -function get4KFileUrl () { - return 'https://download.cpy.re/peertube/4k_file.txt' -} - function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) { const { host, protocol, pathname } = new URL(url) @@ -227,7 +223,6 @@ function unwrapText (test: request.Test): Promise { // --------------------------------------------------------------------------- export { - get4KFileUrl, makeHTMLRequest, makeGetRequest, decodeQueryString, -- cgit v1.2.3 From c0e8b12e7fd554ba4d2ceb0c4900804c6a4c63ea Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 10:42:24 +0200 Subject: Refactor requests --- shared/extra-utils/requests/check-api-params.ts | 10 +- shared/extra-utils/requests/requests.ts | 256 +++++++++--------------- 2 files changed, 105 insertions(+), 161 deletions(-) (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/check-api-params.ts b/shared/extra-utils/requests/check-api-params.ts index 7df63b004..26ba1e913 100644 --- a/shared/extra-utils/requests/check-api-params.ts +++ b/shared/extra-utils/requests/check-api-params.ts @@ -1,4 +1,4 @@ -import { HttpStatusCode } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' import { makeGetRequest } from './requests' function checkBadStartPagination (url: string, path: string, token?: string, query = {}) { @@ -7,7 +7,7 @@ function checkBadStartPagination (url: string, path: string, token?: string, que path, token, query: { ...query, start: 'hello' }, - statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 + expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) } @@ -17,7 +17,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin path, token, query: { ...query, count: 'hello' }, - statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 + expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) await makeGetRequest({ @@ -25,7 +25,7 @@ async function checkBadCountPagination (url: string, path: string, token?: strin path, token, query: { ...query, count: 2000 }, - statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 + expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) } @@ -35,7 +35,7 @@ function checkBadSortPagination (url: string, path: string, token?: string, quer path, token, query: { ...query, sort: 'hello' }, - statusCodeExpected: HttpStatusCode.BAD_REQUEST_400 + expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) } diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts index 60c9b938b..70f790222 100644 --- a/shared/extra-utils/requests/requests.ts +++ b/shared/extra-utils/requests/requests.ts @@ -1,111 +1,82 @@ -/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */ +/* eslint-disable @typescript-eslint/no-floating-promises */ -import { isAbsolute, join } from 'path' import { decode } from 'querystring' import * as request from 'supertest' import { URL } from 'url' -import { HttpStatusCode } from '@shared/core-utils' -import { buildAbsoluteFixturePath, root } from '../miscs/tests' +import { HttpStatusCode } from '@shared/models' +import { buildAbsoluteFixturePath } from '../miscs/tests' -function makeRawRequest (url: string, statusCodeExpected?: HttpStatusCode, range?: string) { - const { host, protocol, pathname } = new URL(url) - - return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range }) -} - -function makeGetRequest (options: { +export type CommonRequestParams = { url: string path?: string - query?: any - token?: string - statusCodeExpected?: HttpStatusCode contentType?: string range?: string redirects?: number accept?: string host?: string -}) { - if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 - if (options.contentType === undefined) options.contentType = 'application/json' - - const req = request(options.url).get(options.path) + token?: string + headers?: { [ name: string ]: string } + type?: string + xForwardedFor?: string + expectedStatus?: HttpStatusCode +} - if (options.contentType) req.set('Accept', options.contentType) - if (options.token) req.set('Authorization', 'Bearer ' + options.token) - if (options.query) req.query(options.query) - if (options.range) req.set('Range', options.range) - if (options.accept) req.set('Accept', options.accept) - if (options.host) req.set('Host', options.host) - if (options.redirects) req.redirects(options.redirects) +function makeRawRequest (url: string, expectedStatus?: HttpStatusCode, range?: string) { + const { host, protocol, pathname } = new URL(url) - return req.expect(options.statusCodeExpected) + return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, expectedStatus, range }) } -function makeDeleteRequest (options: { - url: string - path: string - token?: string - statusCodeExpected?: HttpStatusCode +function makeGetRequest (options: CommonRequestParams & { + query?: any }) { - if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 + const req = request(options.url).get(options.path) + .query(options.query) - const req = request(options.url) - .delete(options.path) - .set('Accept', 'application/json') + return buildRequest(req, { contentType: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) +} - if (options.token) req.set('Authorization', 'Bearer ' + options.token) +function makeHTMLRequest (url: string, path: string) { + return makeGetRequest({ + url, + path, + accept: 'text/html', + expectedStatus: HttpStatusCode.OK_200 + }) +} - return req.expect(options.statusCodeExpected) +function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) { + return makeGetRequest({ + url, + path, + expectedStatus: expectedStatus, + accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8' + }) } -function makeUploadRequest (options: { - url: string +function makeDeleteRequest (options: CommonRequestParams) { + const req = request(options.url).delete(options.path) + + return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) +} + +function makeUploadRequest (options: CommonRequestParams & { method?: 'POST' | 'PUT' - path: string - token?: string fields: { [ fieldName: string ]: any } attaches?: { [ attachName: string ]: any | any[] } - - headers?: { [ name: string ]: string } - - statusCodeExpected?: HttpStatusCode }) { - if (options.statusCodeExpected === undefined) { - options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 - } + let req = options.method === 'PUT' + ? request(options.url).put(options.path) + : request(options.url).post(options.path) - let req: request.Test - if (options.method === 'PUT') { - req = request(options.url).put(options.path) - } else { - req = request(options.url).post(options.path) - } + req = buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) - req.set('Accept', 'application/json') - - if (options.token) req.set('Authorization', 'Bearer ' + options.token) - - Object.keys(options.headers || {}).forEach(name => { - req.set(name, options.headers[name]) - }) - - Object.keys(options.fields).forEach(field => { - const value = options.fields[field] - - if (value === undefined) return - - if (Array.isArray(value)) { - for (let i = 0; i < value.length; i++) { - req.field(field + '[' + i + ']', value[i]) - } - } else { - req.field(field, value) - } - }) + buildFields(req, options.fields) Object.keys(options.attaches || {}).forEach(attach => { const value = options.attaches[attach] + if (Array.isArray(value)) { req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) } else { @@ -113,40 +84,16 @@ function makeUploadRequest (options: { } }) - if (options.statusCodeExpected) { - req.expect(options.statusCodeExpected) - } - return req } -function makePostBodyRequest (options: { - url: string - path: string - token?: string +function makePostBodyRequest (options: CommonRequestParams & { fields?: { [ fieldName: string ]: any } - headers?: { [ name: string ]: string } - type?: string - xForwardedFor?: string - statusCodeExpected?: HttpStatusCode }) { - if (!options.fields) options.fields = {} - if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 - - const req = request(options.url) - .post(options.path) - .set('Accept', 'application/json') + const req = request(options.url).post(options.path) + .send(options.fields) - if (options.token) req.set('Authorization', 'Bearer ' + options.token) - if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor) - if (options.type) req.type(options.type) - - Object.keys(options.headers || {}).forEach(name => { - req.set(name, options.headers[name]) - }) - - return req.send(options.fields) - .expect(options.statusCodeExpected) + return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) } function makePutBodyRequest (options: { @@ -154,58 +101,12 @@ function makePutBodyRequest (options: { path: string token?: string fields: { [ fieldName: string ]: any } - statusCodeExpected?: HttpStatusCode -}) { - if (!options.statusCodeExpected) options.statusCodeExpected = HttpStatusCode.BAD_REQUEST_400 - - const req = request(options.url) - .put(options.path) - .set('Accept', 'application/json') - - if (options.token) req.set('Authorization', 'Bearer ' + options.token) - - return req.send(options.fields) - .expect(options.statusCodeExpected) -} - -function makeHTMLRequest (url: string, path: string) { - return request(url) - .get(path) - .set('Accept', 'text/html') - .expect(HttpStatusCode.OK_200) -} - -function makeActivityPubGetRequest (url: string, path: string, expectedStatus = HttpStatusCode.OK_200) { - return makeGetRequest({ - url, - path, - statusCodeExpected: expectedStatus, - accept: 'application/activity+json,text/html;q=0.9,\\*/\\*;q=0.8' - }) -} - -function updateImageRequest (options: { - url: string - path: string - accessToken: string - fixture: string - fieldname: string + expectedStatus?: HttpStatusCode }) { - let filePath = '' - if (isAbsolute(options.fixture)) { - filePath = options.fixture - } else { - filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture) - } + const req = request(options.url).put(options.path) + .send(options.fields) - return makeUploadRequest({ - url: options.url, - path: options.path, - token: options.accessToken, - fields: {}, - attaches: { [options.fieldname]: filePath }, - statusCodeExpected: HttpStatusCode.OK_200 - }) + return buildRequest(req, { accept: 'application/json', expectedStatus: HttpStatusCode.BAD_REQUEST_400, ...options }) } function decodeQueryString (path: string) { @@ -233,6 +134,49 @@ export { makeRawRequest, makeActivityPubGetRequest, unwrapBody, - unwrapText, - updateImageRequest + unwrapText +} + +// --------------------------------------------------------------------------- + +function buildRequest (req: request.Test, options: CommonRequestParams) { + if (options.contentType) req.set('Accept', options.contentType) + if (options.token) req.set('Authorization', 'Bearer ' + options.token) + if (options.range) req.set('Range', options.range) + if (options.accept) req.set('Accept', options.accept) + if (options.host) req.set('Host', options.host) + if (options.redirects) req.redirects(options.redirects) + if (options.expectedStatus) req.expect(options.expectedStatus) + if (options.xForwardedFor) req.set('X-Forwarded-For', options.xForwardedFor) + if (options.type) req.type(options.type) + + Object.keys(options.headers || {}).forEach(name => { + req.set(name, options.headers[name]) + }) + + return req +} + +function buildFields (req: request.Test, fields: { [ fieldName: string ]: any }, namespace?: string) { + if (!fields) return + + let formKey: string + + for (const key of Object.keys(fields)) { + if (namespace) formKey = `${namespace}[${key}]` + else formKey = key + + if (fields[key] === undefined) continue + + if (Array.isArray(fields[key]) && fields[key].length === 0) { + req.field(key, null) + continue + } + + if (fields[key] !== null && typeof fields[key] === 'object') { + buildFields(req, fields[key], formKey) + } else { + req.field(formKey, fields[key]) + } + } } -- cgit v1.2.3 From 4c7e60bc17ee5830399bac4aa273356903421b4c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 16 Jul 2021 14:27:30 +0200 Subject: Reorganize imports --- shared/extra-utils/requests/activitypub.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'shared/extra-utils/requests') diff --git a/shared/extra-utils/requests/activitypub.ts b/shared/extra-utils/requests/activitypub.ts index ecd8ce823..4ae878384 100644 --- a/shared/extra-utils/requests/activitypub.ts +++ b/shared/extra-utils/requests/activitypub.ts @@ -1,7 +1,7 @@ +import { activityPubContextify } from '../../../server/helpers/activitypub' import { doRequest } from '../../../server/helpers/requests' import { HTTP_SIGNATURE } from '../../../server/initializers/constants' import { buildGlobalHeaders } from '../../../server/lib/job-queue/handlers/utils/activitypub-http-utils' -import { activityPubContextify } from '../../../server/helpers/activitypub' function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) { const options = { -- cgit v1.2.3