diff options
author | Chocobozzz <me@florianbigard.com> | 2018-12-07 16:09:57 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-12-07 16:25:29 +0100 |
commit | 2a8c5d0af13f3ccb9a505e1fbc9d324b9d33ba1f (patch) | |
tree | 998a0af6923475c85eb8e97ca1d30afb42655263 /shared/utils/requests | |
parent | 4b49385892b25aea5aa9e605fbcb66074bcb49b0 (diff) | |
parent | 35adc403b7eed6d1309bbce901268a76dd01b6ef (diff) | |
download | PeerTube-2a8c5d0af13f3ccb9a505e1fbc9d324b9d33ba1f.tar.gz PeerTube-2a8c5d0af13f3ccb9a505e1fbc9d324b9d33ba1f.tar.zst PeerTube-2a8c5d0af13f3ccb9a505e1fbc9d324b9d33ba1f.zip |
Merge branch 'move-utils-to-shared' of https://github.com/buoyantair/PeerTube into buoyantair-move-utils-to-shared
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 | 168 |
3 files changed, 251 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..77e9f6164 --- /dev/null +++ b/shared/utils/requests/requests.ts | |||
@@ -0,0 +1,168 @@ | |||
1 | import * as request from 'supertest' | ||
2 | import { buildAbsoluteFixturePath, root } from '../miscs/miscs' | ||
3 | import { isAbsolute, join } from 'path' | ||
4 | |||
5 | function makeGetRequest (options: { | ||
6 | url: string, | ||
7 | path: string, | ||
8 | query?: any, | ||
9 | token?: string, | ||
10 | statusCodeExpected?: number, | ||
11 | contentType?: string | ||
12 | }) { | ||
13 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | ||
14 | if (options.contentType === undefined) options.contentType = 'application/json' | ||
15 | |||
16 | const req = request(options.url) | ||
17 | .get(options.path) | ||
18 | |||
19 | if (options.contentType) req.set('Accept', options.contentType) | ||
20 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | ||
21 | if (options.query) req.query(options.query) | ||
22 | |||
23 | return req.expect(options.statusCodeExpected) | ||
24 | } | ||
25 | |||
26 | function makeDeleteRequest (options: { | ||
27 | url: string, | ||
28 | path: string, | ||
29 | token?: string, | ||
30 | statusCodeExpected?: number | ||
31 | }) { | ||
32 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | ||
33 | |||
34 | const req = request(options.url) | ||
35 | .delete(options.path) | ||
36 | .set('Accept', 'application/json') | ||
37 | |||
38 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | ||
39 | |||
40 | return req.expect(options.statusCodeExpected) | ||
41 | } | ||
42 | |||
43 | function makeUploadRequest (options: { | ||
44 | url: string, | ||
45 | method?: 'POST' | 'PUT', | ||
46 | path: string, | ||
47 | token?: string, | ||
48 | fields: { [ fieldName: string ]: any }, | ||
49 | attaches: { [ attachName: string ]: any | any[] }, | ||
50 | statusCodeExpected?: number | ||
51 | }) { | ||
52 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | ||
53 | |||
54 | let req: request.Test | ||
55 | if (options.method === 'PUT') { | ||
56 | req = request(options.url).put(options.path) | ||
57 | } else { | ||
58 | req = request(options.url).post(options.path) | ||
59 | } | ||
60 | |||
61 | req.set('Accept', 'application/json') | ||
62 | |||
63 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | ||
64 | |||
65 | Object.keys(options.fields).forEach(field => { | ||
66 | const value = options.fields[field] | ||
67 | |||
68 | if (Array.isArray(value)) { | ||
69 | for (let i = 0; i < value.length; i++) { | ||
70 | req.field(field + '[' + i + ']', value[i]) | ||
71 | } | ||
72 | } else { | ||
73 | req.field(field, value) | ||
74 | } | ||
75 | }) | ||
76 | |||
77 | Object.keys(options.attaches).forEach(attach => { | ||
78 | const value = options.attaches[attach] | ||
79 | if (Array.isArray(value)) { | ||
80 | req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1]) | ||
81 | } else { | ||
82 | req.attach(attach, buildAbsoluteFixturePath(value)) | ||
83 | } | ||
84 | }) | ||
85 | |||
86 | return req.expect(options.statusCodeExpected) | ||
87 | } | ||
88 | |||
89 | function makePostBodyRequest (options: { | ||
90 | url: string, | ||
91 | path: string, | ||
92 | token?: string, | ||
93 | fields?: { [ fieldName: string ]: any }, | ||
94 | statusCodeExpected?: number | ||
95 | }) { | ||
96 | if (!options.fields) options.fields = {} | ||
97 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | ||
98 | |||
99 | const req = request(options.url) | ||
100 | .post(options.path) | ||
101 | .set('Accept', 'application/json') | ||
102 | |||
103 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | ||
104 | |||
105 | return req.send(options.fields) | ||
106 | .expect(options.statusCodeExpected) | ||
107 | } | ||
108 | |||
109 | function makePutBodyRequest (options: { | ||
110 | url: string, | ||
111 | path: string, | ||
112 | token?: string, | ||
113 | fields: { [ fieldName: string ]: any }, | ||
114 | statusCodeExpected?: number | ||
115 | }) { | ||
116 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | ||
117 | |||
118 | const req = request(options.url) | ||
119 | .put(options.path) | ||
120 | .set('Accept', 'application/json') | ||
121 | |||
122 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | ||
123 | |||
124 | return req.send(options.fields) | ||
125 | .expect(options.statusCodeExpected) | ||
126 | } | ||
127 | |||
128 | function makeHTMLRequest (url: string, path: string) { | ||
129 | return request(url) | ||
130 | .get(path) | ||
131 | .set('Accept', 'text/html') | ||
132 | .expect(200) | ||
133 | } | ||
134 | |||
135 | function updateAvatarRequest (options: { | ||
136 | url: string, | ||
137 | path: string, | ||
138 | accessToken: string, | ||
139 | fixture: string | ||
140 | }) { | ||
141 | let filePath = '' | ||
142 | if (isAbsolute(options.fixture)) { | ||
143 | filePath = options.fixture | ||
144 | } else { | ||
145 | filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture) | ||
146 | } | ||
147 | |||
148 | return makeUploadRequest({ | ||
149 | url: options.url, | ||
150 | path: options.path, | ||
151 | token: options.accessToken, | ||
152 | fields: {}, | ||
153 | attaches: { avatarfile: filePath }, | ||
154 | statusCodeExpected: 200 | ||
155 | }) | ||
156 | } | ||
157 | |||
158 | // --------------------------------------------------------------------------- | ||
159 | |||
160 | export { | ||
161 | makeHTMLRequest, | ||
162 | makeGetRequest, | ||
163 | makeUploadRequest, | ||
164 | makePostBodyRequest, | ||
165 | makePutBodyRequest, | ||
166 | makeDeleteRequest, | ||
167 | updateAvatarRequest | ||
168 | } | ||