2016-04-09 21:08:55 +00:00
|
|
|
import networkRequest from 'request'
|
2016-04-10 02:58:48 +00:00
|
|
|
import render from './render'
|
2016-04-17 01:52:10 +00:00
|
|
|
import * as db from '../database'
|
2016-04-09 21:08:55 +00:00
|
|
|
|
2016-04-28 07:30:26 +00:00
|
|
|
function buildRequestConfig (request, patch = {}) {
|
2016-04-09 21:08:55 +00:00
|
|
|
const config = {
|
2016-04-10 02:58:48 +00:00
|
|
|
method: request.method,
|
|
|
|
body: request.body,
|
2016-04-28 07:30:26 +00:00
|
|
|
headers: {},
|
|
|
|
|
|
|
|
// Setup redirect rules
|
|
|
|
followRedirect: true,
|
|
|
|
maxRedirects: 10,
|
|
|
|
|
|
|
|
// Unzip gzipped responses
|
|
|
|
gzip: true
|
2016-04-09 21:08:55 +00:00
|
|
|
};
|
|
|
|
|
2016-04-17 04:28:57 +00:00
|
|
|
// Default the proto if it doesn't exist
|
2016-04-17 01:52:10 +00:00
|
|
|
if (request.url.indexOf('://') === -1) {
|
|
|
|
config.url = `https://${request.url}`;
|
2016-04-17 04:28:57 +00:00
|
|
|
} else {
|
|
|
|
config.url = request.url;
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 04:28:57 +00:00
|
|
|
// Set basic auth if we need to
|
2016-04-09 21:08:55 +00:00
|
|
|
if (request.authentication.username) {
|
|
|
|
config.auth = {
|
|
|
|
user: request.authentication.username,
|
|
|
|
pass: request.authentication.password
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-10 02:58:48 +00:00
|
|
|
for (let i = 0; i < request.headers.length; i++) {
|
|
|
|
let header = request.headers[i];
|
2016-04-12 00:39:49 +00:00
|
|
|
if (header.name) {
|
|
|
|
config.headers[header.name] = header.value;
|
|
|
|
}
|
2016-04-09 21:08:55 +00:00
|
|
|
}
|
2016-04-17 01:52:10 +00:00
|
|
|
|
2016-04-28 07:30:26 +00:00
|
|
|
// TODO: this needs to account for existing URL params (hint: use request)
|
2016-04-12 06:03:52 +00:00
|
|
|
config.url += request.params.map((p, i) => {
|
|
|
|
const name = encodeURIComponent(p.name);
|
|
|
|
const value = encodeURIComponent(p.value);
|
|
|
|
return `${i === 0 ? '?' : '&'}${name}=${value}`;
|
|
|
|
}).join('');
|
2016-04-28 07:30:26 +00:00
|
|
|
|
|
|
|
return Object.assign(config, patch);
|
|
|
|
}
|
|
|
|
|
|
|
|
function actuallySend (request, callback) {
|
|
|
|
// TODO: Handle cookies
|
|
|
|
let config = buildRequestConfig(request, {jar: networkRequest.jar()});
|
2016-04-09 21:41:27 +00:00
|
|
|
|
2016-04-17 05:10:59 +00:00
|
|
|
const startTime = Date.now();
|
2016-04-17 01:52:10 +00:00
|
|
|
networkRequest(config, function (err, response) {
|
2016-04-10 02:58:48 +00:00
|
|
|
if (err) {
|
2016-04-18 04:39:15 +00:00
|
|
|
console.error('Request Failed', err, response);
|
2016-04-10 02:58:48 +00:00
|
|
|
} else {
|
2016-04-18 04:39:15 +00:00
|
|
|
db.responseCreate({
|
2016-04-26 07:29:24 +00:00
|
|
|
parentId: request._id,
|
2016-04-10 06:37:22 +00:00
|
|
|
statusCode: response.statusCode,
|
2016-04-17 05:10:59 +00:00
|
|
|
statusMessage: response.statusMessage,
|
2016-04-18 04:39:15 +00:00
|
|
|
contentType: response.headers['content-type'],
|
|
|
|
millis: Date.now() - startTime,
|
|
|
|
bytes: response.connection.bytesRead,
|
|
|
|
body: response.body,
|
2016-04-10 06:37:22 +00:00
|
|
|
headers: Object.keys(response.headers).map(name => {
|
|
|
|
const value = response.headers[name];
|
|
|
|
return {name, value};
|
|
|
|
})
|
2016-04-10 02:58:48 +00:00
|
|
|
});
|
|
|
|
}
|
2016-04-28 07:30:26 +00:00
|
|
|
|
2016-04-18 04:39:15 +00:00
|
|
|
callback(err);
|
2016-04-09 21:08:55 +00:00
|
|
|
});
|
|
|
|
}
|
2016-04-17 01:52:10 +00:00
|
|
|
|
2016-04-26 07:29:24 +00:00
|
|
|
export function send (request, callback) {
|
2016-04-28 07:30:26 +00:00
|
|
|
db.requestGroupById(request.parentId).then(requestGroup => {
|
|
|
|
const environment = requestGroup ? requestGroup.environment : {};
|
|
|
|
|
|
|
|
if (environment) {
|
|
|
|
// SNEAKY HACK: Render nested object by converting it to JSON then rendering
|
|
|
|
const template = JSON.stringify(request);
|
|
|
|
request = JSON.parse(render(template, environment));
|
|
|
|
}
|
|
|
|
|
|
|
|
actuallySend(request, callback);
|
|
|
|
});
|
2016-04-17 01:52:10 +00:00
|
|
|
}
|