Skip to content
Snippets Groups Projects
Commit 2ead7961 authored by Lloyd Hilaiel's avatar Lloyd Hilaiel
Browse files

port unit tests to new wsapi_client abstraction to eliminate duplicate code

parent 9547deab
No related branches found
No related tags found
No related merge requests found
......@@ -34,132 +34,28 @@
* ***** END LICENSE BLOCK ***** */
const
http = require('http'),
querystring = require('querystring');
wcli = require('../../../libs/wsapi_client');
// wsapi abstractions trivial cookie jar
var cookieJar = {};
// the client "context"
var context = {};
// fetch the CSRF token for POSTs
var g_csrf = undefined;
var fetchCSRF = function(headers, cb) {
// used cached csrf if available
if (g_csrf) return setTimeout(function() { cb(g_csrf); }, 0);
// the configuration
var configuration = {
browserid: 'http://127.0.0.1:62700/'
}
// otherwise, get a new one
return http.get({
host: '127.0.0.1',
port: '62700',
path: '/wsapi/csrf',
headers: headers
}, function(res) {
extractCookies(res);
var body = "";
res.on('data', function(chunk) { body += chunk; })
.on('end', function() {
g_csrf = body;
cb(body);
});
}).on('error', function (e) {
cb();
});
exports.clearCookies = function() {
wcli.clearCookies(context);
};
exports.clearCookies = function() { cookieJar = {}; g_csrf = undefined; };
function extractCookies(res) {
if (res.headers['set-cookie']) {
res.headers['set-cookie'].forEach(function(cookie) {
var m = /^([^;]+)(?:;.*)$/.exec(cookie);
if (m) {
var x = m[1].split('=');
cookieJar[x[0]] = x[1];
}
});
}
}
// A macro for wsapi requests
exports.get = function (path, getArgs) {
return function () {
var cb = this.callback;
if (typeof getArgs === 'object')
path += "?" + querystring.stringify(getArgs);
var headers = {};
if (Object.keys(cookieJar).length) {
headers['Cookie'] = "";
for (var k in cookieJar) {
headers['Cookie'] += k + "=" + cookieJar[k];
}
}
http.get({
host: '127.0.0.1',
port: '62700',
path: path,
headers: headers
}, function(res) {
// see if there are any set-cookies that we should honor
extractCookies(res);
var body = '';
res.on('data', function(chunk) { body += chunk; })
.on('end', function() {
cb({code: res.statusCode, headers: res.headers, body: body});
});
}).on('error', function (e) {
cb();
});
wcli.get(configuration, path, context, getArgs, this.callback);
};
};
// FIXME: dedup code
// A macro for wsapi requests
// add CSRF protection to this test
exports.post = function (path, postArgs) {
return function () {
var cb = this.callback;
var headers = {
'content-type': 'application/x-www-form-urlencoded'
};
fetchCSRF(headers, function() {
if (Object.keys(cookieJar).length) {
headers['Cookie'] = "";
for (var k in cookieJar) {
headers['Cookie'] += k + "=" + cookieJar[k];
}
}
if (typeof postArgs === 'object') {
postArgs['csrf'] = g_csrf;
body = querystring.stringify(postArgs);
}
var req = http.request({
host: '127.0.0.1',
port: '62700',
path: path,
headers: headers,
method: "POST"
}, function(res) {
extractCookies(res);
var body = '';
res.on('data', function(chunk) { body += chunk; })
.on('end', function() {
cb({code: res.statusCode, headers: res.headers, body: body});
});
}).on('error', function (e) {
cb();
});
// send the POST
req.write(body);
req.end();
});
wcli.post(configuration, path, context, postArgs, this.callback);
};
};
......@@ -69,6 +69,11 @@ function extractCookies(ctx, res) {
}
}
exports.clearCookies = function(ctx) {
if (ctx && ctx.cookieJar) delete ctx.cookieJar;
if (ctx && ctx.csrf) delete ctx.csrf;
};
exports.get = function(cfg, path, context, getArgs, cb) {
// parse the server URL (cfg.browserid)
var uObj;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment