]>
Commit | Line | Data |
---|---|---|
0e1dc3e7 | 1 | import * as request from 'supertest' |
99d10301 | 2 | import { buildAbsoluteFixturePath } from '../miscs/miscs' |
4bbfc6c6 | 3 | import { isAbsolute, join } from 'path' |
0e1dc3e7 | 4 | |
eec63bbc C |
5 | function makeGetRequest (options: { |
6 | url: string, | |
7 | path: string, | |
8 | query?: any, | |
9 | token?: string, | |
10 | statusCodeExpected?: number | |
11 | }) { | |
12 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | |
13 | ||
14 | const req = request(options.url) | |
15 | .get(options.path) | |
0e1dc3e7 | 16 | .set('Accept', 'application/json') |
eec63bbc C |
17 | |
18 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
19 | if (options.query) req.query(options.query) | |
20 | ||
21 | return req | |
0e1dc3e7 | 22 | .expect('Content-Type', /json/) |
eec63bbc C |
23 | .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 | |
41 | .expect('Content-Type', /json/) | |
42 | .expect(options.statusCodeExpected) | |
0e1dc3e7 C |
43 | } |
44 | ||
ac81d1a0 | 45 | function makeUploadRequest (options: { |
0e1dc3e7 | 46 | url: string, |
ac81d1a0 | 47 | method?: 'POST' | 'PUT', |
0e1dc3e7 | 48 | path: string, |
4bbfc6c6 | 49 | token?: string, |
0e1dc3e7 C |
50 | fields: { [ fieldName: string ]: any }, |
51 | attaches: { [ attachName: string ]: any }, | |
52 | statusCodeExpected?: number | |
53 | }) { | |
54 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | |
55 | ||
ac81d1a0 C |
56 | let req: request.Test |
57 | if (options.method === 'PUT') { | |
58 | req = request(options.url).put(options.path) | |
59 | } else { | |
60 | req = request(options.url).post(options.path) | |
61 | } | |
62 | ||
63 | req.set('Accept', 'application/json') | |
0e1dc3e7 C |
64 | |
65 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
66 | ||
67 | Object.keys(options.fields).forEach(field => { | |
68 | const value = options.fields[field] | |
69 | ||
70 | if (Array.isArray(value)) { | |
71 | for (let i = 0; i < value.length; i++) { | |
72 | req.field(field + '[' + i + ']', value[i]) | |
73 | } | |
74 | } else { | |
75 | req.field(field, value) | |
76 | } | |
77 | }) | |
78 | ||
79 | Object.keys(options.attaches).forEach(attach => { | |
80 | const value = options.attaches[attach] | |
ac81d1a0 | 81 | req.attach(attach, buildAbsoluteFixturePath(value)) |
0e1dc3e7 C |
82 | }) |
83 | ||
84 | return req.expect(options.statusCodeExpected) | |
85 | } | |
86 | ||
87 | function makePostBodyRequest (options: { | |
88 | url: string, | |
89 | path: string, | |
90 | token?: string, | |
eec63bbc | 91 | fields?: { [ fieldName: string ]: any }, |
0e1dc3e7 C |
92 | statusCodeExpected?: number |
93 | }) { | |
eec63bbc | 94 | if (!options.fields) options.fields = {} |
0e1dc3e7 C |
95 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 |
96 | ||
97 | const req = request(options.url) | |
98 | .post(options.path) | |
99 | .set('Accept', 'application/json') | |
100 | ||
101 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
102 | ||
103 | return req.send(options.fields) | |
104 | .expect(options.statusCodeExpected) | |
105 | } | |
106 | ||
107 | function makePutBodyRequest (options: { | |
108 | url: string, | |
109 | path: string, | |
fd206f0b | 110 | token?: string, |
0e1dc3e7 C |
111 | fields: { [ fieldName: string ]: any }, |
112 | statusCodeExpected?: number | |
113 | }) { | |
114 | if (!options.statusCodeExpected) options.statusCodeExpected = 400 | |
115 | ||
116 | const req = request(options.url) | |
117 | .put(options.path) | |
118 | .set('Accept', 'application/json') | |
119 | ||
120 | if (options.token) req.set('Authorization', 'Bearer ' + options.token) | |
121 | ||
122 | return req.send(options.fields) | |
123 | .expect(options.statusCodeExpected) | |
124 | } | |
125 | ||
e032aec9 C |
126 | function makeHTMLRequest (url: string, path: string) { |
127 | return request(url) | |
128 | .get(path) | |
129 | .set('Accept', 'text/html') | |
130 | .expect(200) | |
131 | } | |
132 | ||
4bbfc6c6 C |
133 | function updateAvatarRequest (options: { |
134 | url: string, | |
135 | path: string, | |
136 | accessToken: string, | |
137 | fixture: string | |
138 | }) { | |
139 | let filePath = '' | |
140 | if (isAbsolute(options.fixture)) { | |
141 | filePath = options.fixture | |
142 | } else { | |
143 | filePath = join(__dirname, '..', '..', 'fixtures', options.fixture) | |
144 | } | |
145 | ||
146 | return makeUploadRequest({ | |
147 | url: options.url, | |
148 | path: options.path, | |
149 | token: options.accessToken, | |
150 | fields: {}, | |
151 | attaches: { avatarfile: filePath }, | |
152 | statusCodeExpected: 200 | |
153 | }) | |
154 | } | |
155 | ||
0e1dc3e7 C |
156 | // --------------------------------------------------------------------------- |
157 | ||
158 | export { | |
e032aec9 | 159 | makeHTMLRequest, |
0e1dc3e7 | 160 | makeGetRequest, |
ac81d1a0 | 161 | makeUploadRequest, |
0e1dc3e7 | 162 | makePostBodyRequest, |
eec63bbc | 163 | makePutBodyRequest, |
4bbfc6c6 C |
164 | makeDeleteRequest, |
165 | updateAvatarRequest | |
0e1dc3e7 | 166 | } |