Last active
December 18, 2015 11:20
-
-
Save zeropaper/5775266 to your computer and use it in GitHub Desktop.
just a little utility to test some webservice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var host = 'test.domain.com'; | |
var apiUrl = 'http://'+ host +'/api/v1'; | |
var URI = require('URIjs'); | |
var async = require('async'); | |
var _ = require('underscore'); | |
var cookies = {}; | |
function data2query(data) { | |
if (!_.size(data)) { | |
return ''; | |
} | |
return URI().query(data).toString().slice(1); | |
} | |
function doCall(resourceName, method, data, done) { | |
var queryString = data2query(data); | |
var options = { | |
host: host, | |
path: '/api/v1/'+ resourceName, | |
method: method, | |
headers: {} | |
}; | |
var exited; | |
_.extend(options.headers, { | |
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8', | |
'Content-Length' : Buffer.byteLength(queryString, 'utf8') | |
}); | |
if (cookies.SX_SID) { | |
options.headers.Cookie = 'SX_SID='+ cookies.SX_SID.value; | |
} | |
var req = http.request(options, function(res) { | |
res.setEncoding('utf8'); | |
var body = ''; | |
// we add the chunk of received data to the body | |
res.on('data', function (chunk) { | |
body = body + chunk; | |
}); | |
// when the response ends... | |
res.on('end', function() { | |
// ... collect the cookies | |
if (res.headers['set-cookie']) { | |
_.each(res.headers['set-cookie'], function(cookie) { | |
var parsed = (/([^=]+)=([^;]*);( Expires=(.*);|) Path=(.*)/ig).exec(cookie); | |
if (!parsed) { | |
exited = 1; | |
return done(new Error('Could not parse the cookie: '+ cookie)); | |
} | |
cookies[parsed[1]] = { | |
value: parsed[2], | |
expires: parsed[4] ? new Date(parsed[4]) : null, | |
path: parsed[5] | |
}; | |
}); | |
} | |
// ... and we parse the complete body | |
var json; | |
if (!exited) { | |
exited = 1; | |
try { | |
json = JSON.parse(body.toString()); | |
if (json.message) { | |
console.info(method, resourceName, 'json.message', json.message); | |
} | |
done(null, { | |
statusCode: res.statusCode, | |
requestHeaders: options.headers, | |
responseHeaders: res.headers, | |
data: json | |
}); | |
} | |
catch (e) { | |
done(null, { | |
'JSON-ERROR': e.message, | |
statusCode: res.statusCode, | |
requestHeaders: options.headers, | |
responseHeaders: res.headers, | |
body: body | |
}); | |
} | |
} | |
}); | |
}); | |
req.write(queryString); | |
req.on('error', function(e) { | |
if (!exited) { | |
exited = 1; | |
done(e); | |
} | |
}); | |
req.end(); | |
} | |
cbs['POST sessions'] = function(done) { | |
doCall('sessions', 'POST', { | |
mail: '', | |
password: '' | |
}, done); | |
}; | |
cbs['GET customers'] = function(done) { | |
doCall('customers', 'GET', {}, done); | |
}; | |
cbs['GET vehicle-types'] = function(done) { | |
doCall('vehicle-types', 'GET', {}, done); | |
}; | |
async.series(cbs, function(err, results) { | |
if (err) { | |
throw err; | |
} | |
// console.info('stack completed without error', JSON.stringify(results, null, 2)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oh.. BTW... not yet finished, cookies need dome love