]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/utils/requests/requests.ts
dc2d4abe566362878fc40eb262edb6717c21c9bf
[github/Chocobozzz/PeerTube.git] / shared / utils / requests / requests.ts
1 import * as request from 'supertest'
2 import { buildAbsoluteFixturePath, root } from '../miscs/miscs'
3 import { isAbsolute, join } from 'path'
4 import { parse } from 'url'
5
6 function get4KFileUrl () {
7 return 'https://download.cpy.re/peertube/4k_file.txt'
8 }
9
10 function makeRawRequest (url: string, statusCodeExpected?: number, range?: string) {
11 const { host, protocol, pathname } = parse(url)
12
13 return makeGetRequest({ url: `${protocol}//${host}`, path: pathname, statusCodeExpected, range })
14 }
15
16 function makeGetRequest (options: {
17 url: string,
18 path?: string,
19 query?: any,
20 token?: string,
21 statusCodeExpected?: number,
22 contentType?: string,
23 range?: string
24 }) {
25 if (!options.statusCodeExpected) options.statusCodeExpected = 400
26 if (options.contentType === undefined) options.contentType = 'application/json'
27
28 const req = request(options.url).get(options.path)
29
30 if (options.contentType) req.set('Accept', options.contentType)
31 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
32 if (options.query) req.query(options.query)
33 if (options.range) req.set('Range', options.range)
34
35 return req.expect(options.statusCodeExpected)
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
52 return req.expect(options.statusCodeExpected)
53 }
54
55 function makeUploadRequest (options: {
56 url: string,
57 method?: 'POST' | 'PUT',
58 path: string,
59 token?: string,
60 fields: { [ fieldName: string ]: any },
61 attaches: { [ attachName: string ]: any | any[] },
62 statusCodeExpected?: number
63 }) {
64 if (!options.statusCodeExpected) options.statusCodeExpected = 400
65
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')
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]
91 if (Array.isArray(value)) {
92 req.attach(attach, buildAbsoluteFixturePath(value[0]), value[1])
93 } else {
94 req.attach(attach, buildAbsoluteFixturePath(value))
95 }
96 })
97
98 return req.expect(options.statusCodeExpected)
99 }
100
101 function makePostBodyRequest (options: {
102 url: string,
103 path: string,
104 token?: string,
105 fields?: { [ fieldName: string ]: any },
106 statusCodeExpected?: number
107 }) {
108 if (!options.fields) options.fields = {}
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,
124 token?: string,
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
140 function makeHTMLRequest (url: string, path: string) {
141 return request(url)
142 .get(path)
143 .set('Accept', 'text/html')
144 .expect(200)
145 }
146
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 {
157 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
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
170 // ---------------------------------------------------------------------------
171
172 export {
173 get4KFileUrl,
174 makeHTMLRequest,
175 makeGetRequest,
176 makeUploadRequest,
177 makePostBodyRequest,
178 makePutBodyRequest,
179 makeDeleteRequest,
180 makeRawRequest,
181 updateAvatarRequest
182 }