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