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