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