Skip to content

Instantly share code, notes, and snippets.

@designeng
Last active December 9, 2015 18:43
Show Gist options
  • Save designeng/5af79b921ec5965a3924 to your computer and use it in GitHub Desktop.
Save designeng/5af79b921ec5965a3924 to your computer and use it in GitHub Desktop.
var app = require('../../server/app.js');
var model = require('../../model.js');
var request = require('supertest');
var apitest = request(app);
describe('integration', function () {
it('model.json post should throw 500 error without args', function (done) {
apitest.post('/model.json')
.expect(500, done);
});
it('model.json should accept post request', function (done) {
apitest.post('/model.json')
.send({
method : 'call',
callPath : ['names','add'],
arguments : ['1234567']
})
.expect(200, done);
});
});
"use strict";
var url = require("url");
var parseArgs = {
"jsonGraph": true,
"callPath": true,
"arguments": true,
"pathSuffixes": true,
"paths": true
};
module.exports = function requestToContext(req) {
var queryMap = req.method === "POST" ? req.body : url.parse(req.url, true).query;
var context = {};
if (queryMap) {
Object.keys(queryMap).forEach(function(key) {
var arg = queryMap[key];
if (parseArgs[key] && arg != null && Object.prototype.toString.call(arg) !== "[object Array]") {
context[key] = JSON.parse(arg);
} else {
context[key] = arg;
}
});
}
return context;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment