]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/utils/requests/requests.ts
Add esperanto, lojban, klingon and kotava (audio/subtitle) languages
[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,
10 statusCodeExpected?: number
11}) {
12 if (!options.statusCodeExpected) options.statusCodeExpected = 400
13
14 const req = request(options.url)
15 .get(options.path)
0e1dc3e7 16 .set('Accept', 'application/json')
eec63bbc
C
17
18 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
19 if (options.query) req.query(options.query)
20
21 return req
0e1dc3e7 22 .expect('Content-Type', /json/)
eec63bbc
C
23 .expect(options.statusCodeExpected)
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
C
50 fields: { [ fieldName: string ]: any },
51 attaches: { [ attachName: string ]: any },
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]
ac81d1a0 81 req.attach(attach, buildAbsoluteFixturePath(value))
0e1dc3e7
C
82 })
83
84 return req.expect(options.statusCodeExpected)
85}
86
87function makePostBodyRequest (options: {
88 url: string,
89 path: string,
90 token?: string,
eec63bbc 91 fields?: { [ fieldName: string ]: any },
0e1dc3e7
C
92 statusCodeExpected?: number
93}) {
eec63bbc 94 if (!options.fields) options.fields = {}
0e1dc3e7
C
95 if (!options.statusCodeExpected) options.statusCodeExpected = 400
96
97 const req = request(options.url)
98 .post(options.path)
99 .set('Accept', 'application/json')
100
101 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
102
103 return req.send(options.fields)
104 .expect(options.statusCodeExpected)
105}
106
107function makePutBodyRequest (options: {
108 url: string,
109 path: string,
fd206f0b 110 token?: string,
0e1dc3e7
C
111 fields: { [ fieldName: string ]: any },
112 statusCodeExpected?: number
113}) {
114 if (!options.statusCodeExpected) options.statusCodeExpected = 400
115
116 const req = request(options.url)
117 .put(options.path)
118 .set('Accept', 'application/json')
119
120 if (options.token) req.set('Authorization', 'Bearer ' + options.token)
121
122 return req.send(options.fields)
123 .expect(options.statusCodeExpected)
124}
125
4bbfc6c6
C
126function updateAvatarRequest (options: {
127 url: string,
128 path: string,
129 accessToken: string,
130 fixture: string
131}) {
132 let filePath = ''
133 if (isAbsolute(options.fixture)) {
134 filePath = options.fixture
135 } else {
136 filePath = join(__dirname, '..', '..', 'fixtures', options.fixture)
137 }
138
139 return makeUploadRequest({
140 url: options.url,
141 path: options.path,
142 token: options.accessToken,
143 fields: {},
144 attaches: { avatarfile: filePath },
145 statusCodeExpected: 200
146 })
147}
148
0e1dc3e7
C
149// ---------------------------------------------------------------------------
150
151export {
152 makeGetRequest,
ac81d1a0 153 makeUploadRequest,
0e1dc3e7 154 makePostBodyRequest,
eec63bbc 155 makePutBodyRequest,
4bbfc6c6
C
156 makeDeleteRequest,
157 updateAvatarRequest
0e1dc3e7 158}