-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from apigee/functional-tests
Adding functional tests for plugins.
- Loading branch information
Showing
8 changed files
with
345 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,5 @@ node_modules | |
|
||
.vscode | ||
.idea | ||
.DS_Store | ||
.DS_Store | ||
env.sh |
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
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
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
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
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]); | ||
}); | ||
}) |
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
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]); | ||
}); | ||
}) |
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
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]); | ||
}); | ||
}); | ||
|
||
|
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
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]); | ||
}); | ||
}); |