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