diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-07 22:18:14 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-07 22:18:14 +0200 |
commit | 25ed57f3db6301a5e87020b66d33671598e61df1 (patch) | |
tree | 04f2264d0ec1fe5aec37dba208586bdf1bb2df5f /server/tests/utils/requests.js | |
parent | 8d30905858245f12a42fc327d2d57cbfe062d548 (diff) | |
download | PeerTube-25ed57f3db6301a5e87020b66d33671598e61df1.tar.gz PeerTube-25ed57f3db6301a5e87020b66d33671598e61df1.tar.zst PeerTube-25ed57f3db6301a5e87020b66d33671598e61df1.zip |
Server: create requests utils module
Diffstat (limited to 'server/tests/utils/requests.js')
-rw-r--r-- | server/tests/utils/requests.js | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/server/tests/utils/requests.js b/server/tests/utils/requests.js new file mode 100644 index 000000000..410e42c77 --- /dev/null +++ b/server/tests/utils/requests.js | |||
@@ -0,0 +1,68 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const request = require('supertest') | ||
4 | |||
5 | const requestsUtils = { | ||
6 | makePostUploadRequest: makePostUploadRequest, | ||
7 | makePostBodyRequest: makePostBodyRequest, | ||
8 | makePutBodyRequest: makePutBodyRequest | ||
9 | } | ||
10 | |||
11 | // ---------------------- Export functions -------------------- | ||
12 | |||
13 | function makePostUploadRequest (url, path, token, fields, attaches, done, statusCodeExpected) { | ||
14 | if (!statusCodeExpected) statusCodeExpected = 400 | ||
15 | |||
16 | const req = request(url) | ||
17 | .post(path) | ||
18 | .set('Accept', 'application/json') | ||
19 | |||
20 | if (token) req.set('Authorization', 'Bearer ' + token) | ||
21 | |||
22 | Object.keys(fields).forEach(function (field) { | ||
23 | const value = fields[field] | ||
24 | |||
25 | if (Array.isArray(value)) { | ||
26 | for (let i = 0; i < value.length; i++) { | ||
27 | req.field(field + '[' + i + ']', value[i]) | ||
28 | } | ||
29 | } else { | ||
30 | req.field(field, value) | ||
31 | } | ||
32 | }) | ||
33 | |||
34 | Object.keys(attaches).forEach(function (attach) { | ||
35 | const value = attaches[attach] | ||
36 | req.attach(attach, value) | ||
37 | }) | ||
38 | |||
39 | req.expect(statusCodeExpected, done) | ||
40 | } | ||
41 | |||
42 | function makePostBodyRequest (url, path, token, fields, done, statusCodeExpected) { | ||
43 | if (!statusCodeExpected) statusCodeExpected = 400 | ||
44 | |||
45 | const req = request(url) | ||
46 | .post(path) | ||
47 | .set('Accept', 'application/json') | ||
48 | |||
49 | if (token) req.set('Authorization', 'Bearer ' + token) | ||
50 | |||
51 | req.send(fields).expect(statusCodeExpected, done) | ||
52 | } | ||
53 | |||
54 | function makePutBodyRequest (url, path, token, fields, done, statusCodeExpected) { | ||
55 | if (!statusCodeExpected) statusCodeExpected = 400 | ||
56 | |||
57 | const req = request(url) | ||
58 | .put(path) | ||
59 | .set('Accept', 'application/json') | ||
60 | |||
61 | if (token) req.set('Authorization', 'Bearer ' + token) | ||
62 | |||
63 | req.send(fields).expect(statusCodeExpected, done) | ||
64 | } | ||
65 | |||
66 | // --------------------------------------------------------------------------- | ||
67 | |||
68 | module.exports = requestsUtils | ||