Skip to content

Commit

Permalink
Merge pull request #9 from apigee/functional-tests
Browse files Browse the repository at this point in the history
Adding functional tests for plugins.
  • Loading branch information
Matthew Dobson authored Dec 2, 2016
2 parents b649f06 + cbb6d27 commit 6f69b73
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ node_modules

.vscode
.idea
.DS_Store
.DS_Store
env.sh
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Plugins for Apige Edge Microgateway",
"main": "index.js",
"scripts": {
"test": "bin/test"
"test": "./node_modules/.bin/mocha -R spec"
},
"dependencies": {
"async": "^1.4.2",
Expand Down
6 changes: 4 additions & 2 deletions quota-memory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var Quota = require('volos-quota-memory');
var debug = require('debug')('gateway:quota');

module.exports.init = function(config, logger, stats) {

var quotas = {}; // productName -> connectMiddleware

var options = {
Expand All @@ -14,7 +13,10 @@ module.exports.init = function(config, logger, stats) {

Object.keys(config).forEach(function(productName) {
var product = config[productName];
if (!product.uri && !product.key && !product.secret && !product.allow && !product.interval) return; // skip non-quota config
if (!product.uri && !product.key && !product.secret && !product.allow && !product.interval) {
return; // skip non-quota config
}

var quota = Quota.create(config[productName]);
quotas[productName] = quota.connectMiddleware().apply(options);
debug('created quota for', productName);
Expand Down
11 changes: 9 additions & 2 deletions quota/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ module.exports.init = function(config, logger, stats) {
var quotas = {}; // productName -> connectMiddleware

var options = {
key: function(req) { return req.token.application_name; }
key: function(req) {
return req.token.application_name;
}
};

Object.keys(config).forEach(function(productName) {
var product = config[productName];
if (!product.uri && !product.key && !product.secret && !product.allow && !product.interval) return; // skip non-quota config
if (!product.uri && !product.key && !product.secret && !product.allow && !product.interval) {
// skip non-quota config
console.log('Not enough info for configuring quota');
return;
}

var quota = Quota.create(config[productName]);
quotas[productName] = quota.connectMiddleware().apply(options);
debug('created quota for', productName);
Expand Down
94 changes: 94 additions & 0 deletions test/accumulate-request-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const accumulateRequest = require('../accumulate-request/index');
const assert = require('assert');

describe('accumulate request plugin', () => {
var plugin = null;

beforeEach(() => {
var config = {};
var logger = {};
var stats = {};

plugin = accumulateRequest.init.apply(null, [config, logger, stats]);
});

it('exposes an ondata_request handler', () => {
assert.ok(plugin.ondata_request);
});

it('exposes an onend_request handler', () => {
assert.ok(plugin.onend_request);
});

it('calls back with two null function arguments in the ondata_request handler', (done) => {
var cb = (err, result) => {
assert.equal(err, null);
assert.equal(result, null);
done();
}


plugin.ondata_request.apply(null, [{}, {}, Buffer.alloc(5, 'a'), cb]);
});

it('will collect all buffers provided to ondata_request handler, concatenate them, and return them as a single buffer', (done) => {
var desiredResult = 'aaaaaaaaaaaaaaa';

var ondata_cb = (err, result) => {
assert.equal(err, null);
assert.equal(result, null);
assert.ok(req._chunks);
}

var onend_cb = (err, result) => {
assert.equal(err, null);
assert.equal(result.toString(), desiredResult);
done();
}

var req = {};

plugin.ondata_request.apply(null, [req, {}, Buffer.alloc(5, 'a'), ondata_cb]);
plugin.ondata_request.apply(null, [req, {}, Buffer.alloc(5, 'a'), ondata_cb]);
plugin.ondata_request.apply(null, [req, {}, Buffer.alloc(5, 'a'), ondata_cb]);

plugin.onend_request.apply(null, [req, {}, null, onend_cb]);
});

it('will append data included in the end call to the buffer', (done) => {
var desiredResult = 'aaaaaaaaaaaaaaaaaaaa';

var ondata_cb = (err, result) => {
assert.equal(err, null);
assert.equal(result, null);
assert.ok(req._chunks);
}

var onend_cb = (err, result) => {
assert.equal(err, null);
assert.equal(result.toString(), desiredResult);
done();
}

var req = {};

plugin.ondata_request.apply(null, [req, {}, Buffer.alloc(5, 'a'), ondata_cb]);
plugin.ondata_request.apply(null, [req, {}, Buffer.alloc(5, 'a'), ondata_cb]);
plugin.ondata_request.apply(null, [req, {}, Buffer.alloc(5, 'a'), ondata_cb]);

plugin.onend_request.apply(null, [req, {}, Buffer.alloc(5, 'a'), onend_cb]);
});

it('will create a req._chunks object on the request object', (done) => {
var req = {};
var cb = (err, result) => {
assert.equal(err, null);
assert.equal(result, null);
assert.ok(req._chunks);
assert.equal(req._chunks.toString(), 'aaaaa');
done();
}

plugin.ondata_request.apply(null, [req, {}, Buffer.alloc(5, 'a'), cb]);
});
})
94 changes: 94 additions & 0 deletions test/accumulate-response-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const accumulateResponse = require('../accumulate-response/index');
const assert = require('assert');

describe('accumulate response plugin', () => {
var plugin = null;

beforeEach(() => {
var config = {};
var logger = {};
var stats = {};

plugin = accumulateResponse.init.apply(null, [config, logger, stats]);
});

it('exposes an ondata_response handler', () => {
assert.ok(plugin.ondata_response);
});

it('exposes an onend_response handler', () => {
assert.ok(plugin.onend_response);
});

it('calls back with two null function arguments in the ondata_response handler', (done) => {
var cb = (err, result) => {
assert.equal(err, null);
assert.equal(result, null);
done();
}


plugin.ondata_response.apply(null, [{}, {}, Buffer.alloc(5, 'a'), cb]);
});

it('will collect all buffers provided to ondata_response handler, concatenate them, and return them as a single buffer', (done) => {
var desiredResult = 'aaaaaaaaaaaaaaa';

var ondata_cb = (err, result) => {
assert.equal(err, null);
assert.equal(result, null);
assert.ok(res._chunks);
}

var onend_cb = (err, result) => {
assert.equal(err, null);
assert.equal(result.toString(), desiredResult);
done();
}

var res = {};

plugin.ondata_response.apply(null, [{}, res, Buffer.alloc(5, 'a'), ondata_cb]);
plugin.ondata_response.apply(null, [{}, res, Buffer.alloc(5, 'a'), ondata_cb]);
plugin.ondata_response.apply(null, [{}, res, Buffer.alloc(5, 'a'), ondata_cb]);

plugin.onend_response.apply(null, [{}, res, null, onend_cb]);
});

it('will append data included in the end call to the buffer', (done) => {
var desiredResult = 'aaaaaaaaaaaaaaaaaaaa';

var ondata_cb = (err, result) => {
assert.equal(err, null);
assert.equal(result, null);
assert.ok(res._chunks);
}

var onend_cb = (err, result) => {
assert.equal(err, null);
assert.equal(result.toString(), desiredResult);
done();
}

var res = {};

plugin.ondata_response.apply(null, [{}, res, Buffer.alloc(5, 'a'), ondata_cb]);
plugin.ondata_response.apply(null, [{}, res, Buffer.alloc(5, 'a'), ondata_cb]);
plugin.ondata_response.apply(null, [{}, res, Buffer.alloc(5, 'a'), ondata_cb]);

plugin.onend_response.apply(null, [{}, res, Buffer.alloc(5, 'a'), onend_cb]);
});

it('will create a req._chunks object on the request object', (done) => {
var res = {};
var cb = (err, result) => {
assert.equal(err, null);
assert.equal(result, null);
assert.ok(res._chunks);
assert.equal(res._chunks.toString(), 'aaaaa');
done();
}

plugin.ondata_response.apply(null, [{}, res, Buffer.alloc(5, 'a'), cb]);
});
})
93 changes: 93 additions & 0 deletions test/quota-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const quota = require('../quota/index');
const assert = require('assert');

var exampleConfig = {
EdgeMicroTestProduct: {
allow: process.env.QUOTA_ALLOW,
interval: process.env.QUOTA_INTERVAL,
timeUnit: process.env.QUOTA_TIMEUNIT,
bufferSize: process.env.QUOTA_BUFFERSIZE,
uri: process.env.QUOTA_URI,
key: process.env.QUOTA_KEY,
secret: process.env.QUOTA_SECRET
}
}

describe('quota plugin', () => {
var plugin = null;

beforeEach(() => {
var logger = {};
var stats = {};

plugin = quota.init.apply(null, [exampleConfig, logger, stats]);

});

it('exposes an onrequest handler', () => {
assert.ok(plugin.onrequest);
});

it('will quota limit after 3 API calls', (done) => {
var count = 0;
var onrequest_cb = (err) => {
count++;
if(count == 4) {
assert.equal(count, 4);
assert.equal(err.message, 'exceeded quota');
done();
}
};

var req = {
token: {
application_name: '0e7762f4-ea67-4cc1-ae4a-21598c35b18f',
api_product_list: ['EdgeMicroTestProduct']
}
}

var res = {
headers: {},
setHeader: (key, val) => {
res.headers[key] = val;
}
}

plugin.onrequest.apply(null, [req, res, onrequest_cb]);
plugin.onrequest.apply(null, [req, res, onrequest_cb]);
plugin.onrequest.apply(null, [req, res, onrequest_cb]);
plugin.onrequest.apply(null, [req, res, onrequest_cb]);
});

it('will not quota limit before 3 API calls', (done) => {
var count = 0;
var onrequest_cb = (err) => {
count++;
if(count == 3) {
assert.equal(count, 3);
assert.ok(!(err instanceof Error));
done();
}
};

var req = {
token: {
application_name: '0e7762f4-ea67-4cc1-ae4a-21598c35b18f',
api_product_list: ['EdgeMicroTestProduct']
}
}

var res = {
headers: {},
setHeader: (key, val) => {
res.headers[key] = val;
}
}

plugin.onrequest.apply(null, [req, res, onrequest_cb]);
plugin.onrequest.apply(null, [req, res, onrequest_cb]);
plugin.onrequest.apply(null, [req, res, onrequest_cb]);
});
});


48 changes: 48 additions & 0 deletions test/spike-arrest-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const spikeArrest = require('../spikearrest/index');
const assert = require('assert');

describe('spike arrest plugin', () => {
var plugin = null;

beforeEach(() => {
var config = {
timeUnit: 'minute',
bufferSize: 0,
allow: 1
};
var logger = {};
var stats = {};

plugin = spikeArrest.init.apply(null, [config, logger, stats]);
});

it('exposes an onrequest handler', () => {
assert.ok(plugin.onrequest);
});

it('will accept a request', (done) => {
var onrequest_cb = () => {
done();
};

var req = {};
var res = {};
plugin.onrequest.apply(null, [req, res, onrequest_cb]);
});

it('will reject a requests that come in too fast', (done) => {
var onrequest_cb = () => {
};

var onrequest_cb_second = (req, res, next) => {
assert.ok(req instanceof Error);
assert.equal(req.message, 'SpikeArrest engaged');
done();
};

var req = {};
var res = {};
plugin.onrequest.apply(null, [req, res, onrequest_cb]);
plugin.onrequest.apply(null, [req, res, onrequest_cb_second]);
});
});

0 comments on commit 6f69b73

Please sign in to comment.