aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/utils/requests
diff options
context:
space:
mode:
Diffstat (limited to 'shared/utils/requests')
-rw-r--r--shared/utils/requests/activitypub.ts43
-rw-r--r--shared/utils/requests/check-api-params.ts40
-rw-r--r--shared/utils/requests/requests.ts184
3 files changed, 0 insertions, 267 deletions
diff --git a/shared/utils/requests/activitypub.ts b/shared/utils/requests/activitypub.ts
deleted file mode 100644
index 4762a8665..000000000
--- a/shared/utils/requests/activitypub.ts
+++ /dev/null
@@ -1,43 +0,0 @@
1import { doRequest } from '../../../server/helpers/requests'
2import { HTTP_SIGNATURE } from '../../../server/initializers/constants'
3import { buildGlobalHeaders } from '../../../server/lib/job-queue/handlers/utils/activitypub-http-utils'
4import { activityPubContextify } from '../../../server/helpers/activitypub'
5
6function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) {
7 const options = {
8 method: 'POST',
9 uri: url,
10 json: body,
11 httpSignature,
12 headers
13 }
14
15 return doRequest(options)
16}
17
18async function makeFollowRequest (to: { url: string }, by: { url: string, privateKey }) {
19 const follow = {
20 type: 'Follow',
21 id: by.url + '/toto',
22 actor: by.url,
23 object: to.url
24 }
25
26 const body = activityPubContextify(follow)
27
28 const httpSignature = {
29 algorithm: HTTP_SIGNATURE.ALGORITHM,
30 authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
31 keyId: by.url,
32 key: by.privateKey,
33 headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
34 }
35 const headers = buildGlobalHeaders(body)
36
37 return makePOSTAPRequest(to.url, body, httpSignature, headers)
38}
39
40export {
41 makePOSTAPRequest,
42 makeFollowRequest
43}
diff --git a/shared/utils/requests/check-api-params.ts b/shared/utils/requests/check-api-params.ts
deleted file mode 100644
index a2a549682..000000000
--- a/shared/utils/requests/check-api-params.ts
+++ /dev/null
@@ -1,40 +0,0 @@
1import { makeGetRequest } from './requests'
2import { immutableAssign } from '../miscs/miscs'
3
4function checkBadStartPagination (url: string, path: string, token?: string, query = {}) {
5 return makeGetRequest({
6 url,
7 path,
8 token,
9 query: immutableAssign(query, { start: 'hello' }),
10 statusCodeExpected: 400
11 })
12}
13
14function checkBadCountPagination (url: string, path: string, token?: string, query = {}) {
15 return makeGetRequest({
16 url,
17 path,
18 token,
19 query: immutableAssign(query, { count: 'hello' }),
20 statusCodeExpected: 400
21 })
22}
23
24function checkBadSortPagination (url: string, path: string, token?: string, query = {}) {
25 return makeGetRequest({
26 url,
27 path,
28 token,
29 query: immutableAssign(query, { sort: 'hello' }),
30 statusCodeExpected: 400
31 })
32}
33
34// ---------------------------------------------------------------------------
35
36export {
37 checkBadStartPagination,
38 checkBadCountPagination,
39 checkBadSortPagination
40}
diff --git a/shared/utils/requests/requests.ts b/shared/utils/requests/requests.ts
deleted file mode 100644
index 3532fb429..000000000
--- a/shared/utils/requests/requests.ts
+++ /dev/null
@@ -1,184 +0,0 @@
1import * as request from 'supertest'
2import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
3import { isAbsolute, join } from 'path'
4import { parse } from 'url'
5
6function get4KFileUrl () {
7 return 'https://download.cpy.re/peertube/4k_file.txt'
8}
9
10function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
11 const { host, protocol, pathname } = parse(url)
12
13 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
14}
15
16function makeGetRequest (options: {
17 url: string,
18 path?: string,
19 query?: any,
20 token?: string,
21 statusCodeExpected?: number,
22 contentType?: string,
23 range?: string
24}) {
25 if (!options.statusCodeExpected) options.statusCodeExpected = 400
26 if (options.contentType === undefined) options.contentType = 'application/json'
27
28 const req = request(options.url).get(options.path)
29
30 if (options.contentType) req.set('Accept', options.contentType)
31 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
32 if (options.query) req.query(options.query)
33 if (options.range) req.set('Range', options.range)
34
35 return req.expect(options.statusCodeExpected)
36}
37
38function makeDeleteRequest (options: {
39 url: string,
40 path: string,
41 token?: string,
42 statusCodeExpected?: number
43}) {
44 if (!options.statusCodeExpected) options.statusCodeExpected = 400
45
46 const req = request(options.url)
47 .delete(options.path)
48 .set('Accept', 'application/json')
49
50 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
51
52 return req.expect(options.statusCodeExpected)
53}
54
55function makeUploadRequest (options: {
56 url: string,
57 method?: 'POST' | 'PUT',
58 path: string,
59 token?: string,
60 fields: { [ fieldName: string ]: any },
61 attaches: { [ attachName: string ]: any | any[] },
62 statusCodeExpected?: number
63}) {
64 if (!options.statusCodeExpected) options.statusCodeExpected = 400
65
66 let req: request.Test
67 if (options.method === 'PUT') {
68 req = request(options.url).put(options.path)
69 } else {
70 req = request(options.url).post(options.path)
71 }
72
73 req.set('Accept', 'application/json')
74
75 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
76
77 Object.keys(options.fields).forEach(field => {
78 const value = options.fields[field]
79
80 if (value === undefined) return
81
82 if (Array.isArray(value)) {
83 for (let i = 0; i < value.length; i++) {
84 req.field(field + '[' + i + ']', value[i])
85 }
86 } else {
87 req.field(field, value)
88 }
89 })
90
91 Object.keys(options.attaches).forEach(attach => {
92 const value = options.attaches[attach]
93 if (Array.isArray(value)) {
94 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
95 } else {
96 req.attach(attach, buildAbsoluteFixturePath(value))
97 }
98 })
99
100 return req.expect(options.statusCodeExpected)
101}
102
103function makePostBodyRequest (options: {
104 url: string,
105 path: string,
106 token?: string,
107 fields?: { [ fieldName: string ]: any },
108 statusCodeExpected?: number
109}) {
110 if (!options.fields) options.fields = {}
111 if (!options.statusCodeExpected) options.statusCodeExpected = 400
112
113 const req = request(options.url)
114 .post(options.path)
115 .set('Accept', 'application/json')
116
117 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
118
119 return req.send(options.fields)
120 .expect(options.statusCodeExpected)
121}
122
123function makePutBodyRequest (options: {
124 url: string,
125 path: string,
126 token?: string,
127 fields: { [ fieldName: string ]: any },
128 statusCodeExpected?: number
129}) {
130 if (!options.statusCodeExpected) options.statusCodeExpected = 400
131
132 const req = request(options.url)
133 .put(options.path)
134 .set('Accept', 'application/json')
135
136 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
137
138 return req.send(options.fields)
139 .expect(options.statusCodeExpected)
140}
141
142function makeHTMLRequest (url: string, path: string) {
143 return request(url)
144 .get(path)
145 .set('Accept', 'text/html')
146 .expect(200)
147}
148
149function updateAvatarRequest (options: {
150 url: string,
151 path: string,
152 accessToken: string,
153 fixture: string
154}) {
155 let filePath = ''
156 if (isAbsolute(options.fixture)) {
157 filePath = options.fixture
158 } else {
159 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
160 }
161
162 return makeUploadRequest({
163 url: options.url,
164 path: options.path,
165 token: options.accessToken,
166 fields: {},
167 attaches: { avatarfile: filePath },
168 statusCodeExpected: 200
169 })
170}
171
172// ---------------------------------------------------------------------------
173
174export {
175 get4KFileUrl,
176 makeHTMLRequest,
177 makeGetRequest,
178 makeUploadRequest,
179 makePostBodyRequest,
180 makePutBodyRequest,
181 makeDeleteRequest,
182 makeRawRequest,
183 updateAvatarRequest
184}