2016-08-07 16:18:14 -04:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const request = require('supertest')
|
|
|
|
|
|
|
|
const requestsUtils = {
|
2016-10-02 06:19:02 -04:00
|
|
|
makePostUploadRequest,
|
|
|
|
makePostBodyRequest,
|
|
|
|
makePutBodyRequest
|
2016-08-07 16:18:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------- 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)
|
|
|
|
})
|
|
|
|
|
2017-01-27 05:55:31 -05:00
|
|
|
req.expect(statusCodeExpected)
|
|
|
|
.end(done)
|
2016-08-07 16:18:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-01-27 05:55:31 -05:00
|
|
|
req.send(fields)
|
|
|
|
.expect(statusCodeExpected)
|
|
|
|
.end(done)
|
2016-08-07 16:18:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2017-01-27 05:55:31 -05:00
|
|
|
req.send(fields)
|
|
|
|
.expect(statusCodeExpected)
|
|
|
|
.end(done)
|
2016-08-07 16:18:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
module.exports = requestsUtils
|