apigeek-affirm
Version:
A BDD/Gherkin micro-framework for REST APIs
128 lines (107 loc) • 3.7 kB
JavaScript
var assert = require('assert');
var request = require('request'); // https://github.com/request/request
var _ = require('lodash');
var fs = require('fs');
var jsonPath = require('JSONPath');
var DOM = require('xmldom').DOMParser;
//var xpath = require('xpath');
var http = module.exports;
http._cookies = {};
http.cookies = function(name) {
(name=name===true||name == undefined)?"default":name;
return http._cookies[name] = http._cookies[name]?http._cookies[name]:request.jar();
}
http.authorize = function (request, principal) {
var base64 = new Buffer(principal.username + ':' + principal.password).toString('base64');
request.headers.Authorization = 'Basic ' + base64;
return base64;
}
http.command = function (method, resource, options, context) {
if (resource.indexOf("://")<0) {
var host = context.protocol + "://" + context.hostname + (context.port > 0 ? ":" + context.port : "");
options.url = options.url || (host + ""+context.basePath+""+resource);
this.DEBUG && console.log("Using URL %s", options.url);
} else {
options.url = resource;
}
var cmd = _.extend({
method: method,
jar: options.cookies || context.cookies,
headers: {},
strictSSL: false,
body: null,
followRedirect: options.followRedirect || false,
qs: {}
}, options);
this.DEBUG && console.log("%s @ %s\n%j\n%j", method, resource, cmd.strictSSL, options.strictSSL);
return cmd;
}
http.handleResponse = function (self, done) {
self.stopwatch.start = _.now();
return function (error, response) {
self.stopwatch.stop = _.now();
self.stopwatch.duration = self.stopwatch.stop - self.stopwatch.start;
if (error) {
self.error = error;
done && done(error);
} else {
_.extend(self.response, response);
done && done(null, response);
}
this.DEBUG && console.log("Response (%s) from %s in %s ms", response.statusCode, self.request.url, self.stopwatch.duration);
};
}
http.certificate = function (request, cert, options) {
var isRawPEM = function(pem) {
return pem.indexOf("-----BEGIN")==0;
}
_.extend(request, {
agentOptions: {
key: isRawPEM(cert.key)?cert.key:fs.readFileSync(cert.key),
cert: isRawPEM(cert.cert)?cert.cert:fs.readFileSync(cert.cert),
ca: isRawPEM(cert.ca)?cert.ca:fs.readFileSync(cert.ca),
passphrase: cert.passphrase,
},
requestCert: true,
strictSSL: false,
rejectUnauthorized: false
}, options);
}
http.detectContentType = function (payload) {
try {
JSON.parse(payload);
return 'json';
} catch (e) {
try {
new DOM().parseFromString(payload);
return 'xml';
} catch (e) {
return null;
}
}
};
http.parse = function (payload) {
try {
return JSON.parse(payload);
} catch (e) {
return new DOM().parseFromString(payload);
}
};
http.detectFileType = function (file) {
var ix = file.lastIndexOf(".");
if (ix<0) return "";
return file.substring(ix+1);
}
http.findInPath = function (body, path) {
var json = _.isString(body)?JSON.parse(body):body;
var found = jsonPath({resultType: 'all'}, path, json);
return (found.length > 0) ? found[0].value : undefined;
};
/**
* @return {boolean}
*/
http.IsStatusCodeXX = function(statusXX, statusCode) {
if (statusXX.indexOf("xx")>0) {
return statusCode >= (statusXX[0] * 100) && statusCode <= 99 + (statusXX[0] * 100);
} else return statusCode == statusXX;
}