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