]>
Commit | Line | Data |
---|---|---|
0e1dc3e7 C |
1 | import * as request from 'supertest' |
2 | ||
eec63bbc C |
3 | function makeGetRequest (options: { |
4 | url: string, | |
5 | path: string, | |
6 | query?: any, | |
7 | token?: string, | |
8 | statusCodeExpected?: number | |
9 | }) { | |
10 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | |
11 | ||
12 | const req = request(options.url) | |
13 | .get(options.path) | |
0e1dc3e7 | 14 | .set('Accept', 'application/json') |
eec63bbc C |
15 | |
16 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
17 | if (options.query) req.query(options.query) | |
18 | ||
19 | return req | |
0e1dc3e7 | 20 | .expect('Content-Type', /json/) |
eec63bbc C |
21 | .expect(options.statusCodeExpected) |
22 | } | |
23 | ||
24 | function makeDeleteRequest (options: { | |
25 | url: string, | |
26 | path: string, | |
27 | token?: string, | |
28 | statusCodeExpected?: number | |
29 | }) { | |
30 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | |
31 | ||
32 | const req = request(options.url) | |
33 | .delete(options.path) | |
34 | .set('Accept', 'application/json') | |
35 | ||
36 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
37 | ||
38 | return req | |
39 | .expect('Content-Type', /json/) | |
40 | .expect(options.statusCodeExpected) | |
0e1dc3e7 C |
41 | } |
42 | ||
43 | function makePostUploadRequest (options: { | |
44 | url: string, | |
45 | path: string, | |
46 | token: string, | |
47 | fields: { [ fieldName: string ]: any }, | |
48 | attaches: { [ attachName: string ]: any }, | |
49 | statusCodeExpected?: number | |
50 | }) { | |
51 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | |
52 | ||
53 | const req = request(options.url) | |
54 | .post(options.path) | |
55 | .set('Accept', 'application/json') | |
56 | ||
57 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
58 | ||
59 | Object.keys(options.fields).forEach(field => { | |
60 | const value = options.fields[field] | |
61 | ||
62 | if (Array.isArray(value)) { | |
63 | for (let i = 0; i < value.length; i++) { | |
64 | req.field(field + '[' + i + ']', value[i]) | |
65 | } | |
66 | } else { | |
67 | req.field(field, value) | |
68 | } | |
69 | }) | |
70 | ||
71 | Object.keys(options.attaches).forEach(attach => { | |
72 | const value = options.attaches[attach] | |
73 | req.attach(attach, value) | |
74 | }) | |
75 | ||
76 | return req.expect(options.statusCodeExpected) | |
77 | } | |
78 | ||
79 | function makePostBodyRequest (options: { | |
80 | url: string, | |
81 | path: string, | |
82 | token?: string, | |
eec63bbc | 83 | fields?: { [ fieldName: string ]: any }, |
0e1dc3e7 C |
84 | statusCodeExpected?: number |
85 | }) { | |
eec63bbc | 86 | if (!options.fields) options.fields = {} |
0e1dc3e7 C |
87 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 |
88 | ||
89 | const req = request(options.url) | |
90 | .post(options.path) | |
91 | .set('Accept', 'application/json') | |
92 | ||
93 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
94 | ||
95 | return req.send(options.fields) | |
96 | .expect(options.statusCodeExpected) | |
97 | } | |
98 | ||
99 | function makePutBodyRequest (options: { | |
100 | url: string, | |
101 | path: string, | |
102 | token: string, | |
103 | fields: { [ fieldName: string ]: any }, | |
104 | statusCodeExpected?: number | |
105 | }) { | |
106 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | |
107 | ||
108 | const req = request(options.url) | |
109 | .put(options.path) | |
110 | .set('Accept', 'application/json') | |
111 | ||
112 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
113 | ||
114 | return req.send(options.fields) | |
115 | .expect(options.statusCodeExpected) | |
116 | } | |
117 | ||
118 | // --------------------------------------------------------------------------- | |
119 | ||
120 | export { | |
121 | makeGetRequest, | |
122 | makePostUploadRequest, | |
123 | makePostBodyRequest, | |
eec63bbc C |
124 | makePutBodyRequest, |
125 | makeDeleteRequest | |
0e1dc3e7 | 126 | } |