]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/utils/requests/requests.ts
Use a single file instead of segments for HLS
[github/Chocobozzz/PeerTube.git] / shared / 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
4c280004 6function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
09209296
C
7 const { host, protocol, pathname } = parse(url)
8
4c280004 9 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
09209296 10}
0e1dc3e7 11
eec63bbc
C
12function makeGetRequest (options: {
13 url: string,
09209296 14 path?: string,
eec63bbc
C
15 query?: any,
16 token?: string,
ebdb6124 17 statusCodeExpected?: number,
4c280004
C
18 contentType?: string,
19 range?: string
eec63bbc
C
20}) {
21 if (!options.statusCodeExpected) options.statusCodeExpected = 400
ebdb6124 22 if (options.contentType === undefined) options.contentType = 'application/json'
eec63bbc 23
09209296 24 const req = request(options.url).get(options.path)
eec63bbc 25
ebdb6124 26 if (options.contentType) req.set('Accept', options.contentType)
eec63bbc
C
27 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
28 if (options.query) req.query(options.query)
4c280004 29 if (options.range) req.set('Range', options.range)
eec63bbc 30
ebdb6124 31 return req.expect(options.statusCodeExpected)
eec63bbc
C
32}
33
34function 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
7ad9b984 48 return req.expect(options.statusCodeExpected)
0e1dc3e7
C
49}
50
ac81d1a0 51function makeUploadRequest (options: {
0e1dc3e7 52 url: string,
ac81d1a0 53 method?: 'POST' | 'PUT',
0e1dc3e7 54 path: string,
4bbfc6c6 55 token?: string,
0e1dc3e7 56 fields: { [ fieldName: string ]: any },
2769e297 57 attaches: { [ attachName: string ]: any | any[] },
0e1dc3e7
C
58 statusCodeExpected?: number
59}) {
60 if (!options.statusCodeExpected) options.statusCodeExpected = 400
61
ac81d1a0
C
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')
0e1dc3e7
C
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]
2769e297
C
87 if (Array.isArray(value)) {
88 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
89 } else {
90 req.attach(attach, buildAbsoluteFixturePath(value))
91 }
0e1dc3e7
C
92 })
93
94 return req.expect(options.statusCodeExpected)
95}
96
97function makePostBodyRequest (options: {
98 url: string,
99 path: string,
100 token?: string,
eec63bbc 101 fields?: { [ fieldName: string ]: any },
0e1dc3e7
C
102 statusCodeExpected?: number
103}) {
eec63bbc 104 if (!options.fields) options.fields = {}
0e1dc3e7
C
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
117function makePutBodyRequest (options: {
118 url: string,
119 path: string,
fd206f0b 120 token?: string,
0e1dc3e7
C
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
e032aec9
C
136function makeHTMLRequest (url: string, path: string) {
137 return request(url)
138 .get(path)
139 .set('Accept', 'text/html')
140 .expect(200)
141}
142
4bbfc6c6
C
143function 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 {
2a8c5d0a 153 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
4bbfc6c6
C
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
0e1dc3e7
C
166// ---------------------------------------------------------------------------
167
168export {
e032aec9 169 makeHTMLRequest,
0e1dc3e7 170 makeGetRequest,
ac81d1a0 171 makeUploadRequest,
0e1dc3e7 172 makePostBodyRequest,
eec63bbc 173 makePutBodyRequest,
4bbfc6c6 174 makeDeleteRequest,
09209296 175 makeRawRequest,
4bbfc6c6 176 updateAvatarRequest
0e1dc3e7 177}