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