aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils/requests
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/utils/requests')
-rw-r--r--server/tests/utils/requests/requests.ts92
1 files changed, 92 insertions, 0 deletions
diff --git a/server/tests/utils/requests/requests.ts b/server/tests/utils/requests/requests.ts
new file mode 100644
index 000000000..52b7a4c29
--- /dev/null
+++ b/server/tests/utils/requests/requests.ts
@@ -0,0 +1,92 @@
1import * as request from 'supertest'
2
3function 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
11function 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
47function 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
66function 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
87export {
88 makeGetRequest,
89 makePostUploadRequest,
90 makePostBodyRequest,
91 makePutBodyRequest
92}