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