]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/requests/requests.ts
Cleanup tests
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / requests / requests.ts
CommitLineData
0e1dc3e7 1import * as request from 'supertest'
2a8c5d0a 2import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
4bbfc6c6 3import { isAbsolute, join } from 'path'
09209296
C
4import { parse } from 'url'
5
bfe2ef6b
C
6function get4KFileUrl () {
7 return 'https://download.cpy.re/peertube/4k_file.txt'
8}
9
4c280004 10function 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
16function 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
38function 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 55function 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
df0b219d
C
80 if (value === undefined) return
81
0e1dc3e7
C
82 if (Array.isArray(value)) {
83 for (let i = 0; i < value.length; i++) {
84 req.field(field + '[' + i + ']', value[i])
85 }
86 } else {
87 req.field(field, value)
88 }
89 })
90
91 Object.keys(options.attaches).forEach(attach => {
92 const value = options.attaches[attach]
2769e297
C
93 if (Array.isArray(value)) {
94 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
95 } else {
96 req.attach(attach, buildAbsoluteFixturePath(value))
97 }
0e1dc3e7
C
98 })
99
100 return req.expect(options.statusCodeExpected)
101}
102
103function makePostBodyRequest (options: {
104 url: string,
105 path: string,
106 token?: string,
eec63bbc 107 fields?: { [ fieldName: string ]: any },
0e1dc3e7
C
108 statusCodeExpected?: number
109}) {
eec63bbc 110 if (!options.fields) options.fields = {}
0e1dc3e7
C
111 if (!options.statusCodeExpected) options.statusCodeExpected = 400
112
113 const req = request(options.url)
114 .post(options.path)
115 .set('Accept', 'application/json')
116
117 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
118
119 return req.send(options.fields)
120 .expect(options.statusCodeExpected)
121}
122
123function makePutBodyRequest (options: {
124 url: string,
125 path: string,
fd206f0b 126 token?: string,
0e1dc3e7
C
127 fields: { [ fieldName: string ]: any },
128 statusCodeExpected?: number
129}) {
130 if (!options.statusCodeExpected) options.statusCodeExpected = 400
131
132 const req = request(options.url)
133 .put(options.path)
134 .set('Accept', 'application/json')
135
136 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
137
138 return req.send(options.fields)
139 .expect(options.statusCodeExpected)
140}
141
e032aec9
C
142function makeHTMLRequest (url: string, path: string) {
143 return request(url)
144 .get(path)
145 .set('Accept', 'text/html')
146 .expect(200)
147}
148
4bbfc6c6
C
149function updateAvatarRequest (options: {
150 url: string,
151 path: string,
152 accessToken: string,
153 fixture: string
154}) {
155 let filePath = ''
156 if (isAbsolute(options.fixture)) {
157 filePath = options.fixture
158 } else {
2a8c5d0a 159 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
4bbfc6c6
C
160 }
161
162 return makeUploadRequest({
163 url: options.url,
164 path: options.path,
165 token: options.accessToken,
166 fields: {},
167 attaches: { avatarfile: filePath },
168 statusCodeExpected: 200
169 })
170}
171
0e1dc3e7
C
172// ---------------------------------------------------------------------------
173
174export {
bfe2ef6b 175 get4KFileUrl,
e032aec9 176 makeHTMLRequest,
0e1dc3e7 177 makeGetRequest,
ac81d1a0 178 makeUploadRequest,
0e1dc3e7 179 makePostBodyRequest,
eec63bbc 180 makePutBodyRequest,
4bbfc6c6 181 makeDeleteRequest,
09209296 182 makeRawRequest,
4bbfc6c6 183 updateAvatarRequest
0e1dc3e7 184}