diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-09-04 21:21:47 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-09-04 21:30:18 +0200 |
commit | 0e1dc3e7c69995c691e1dd82e3c2bc68748661ca (patch) | |
tree | f2a4b5cffc72e33c902b67083bbaa35b6f22f0ca /server/tests/utils/requests.ts | |
parent | b0f9f39ed70299a208d1b388c72de8b7f3510cb7 (diff) | |
download | PeerTube-0e1dc3e7c69995c691e1dd82e3c2bc68748661ca.tar.gz PeerTube-0e1dc3e7c69995c691e1dd82e3c2bc68748661ca.tar.zst PeerTube-0e1dc3e7c69995c691e1dd82e3c2bc68748661ca.zip |
Convert tests to typescript
Diffstat (limited to 'server/tests/utils/requests.ts')
-rw-r--r-- | server/tests/utils/requests.ts | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/server/tests/utils/requests.ts b/server/tests/utils/requests.ts new file mode 100644 index 000000000..52b7a4c29 --- /dev/null +++ b/server/tests/utils/requests.ts | |||
@@ -0,0 +1,92 @@ | |||
1 | import * as request from 'supertest' | ||
2 | |||
3 | function makeGetRequest (url: string, path: string) { | ||
4 | return request(url) | ||
5 | .get(path) | ||
6 | .set('Accept', 'application/json') | ||
7 | .expect(200) | ||
8 | .expect('Content-Type', /json/) | ||
9 | } | ||
10 | |||
11 | function makePostUploadRequest (options: { | ||
12 | url: string, | ||
13 | path: string, | ||
14 | token: string, | ||
15 | fields: { [ fieldName: string ]: any }, | ||
16 | attaches: { [ attachName: string ]: any }, | ||
17 | statusCodeExpected?: number | ||
18 | }) { | ||
19 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | ||
20 | |||
21 | const req = request(options.url) | ||
22 | .post(options.path) | ||
23 | .set('Accept', 'application/json') | ||
24 | |||
25 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | ||
26 | |||
27 | Object.keys(options.fields).forEach(field => { | ||
28 | const value = options.fields[field] | ||
29 | |||
30 | if (Array.isArray(value)) { | ||
31 | for (let i = 0; i < value.length; i++) { | ||
32 | req.field(field + '[' + i + ']', value[i]) | ||
33 | } | ||
34 | } else { | ||
35 | req.field(field, value) | ||
36 | } | ||
37 | }) | ||
38 | |||
39 | Object.keys(options.attaches).forEach(attach => { | ||
40 | const value = options.attaches[attach] | ||
41 | req.attach(attach, value) | ||
42 | }) | ||
43 | |||
44 | return req.expect(options.statusCodeExpected) | ||
45 | } | ||
46 | |||
47 | function makePostBodyRequest (options: { | ||
48 | url: string, | ||
49 | path: string, | ||
50 | token?: string, | ||
51 | fields: { [ fieldName: string ]: any }, | ||
52 | statusCodeExpected?: number | ||
53 | }) { | ||
54 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | ||
55 | |||
56 | const req = request(options.url) | ||
57 | .post(options.path) | ||
58 | .set('Accept', 'application/json') | ||
59 | |||
60 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | ||
61 | |||
62 | return req.send(options.fields) | ||
63 | .expect(options.statusCodeExpected) | ||
64 | } | ||
65 | |||
66 | function makePutBodyRequest (options: { | ||
67 | url: string, | ||
68 | path: string, | ||
69 | token: string, | ||
70 | fields: { [ fieldName: string ]: any }, | ||
71 | statusCodeExpected?: number | ||
72 | }) { | ||
73 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | ||
74 | |||
75 | const req = request(options.url) | ||
76 | .put(options.path) | ||
77 | .set('Accept', 'application/json') | ||
78 | |||
79 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | ||
80 | |||
81 | return req.send(options.fields) | ||
82 | .expect(options.statusCodeExpected) | ||
83 | } | ||
84 | |||
85 | // --------------------------------------------------------------------------- | ||
86 | |||
87 | export { | ||
88 | makeGetRequest, | ||
89 | makePostUploadRequest, | ||
90 | makePostBodyRequest, | ||
91 | makePutBodyRequest | ||
92 | } | ||