2016-10-10 21:37:46 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('../helper');
|
|
|
|
var assert = require('assert');
|
|
|
|
var appServer = require('../../app/server');
|
|
|
|
|
|
|
|
function response(code) {
|
|
|
|
return {
|
|
|
|
status: code
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var RESPONSE = {
|
|
|
|
OK: response(200),
|
|
|
|
CREATED: response(201)
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function TestClient(config) {
|
|
|
|
this.config = config || {};
|
|
|
|
this.server = appServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TestClient;
|
|
|
|
|
|
|
|
|
2016-10-19 22:56:43 +08:00
|
|
|
TestClient.prototype.getResult = function(query, override, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = override;
|
|
|
|
override = {};
|
|
|
|
}
|
2016-10-10 21:37:46 +08:00
|
|
|
assert.response(
|
|
|
|
this.server,
|
|
|
|
{
|
2016-10-19 22:56:43 +08:00
|
|
|
url: this.getUrl(override),
|
2016-10-10 21:37:46 +08:00
|
|
|
headers: {
|
2016-10-19 22:56:43 +08:00
|
|
|
host: this.getHost(override),
|
2016-10-10 21:37:46 +08:00
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
data: JSON.stringify({
|
2017-08-04 01:15:24 +08:00
|
|
|
q: query,
|
|
|
|
format: this.getFormat(override)
|
2016-10-10 21:37:46 +08:00
|
|
|
})
|
|
|
|
},
|
2017-08-04 01:15:24 +08:00
|
|
|
this.getExpectedResponse(override),
|
2016-10-10 21:37:46 +08:00
|
|
|
function (err, res) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
var result = JSON.parse(res.body);
|
|
|
|
|
2017-08-04 01:15:24 +08:00
|
|
|
if (res.status > 299) {
|
|
|
|
return callback(null, result);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:37:46 +08:00
|
|
|
return callback(null, result.rows || []);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-10-19 22:56:43 +08:00
|
|
|
TestClient.prototype.getHost = function(override) {
|
|
|
|
return override.host || this.config.host || 'vizzuality.cartodb.com';
|
2016-10-10 21:37:46 +08:00
|
|
|
};
|
|
|
|
|
2016-10-19 22:56:43 +08:00
|
|
|
TestClient.prototype.getUrl = function(override) {
|
|
|
|
return '/api/v2/sql?api_key=' + (override.apiKey || this.config.apiKey || '1234');
|
2016-10-10 21:37:46 +08:00
|
|
|
};
|
2017-08-04 01:15:24 +08:00
|
|
|
|
|
|
|
TestClient.prototype.getExpectedResponse = function (override) {
|
|
|
|
return override.response || this.config.response || RESPONSE.OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestClient.prototype.getFormat = function (override) {
|
|
|
|
return override.format || this.config.format || undefined;
|
|
|
|
};
|