1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
'use strict'
const request = require('supertest')
const requestsUtils = {
makePostUploadRequest,
makePostBodyRequest,
makePutBodyRequest
}
// ---------------------- Export functions --------------------
function makePostUploadRequest (url, path, token, fields, attaches, done, statusCodeExpected) {
if (!statusCodeExpected) statusCodeExpected = 400
const req = request(url)
.post(path)
.set('Accept', 'application/json')
if (token) req.set('Authorization', 'Bearer ' + token)
Object.keys(fields).forEach(function (field) {
const value = fields[field]
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
req.field(field + '[' + i + ']', value[i])
}
} else {
req.field(field, value)
}
})
Object.keys(attaches).forEach(function (attach) {
const value = attaches[attach]
req.attach(attach, value)
})
req.expect(statusCodeExpected, done)
}
function makePostBodyRequest (url, path, token, fields, done, statusCodeExpected) {
if (!statusCodeExpected) statusCodeExpected = 400
const req = request(url)
.post(path)
.set('Accept', 'application/json')
if (token) req.set('Authorization', 'Bearer ' + token)
req.send(fields).expect(statusCodeExpected, done)
}
function makePutBodyRequest (url, path, token, fields, done, statusCodeExpected) {
if (!statusCodeExpected) statusCodeExpected = 400
const req = request(url)
.put(path)
.set('Accept', 'application/json')
if (token) req.set('Authorization', 'Bearer ' + token)
req.send(fields).expect(statusCodeExpected, done)
}
// ---------------------------------------------------------------------------
module.exports = requestsUtils
|