aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils/requests/requests.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tests/utils/requests/requests.ts')
-rw-r--r--server/tests/utils/requests/requests.ts170
1 files changed, 0 insertions, 170 deletions
diff --git a/server/tests/utils/requests/requests.ts b/server/tests/utils/requests/requests.ts
deleted file mode 100644
index 27a529eda..000000000
--- a/server/tests/utils/requests/requests.ts
+++ /dev/null
@@ -1,170 +0,0 @@
1import * as request from 'supertest'
2import { buildAbsoluteFixturePath } from '../miscs/miscs'
3import { isAbsolute, join } from 'path'
4
5function makeGetRequest (options: {
6 url: string,
7 path: string,
8 query?: any,
9 token?: string,
10 statusCodeExpected?: number,
11 contentType?: string
12}) {
13 if (!options.statusCodeExpected) options.statusCodeExpected = 400
14 if (options.contentType === undefined) options.contentType = 'application/json'
15
16 const req = request(options.url)
17 .get(options.path)
18
19 if (options.contentType) req.set('Accept', options.contentType)
20 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
21 if (options.query) req.query(options.query)
22
23 return req.expect(options.statusCodeExpected)
24}
25
26function 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)
43}
44
45function makeUploadRequest (options: {
46 url: string,
47 method?: 'POST' | 'PUT',
48 path: string,
49 token?: string,
50 fields: { [ fieldName: string ]: any },
51 attaches: { [ attachName: string ]: any | any[] },
52 statusCodeExpected?: number
53}) {
54 if (!options.statusCodeExpected) options.statusCodeExpected = 400
55
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')
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]
81 if (Array.isArray(value)) {
82 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
83 } else {
84 req.attach(attach, buildAbsoluteFixturePath(value))
85 }
86 })
87
88 return req.expect(options.statusCodeExpected)
89}
90
91function makePostBodyRequest (options: {
92 url: string,
93 path: string,
94 token?: string,
95 fields?: { [ fieldName: string ]: any },
96 statusCodeExpected?: number
97}) {
98 if (!options.fields) options.fields = {}
99 if (!options.statusCodeExpected) options.statusCodeExpected = 400
100
101 const req = request(options.url)
102 .post(options.path)
103 .set('Accept', 'application/json')
104
105 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
106
107 return req.send(options.fields)
108 .expect(options.statusCodeExpected)
109}
110
111function makePutBodyRequest (options: {
112 url: string,
113 path: string,
114 token?: string,
115 fields: { [ fieldName: string ]: any },
116 statusCodeExpected?: number
117}) {
118 if (!options.statusCodeExpected) options.statusCodeExpected = 400
119
120 const req = request(options.url)
121 .put(options.path)
122 .set('Accept', 'application/json')
123
124 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
125
126 return req.send(options.fields)
127 .expect(options.statusCodeExpected)
128}
129
130function makeHTMLRequest (url: string, path: string) {
131 return request(url)
132 .get(path)
133 .set('Accept', 'text/html')
134 .expect(200)
135}
136
137function updateAvatarRequest (options: {
138 url: string,
139 path: string,
140 accessToken: string,
141 fixture: string
142}) {
143 let filePath = ''
144 if (isAbsolute(options.fixture)) {
145 filePath = options.fixture
146 } else {
147 filePath = join(__dirname, '..', '..', 'fixtures', options.fixture)
148 }
149
150 return makeUploadRequest({
151 url: options.url,
152 path: options.path,
153 token: options.accessToken,
154 fields: {},
155 attaches: { avatarfile: filePath },
156 statusCodeExpected: 200
157 })
158}
159
160// ---------------------------------------------------------------------------
161
162export {
163 makeHTMLRequest,
164 makeGetRequest,
165 makeUploadRequest,
166 makePostBodyRequest,
167 makePutBodyRequest,
168 makeDeleteRequest,
169 updateAvatarRequest
170}