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