From 1c6cb293d4e958ec7fcec39a228cf4c1aa24bdb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 27 Feb 2019 10:09:08 +0100 Subject: [PATCH] testing log middleware --- test/acceptance/log.js | 124 ++++++++++++++++++++++++++++++ test/support/batch-test-client.js | 2 +- 2 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 test/acceptance/log.js diff --git a/test/acceptance/log.js b/test/acceptance/log.js new file mode 100644 index 00000000..75815830 --- /dev/null +++ b/test/acceptance/log.js @@ -0,0 +1,124 @@ +'use strict'; + +require('../helper'); + +const server = require('../../app/server')(); +const assert = require('../support/assert'); +const qs = require('querystring'); +const BatchTestClient = require('../support/batch-test-client'); + +const QUERY = `SELECT 14 as foo`; +const API_KEY = 1234; + +describe('Log middleware', function() { + describe('regular queries endpoint', function() { + ['GET', 'POST'].forEach(method => { + it(`${method} without query fails`, function(done) { + assert.response(server, + { + method, + url: '/api/v1/sql?' + qs.stringify({ + api_key: API_KEY + }), + headers: { + host: 'vizzuality.cartodb.com' + } + }, + { statusCode: 400 }, + function(err, res) { + assert.ok(!err); + + assert.ok(res.headers['x-sqlapi-log']); + const log = JSON.parse(res.headers['x-sqlapi-log']); + assert.deepEqual(log, { + request: { + sql: null + } + }); + + return done(); + } + ); + }); + + it(`${method} query`, function(done) { + assert.response(server, + { + method, + url: '/api/v1/sql?' + qs.stringify({ + q: QUERY, + api_key: API_KEY + }), + headers: { + host: 'vizzuality.cartodb.com' + } + }, + { statusCode: 200 }, + function(err, res) { + assert.ok(!err); + + assert.ok(res.headers['x-sqlapi-log']); + const log = JSON.parse(res.headers['x-sqlapi-log']); + assert.deepEqual(log, { + request: { + sql: { + simple: QUERY + } + } + }); + + return done(); + } + ); + }); + }); + }); + + describe('batch api queries', function() { + before(function() { + this.batchTestClient = new BatchTestClient(); + }); + + after(function(done) { + this.batchTestClient.drain(done); + }); + + it('one query', function (done) { + var payload = { query: QUERY }; + this.batchTestClient.createJob(payload, function(err, jobResult, res) { + assert.ok(!err); + + assert.ok(res.headers['x-sqlapi-log']); + const log = JSON.parse(res.headers['x-sqlapi-log']); + assert.deepEqual(log, { + request: { + sql: { + simple: QUERY + } + } + }); + + return done(); + }); + }); + + it('multiquery job with two queries', function (done) { + var payload = { query: [QUERY, QUERY] }; + this.batchTestClient.createJob(payload, function(err, jobResult, res) { + assert.ok(!err); + + assert.ok(res.headers['x-sqlapi-log']); + const log = JSON.parse(res.headers['x-sqlapi-log']); + assert.deepEqual(log, { + request: { + sql: { + multiple: [QUERY, QUERY] + } + } + }); + + return done(); + }); + }); + }); +}); diff --git a/test/support/batch-test-client.js b/test/support/batch-test-client.js index 99559ca2..3c43735a 100644 --- a/test/support/batch-test-client.js +++ b/test/support/batch-test-client.js @@ -82,7 +82,7 @@ BatchTestClient.prototype.createJob = function(job, override, callback) { } if (res.statusCode < 400) { - return callback(null, new JobResult(JSON.parse(res.body), this, override)); + return callback(null, new JobResult(JSON.parse(res.body), this, override), res); } else { return callback(null, res); }