diff --git a/test/support/SQLAPIEmu.js b/test/support/SQLAPIEmu.js index 9bb52a9c..074e7ada 100644 --- a/test/support/SQLAPIEmu.js +++ b/test/support/SQLAPIEmu.js @@ -2,7 +2,7 @@ var http = require('http'); var url = require('url'); var _ = require('underscore'); -var o = function(port, cb) { +var SQLAPIEmulator = function(port, cb) { this.queries = []; var that = this; @@ -37,47 +37,45 @@ var o = function(port, cb) { }).listen(port, cb); }; -o.prototype.handleQuery = function(query, res) { +SQLAPIEmulator.prototype.handleQuery = function(query, res) { this.queries.push(query); if ( query.q.match('SQLAPIERROR') ) { res.statusCode = 400; res.write(JSON.stringify({'error':'Some error occurred'})); } else if ( query.q.match('SQLAPINOANSWER') ) { - console.log("SQLAPIEmulator will never respond, on request"); - return; + console.log("SQLAPIEmulator will never respond, on request"); + return; + } else if (query.q.match('tablenames')) { + var tableNames = JSON.stringify(query); + res.write(queryResult({tablenames: '{' + tableNames + '}', max: 1234567890.123})); } else if ( query.q.match('EPOCH.* as max') ) { // This is the structure of the known query sent by tiler - var row = { - 'max': 1234567890.123 - }; - res.write(JSON.stringify({rows: [ row ]})); + res.write(queryResult({max: 1234567890.123})); } else { if ( query.q.match('_private_') && query.api_key === undefined) { res.statusCode = 403; res.write(JSON.stringify({'error':'forbidden: ' + JSON.stringify(query)})); } else { var qs = JSON.stringify(query); - var row = { - // This is the structure of the known query sent by tiler - 'cdb_querytables': '{' + qs + '}', - 'max': qs - }; - var out_obj = {rows: [ row ]}; - var out = JSON.stringify(out_obj); - res.write(out); + res.write(queryResult({cdb_querytables: '{' + qs + '}', max: 1234567890.123})); } } res.end(); }; - -o.prototype.close = function(cb) { +SQLAPIEmulator.prototype.close = function(cb) { this.sqlapi_server.close(cb); }; -o.prototype.getLastRequest = function() { +SQLAPIEmulator.prototype.getLastRequest = function() { return this.requests.pop(); }; -module.exports = o; +function queryResult(row) { + return JSON.stringify({ + rows: [row] + }); +} + +module.exports = SQLAPIEmulator;