From 25ed57f3db6301a5e87020b66d33671598e61df1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 7 Aug 2016 22:18:14 +0200 Subject: Server: create requests utils module --- server/tests/utils/requests.js | 68 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 server/tests/utils/requests.js (limited to 'server/tests/utils') diff --git a/server/tests/utils/requests.js b/server/tests/utils/requests.js new file mode 100644 index 000000000..410e42c77 --- /dev/null +++ b/server/tests/utils/requests.js @@ -0,0 +1,68 @@ +'use strict' + +const request = require('supertest') + +const requestsUtils = { + makePostUploadRequest: makePostUploadRequest, + makePostBodyRequest: makePostBodyRequest, + makePutBodyRequest: 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 -- cgit v1.2.3