From b4d7ee7dfb3891c5d00343e791a76a09e544cc7b Mon Sep 17 00:00:00 2001 From: Linjie Peng Date: Tue, 11 Aug 2015 15:56:02 -0700 Subject: [PATCH] add the load testing feature into module --- README.md | 2 + index.js | 34 +- templates/outerDescribe.handlebars | 6 +- templates/request/delete/delete.handlebars | 4 +- templates/request/get/get.handlebars | 66 ++- templates/request/head/head.handlebars | 4 +- templates/request/patch/patch.handlebars | 93 +++- templates/request/post/post.handlebars | 93 +++- templates/request/put/put.handlebars | 93 +++- templates/supertest/delete/delete.handlebars | 2 +- templates/supertest/get/get.handlebars | 75 ++- templates/supertest/head/head.handlebars | 2 +- .../supertest/options/options.handlebars | 75 ++- templates/supertest/patch/patch.handlebars | 95 +++- templates/supertest/post/post.handlebars | 95 +++- templates/supertest/put/put.handlebars | 95 +++- test/loadTest/compare/request/assert/.env | 4 + .../compare/request/assert/base-path-test.js | 230 ++++++++ .../compare/request/assert/user-test.js | 495 ++++++++++++++++++ test/loadTest/compare/request/expect/.env | 4 + .../compare/request/expect/base-path-test.js | 230 ++++++++ .../compare/request/expect/user-test.js | 375 +++++++++++++ test/loadTest/compare/request/should/.env | 4 + .../compare/request/should/base-path-test.js | 231 ++++++++ .../compare/request/should/user-test.js | 394 ++++++++++++++ test/loadTest/compare/supertest/assert/.env | 4 + .../supertest/assert/base-path-test.js | 201 +++++++ .../compare/supertest/assert/user-test.js | 406 ++++++++++++++ test/loadTest/compare/supertest/expect/.env | 4 + .../supertest/expect/base-path-test.js | 201 +++++++ .../compare/supertest/expect/user-test.js | 298 +++++++++++ test/loadTest/compare/supertest/should/.env | 4 + .../supertest/should/base-path-test.js | 202 +++++++ .../compare/supertest/should/user-test.js | 317 +++++++++++ test/loadTest/swagger.json | 284 ++++++++++ test/loadTest/test.js | 301 +++++++++++ test/pathParam/compare/output1-{id}-test.js | 1 - test/pathParam/compare/output2-{id}-test.js | 1 - 38 files changed, 4994 insertions(+), 31 deletions(-) create mode 100644 test/loadTest/compare/request/assert/.env create mode 100644 test/loadTest/compare/request/assert/base-path-test.js create mode 100644 test/loadTest/compare/request/assert/user-test.js create mode 100644 test/loadTest/compare/request/expect/.env create mode 100644 test/loadTest/compare/request/expect/base-path-test.js create mode 100644 test/loadTest/compare/request/expect/user-test.js create mode 100644 test/loadTest/compare/request/should/.env create mode 100644 test/loadTest/compare/request/should/base-path-test.js create mode 100644 test/loadTest/compare/request/should/user-test.js create mode 100644 test/loadTest/compare/supertest/assert/.env create mode 100644 test/loadTest/compare/supertest/assert/base-path-test.js create mode 100644 test/loadTest/compare/supertest/assert/user-test.js create mode 100644 test/loadTest/compare/supertest/expect/.env create mode 100644 test/loadTest/compare/supertest/expect/base-path-test.js create mode 100644 test/loadTest/compare/supertest/expect/user-test.js create mode 100644 test/loadTest/compare/supertest/should/.env create mode 100644 test/loadTest/compare/supertest/should/base-path-test.js create mode 100644 test/loadTest/compare/supertest/should/user-test.js create mode 100644 test/loadTest/swagger.json create mode 100644 test/loadTest/test.js diff --git a/README.md b/README.md index 919ae50..a359740 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ var config = { assertionFormat: 'should', testModule: 'supertest', pathNames: ['/user', '/user/{id}'], + loadTest: [{pathName:'/user', operation:'get', load:{requests: 1000, concurrent: 100}}, { /* ... */ }], maxLen: 80 }; @@ -36,6 +37,7 @@ testGen(swagger, config); * **`assertionFormat`** *required*: One of `should`, `expect` or `assert`. Choose which assertion method should be used in output test code. * **`testModule`** *required*: One of `supertest` or `request`. Choose between direct API calls (`request`) vs. programatic access to your API (`supertest`). * **`pathNames`** *required*: List of path names available in your Swagger API spec used to generate tests for. Empty array leads to **all paths**. +* **`loadTest`** *optional*: List of objects info in your Swagger API spec used to generate stress tests. If specify, pathName & operation are **required**. Optional fields requests defaults to `1000`, concurrent defaults to `100`. * **`maxLen`** *optional*: Maximum line length. Defaults to `80`. #### Return value diff --git a/index.js b/index.js index 4775cb4..717ea6f 100644 --- a/index.js +++ b/index.js @@ -74,9 +74,30 @@ function getData(swagger, path, operation, response, config, info) { queryApiKey: null, headerApiKey: null, headerSecurity: null, - path: '' + path: '', + isLoadTest: false, + loadName: '', + requests: 0, + concurrent: 0 }; + // cope with loadTest info + if (info.loadTest != null) { + _.forEach(info.loadTest, function(loadTestParam) { + if (loadTestParam.pathName === path + && loadTestParam.operation === operation) { + data.loadName = path.replace(/\//g, '_') + + '_' + operation + '_load_test'; + info.importArete = true; + data.isLoadTest = true; + data.requests = loadTestParam.load.requests !== undefined ? + loadTestParam.load.requests : 1000; + data.concurrent = loadTestParam.load.concurrent !== undefined ? + loadTestParam.load.concurrent : 100; + } + }); + } + // deal with the security properties if (info.security && info.security.length !== 0) { Object.keys(info.security[0]).forEach(function(element) { @@ -336,11 +357,17 @@ function testGenPath(swagger, path, config) { var info = { importValidator: false, importEnv: false, + importArete: false, consumes: [], produces: [], - security: [] + security: [], + loadTest: null }; + if (config.loadTest) { + info.loadTest = config.loadTest; + } + source = read(join(__dirname, '/templates/outerDescribe.handlebars'), 'utf8'); outerDescribeFn = handlebars.compile(source, {noEscape: true}); @@ -361,7 +388,8 @@ function testGenPath(swagger, path, config) { host: (swagger.host !== undefined ? swagger.host : 'localhost:10010'), tests: result, importValidator: info.importValidator, - importEnv: info.importEnv + importEnv: info.importEnv, + importArete: info.importArete }; if (!allDeprecated) { diff --git a/templates/outerDescribe.handlebars b/templates/outerDescribe.handlebars index 352bc4a..b2795ea 100644 --- a/templates/outerDescribe.handlebars +++ b/templates/outerDescribe.handlebars @@ -19,9 +19,13 @@ chai.should(); {{#is assertion 'assert'}} var assert = chai.assert; {{/is}} +{{#if importArete}} +var arete = require('arete'); +{{/if}} {{#if importEnv}} -require('dotenv').load();{{/if}} +require('dotenv').load(); +{{/if}} describe('{{description}}', function() { {{#each tests}} diff --git a/templates/request/delete/delete.handlebars b/templates/request/delete/delete.handlebars index 292fa1f..0fc775a 100644 --- a/templates/request/delete/delete.handlebars +++ b/templates/request/delete/delete.handlebars @@ -16,9 +16,7 @@ method: 'DELETE', headers: { 'Content-Type': '{{contentType}}'{{#if headerParameters}}, - 'Custom-Header': { {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}} - }{{/if}}{{#if headerApiKey}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} } diff --git a/templates/request/get/get.handlebars b/templates/request/get/get.handlebars index abb4349..b3c6fd3 100644 --- a/templates/request/get/get.handlebars +++ b/templates/request/get/get.handlebars @@ -16,9 +16,7 @@ method: 'GET', headers: { 'Content-Type': '{{contentType}}'{{#if headerParameters}}, - 'Custom-Header': { {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}} - }{{/if}}{{#if headerApiKey}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} } @@ -72,3 +70,65 @@ done(); }); }); +{{#if isLoadTest}} + it('load tests with {{length description}}', function(done) { + arete.loadTest({ + name: '{{loadName}}', + requests: {{requests}}, + concurrentRequests: {{concurrent}}, + targetFunction: function(callback) { + request({ + url: '{{pathify path}}', + {{#ifCond queryParameters queryApiKey}} + qs: { + {{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}}, + {{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}} + }, + {{/ifCond}} + method: 'GET', + headers: { + 'Content-Type': '{{contentType}}'{{#if headerParameters}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, + {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, + Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + {{#is assertion 'expect'}} + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'should'}} + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'assert'}} + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + {{/is}} + done(); + } + }); + }); +{{/if}} diff --git a/templates/request/head/head.handlebars b/templates/request/head/head.handlebars index 0e7c2dd..a99827b 100644 --- a/templates/request/head/head.handlebars +++ b/templates/request/head/head.handlebars @@ -16,9 +16,7 @@ method: 'HEAD', headers: { 'Content-Type': '{{contentType}}'{{#if headerParameters}}, - 'Custom-Header': { {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}} - }{{/if}}{{#if headerApiKey}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} } diff --git a/templates/request/patch/patch.handlebars b/templates/request/patch/patch.handlebars index 45d002f..6e4d183 100644 --- a/templates/request/patch/patch.handlebars +++ b/templates/request/patch/patch.handlebars @@ -15,9 +15,7 @@ {{/ifCond}} method: 'PATCH', headers: { 'Content-Type': '{{contentType}}'{{#if headerParameters}}, - 'Custom-Header': { {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}} - }{{/if}}{{#if headerApiKey}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} }, @@ -98,3 +96,92 @@ done(); }); }); +{{#if isLoadTest}} + it('load tests with {{length description}}', function(done) { + arete.loadTest({ + name: '{{loadName}}', + requests: {{requests}}, + concurrentRequests: {{concurrent}}, + targetFunction: function(callback) { + request({ + url: '{{pathify path}}', + {{#ifCond queryParameters queryApiKey}} + qs: { + {{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}}, + {{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}} + }, + {{/ifCond}} + method: 'PATCH', + headers: { + 'Content-Type': '{{contentType}}'{{#if headerParameters}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, + {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, + Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} + }, + {{#is contentType 'application/json'}} + json: { + {{#each bodyParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + } + }, + {{/is}} + {{#is contentType 'application/x-www-form-urlencoded'}} + form: { + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + } + }, + {{/is}} + {{#is contentType 'multipart/form-data'}} + form: { + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + } + }, + {{/is}} + {{#is contentType 'application/xml'}} + body: 'XML STRING GOES HERE' + }, + {{/is}} + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + {{#is assertion 'expect'}} + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'should'}} + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'assert'}} + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + {{/is}} + done(); + } + }); + }); +{{/if}} diff --git a/templates/request/post/post.handlebars b/templates/request/post/post.handlebars index 440fe1a..f873e71 100644 --- a/templates/request/post/post.handlebars +++ b/templates/request/post/post.handlebars @@ -16,9 +16,7 @@ method: 'POST', headers: { 'Content-Type': '{{contentType}}'{{#if headerParameters}}, - 'Custom-Header': { {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}} - }{{/if}}{{#if headerApiKey}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} }, @@ -99,3 +97,92 @@ done(); }); }); +{{#if isLoadTest}} + it('load tests with {{length description}}', function(done) { + arete.loadTest({ + name: '{{loadName}}', + requests: {{requests}}, + concurrentRequests: {{concurrent}}, + targetFunction: function(callback) { + request({ + url: '{{pathify path}}', + {{#ifCond queryParameters queryApiKey}} + qs: { + {{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}}, + {{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}} + }, + {{/ifCond}} + method: 'POST', + headers: { + 'Content-Type': '{{contentType}}'{{#if headerParameters}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, + {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, + Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} + }, + {{#is contentType 'application/json'}} + json: { + {{#each bodyParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + } + }, + {{/is}} + {{#is contentType 'application/x-www-form-urlencoded'}} + form: { + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + } + }, + {{/is}} + {{#is contentType 'multipart/form-data'}} + form: { + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + } + }, + {{/is}} + {{#is contentType 'application/xml'}} + body: 'XML STRING GOES HERE' + }, + {{/is}} + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + {{#is assertion 'expect'}} + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'should'}} + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'assert'}} + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + {{/is}} + done(); + } + }); + }); +{{/if}} diff --git a/templates/request/put/put.handlebars b/templates/request/put/put.handlebars index f693fc1..5aaa786 100644 --- a/templates/request/put/put.handlebars +++ b/templates/request/put/put.handlebars @@ -16,9 +16,7 @@ method: 'PUT', headers: { 'Content-Type': '{{contentType}}'{{#if headerParameters}}, - 'Custom-Header': { {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}} - }{{/if}}{{#if headerApiKey}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} }, @@ -99,3 +97,92 @@ done(); }); }); +{{#if isLoadTest}} + it('load tests with {{length description}}', function(done) { + arete.loadTest({ + name: '{{loadName}}', + requests: {{requests}}, + concurrentRequests: {{concurrent}}, + targetFunction: function(callback) { + request({ + url: '{{pathify path}}', + {{#ifCond queryParameters queryApiKey}} + qs: { + {{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}}, + {{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}} + }, + {{/ifCond}} + method: 'PUT', + headers: { + 'Content-Type': '{{contentType}}'{{#if headerParameters}}, + {{#each headerParameters}}'{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}}{{#if headerApiKey}}, + {{headerApiKey.type}}: process.env.{{headerApiKey.name}}{{/if}}{{#if headerSecurity}}, + Authorization: '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}{{/if}} + }, + {{#is contentType 'application/json'}} + json: { + {{#each bodyParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + } + }, + {{/is}} + {{#is contentType 'application/x-www-form-urlencoded'}} + form: { + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + } + }, + {{/is}} + {{#is contentType 'multipart/form-data'}} + form: { + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + } + }, + {{/is}} + {{#is contentType 'application/xml'}} + body: 'XML STRING GOES HERE' + }, + {{/is}} + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + {{#is assertion 'expect'}} + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'should'}} + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'assert'}} + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + {{/is}} + done(); + } + }); + }); +{{/if}} diff --git a/templates/supertest/delete/delete.handlebars b/templates/supertest/delete/delete.handlebars index 68f2962..59bebcc 100644 --- a/templates/supertest/delete/delete.handlebars +++ b/templates/supertest/delete/delete.handlebars @@ -20,7 +20,7 @@ {{#if headerParameters}} .set({ {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} {{/each}} }) {{/if}} diff --git a/templates/supertest/get/get.handlebars b/templates/supertest/get/get.handlebars index 0d015f7..0d957ec 100644 --- a/templates/supertest/get/get.handlebars +++ b/templates/supertest/get/get.handlebars @@ -20,7 +20,7 @@ {{#if headerParameters}} .set({ {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} {{/each}} }) {{/if}} @@ -59,3 +59,76 @@ done(); }); }); +{{#if isLoadTest}} + it('load tests with {{length description}}', function(done) { + arete.loadTest({ + name: '{{loadName}}', + requests: {{requests}}, + concurrentRequests: {{concurrent}}, + targetFunction: function(callback) { + api.get('{{pathify path}}') + {{#ifCond queryParameters queryApiKey}} + .query({ + {{#if queryApiKey}} + {{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}}, + {{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}} + }) + {{/ifCond}} + {{#if headerSecurity}} + .set('Authorization', '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}) + {{/if}} + .set('Accept', '{{contentType}}') + {{#if headerParameters}} + .set({ + {{#each headerParameters}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/if}} + {{#if headerApiKey}} + .set('{{headerApiKey.type}}', process.env.{{headerApiKey.name}}) + {{/if}} + {{#if default}} + .expect('DEFAULT RESPONSE CODE HERE') + {{else}} + .expect({{responseCode}}) + {{/if}} + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + {{#is assertion 'expect'}} + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'should'}} + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'assert'}} + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + {{/is}} + done(); + } + }); + }); +{{/if}} diff --git a/templates/supertest/head/head.handlebars b/templates/supertest/head/head.handlebars index c1c67ba..3236303 100644 --- a/templates/supertest/head/head.handlebars +++ b/templates/supertest/head/head.handlebars @@ -20,7 +20,7 @@ {{#if headerParameters}} .set({ {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} {{/each}} }) {{/if}} diff --git a/templates/supertest/options/options.handlebars b/templates/supertest/options/options.handlebars index bd12a97..002d63b 100644 --- a/templates/supertest/options/options.handlebars +++ b/templates/supertest/options/options.handlebars @@ -20,7 +20,7 @@ {{#if headerParameters}} .set({ {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} {{/each}} }) {{/if}} @@ -59,3 +59,76 @@ done(); }); }); +{{#if isLoadTest}} + it('load tests with {{length description}}', function(done) { + arete.loadTest({ + name: '{{loadName}}', + requests: {{requests}}, + concurrentRequests: {{concurrent}}, + targetFunction: function(callback) { + api.options('{{pathify path}}') + {{#ifCond queryParameters queryApiKey}} + .query({ + {{#if queryApiKey}} + {{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}}, + {{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}} + }) + {{/ifCond}} + {{#if headerSecurity}} + .set('Authorization', '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}) + {{/if}} + .set('Accept', '{{contentType}}') + {{#if headerParameters}} + .set({ + {{#each headerParameters}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/if}} + {{#if headerApiKey}} + .set('{{headerApiKey.type}}', process.env.{{headerApiKey.name}}) + {{/if}} + {{#if default}} + .expect('DEFAULT RESPONSE CODE HERE') + {{else}} + .expect({{responseCode}}) + {{/if}} + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + {{#is assertion 'expect'}} + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'should'}} + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'assert'}} + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + {{/is}} + done(); + } + }); + }); +{{/if}} diff --git a/templates/supertest/patch/patch.handlebars b/templates/supertest/patch/patch.handlebars index b06b769..5abf174 100644 --- a/templates/supertest/patch/patch.handlebars +++ b/templates/supertest/patch/patch.handlebars @@ -20,7 +20,7 @@ {{#if headerParameters}} .set({ {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} {{/each}} }) {{/if}} @@ -80,3 +80,96 @@ done(); }); }); +{{#if isLoadTest}} + it('load tests with {{length description}}', function(done) { + arete.loadTest({ + name: '{{loadName}}', + requests: {{requests}}, + concurrentRequests: {{concurrent}}, + targetFunction: function(callback) { + api.patch('{{pathify path}}') + {{#ifCond queryParameters queryApiKey}} + .query({ + {{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}}, + {{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}} + }) + {{/ifCond}} + {{#if headerSecurity}} + .set('Authorization', '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}) + {{/if}} + .set('Accept', '{{contentType}}') + {{#if headerParameters}} + .set({ + {{#each headerParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/if}} + {{#if headerApiKey}} + .set('{{headerApiKey.type}}', process.env.{{headerApiKey.name}}) + {{/if}} + {{#is contentType 'application/json'}} + .send({ + {{#each bodyParameters}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/is}} + {{#is contentType 'application/x-www-form-urlencoded'}} + .send({ + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/is}} + {{#is contentType 'multipart/form-data'}} + .send({ + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/is}} + {{#if default}} + .expect('DEFAULT RESPONSE CODE HERE') + {{else}} + .expect({{responseCode}}) + {{/if}} + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + {{#is assertion 'expect'}} + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'should'}} + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'assert'}} + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + {{/is}} + done(); + } + }); + }); +{{/if}} diff --git a/templates/supertest/post/post.handlebars b/templates/supertest/post/post.handlebars index ce70700..e7b4c5d 100644 --- a/templates/supertest/post/post.handlebars +++ b/templates/supertest/post/post.handlebars @@ -19,7 +19,7 @@ {{#if headerParameters}} .set({ {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} {{/each}} }) {{/if}} @@ -79,3 +79,96 @@ done(); }); }); +{{#if isLoadTest}} + it('load tests with {{length description}}', function(done) { + arete.loadTest({ + name: '{{loadName}}', + requests: {{requests}}, + concurrentRequests: {{concurrent}}, + targetFunction: function(callback) { + api.post('{{pathify path}}') + {{#ifCond queryParameters queryApiKey}} + .query({ + {{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}}, + {{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}} + }) + {{/ifCond}} + {{#if headerSecurity}} + .set('Authorization', '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}) + {{/if}} + .set('Accept', '{{contentType}}') + {{#if headerParameters}} + .set({ + {{#each headerParameters}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/if}} + {{#if headerApiKey}} + .set('{{headerApiKey.type}}', process.env.{{headerApiKey.name}}) + {{/if}} + {{#is contentType 'application/json'}} + .send({ + {{#each bodyParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/is}} + {{#is contentType 'application/x-www-form-urlencoded'}} + .send({ + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/is}} + {{#is contentType 'multipart/form-data'}} + .send({ + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/is}} + {{#if default}} + .expect('DEFAULT RESPONSE CODE HERE') + {{else}} + .expect({{responseCode}}) + {{/if}} + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + {{#is assertion 'expect'}} + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'should'}} + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'assert'}} + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + {{/is}} + done(); + } + }); + }); +{{/if}} diff --git a/templates/supertest/put/put.handlebars b/templates/supertest/put/put.handlebars index 8e98e45..c7345d6 100644 --- a/templates/supertest/put/put.handlebars +++ b/templates/supertest/put/put.handlebars @@ -19,7 +19,7 @@ {{#if headerParameters}} .set({ {{#each headerParameters}} - {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} {{/each}} }) {{/if}} @@ -79,3 +79,96 @@ done(); }); }); +{{#if isLoadTest}} + it('load tests with {{length description}}', function(done) { + arete.loadTest({ + name: '{{loadName}}', + requests: {{requests}}, + concurrentRequests: {{concurrent}}, + targetFunction: function(callback) { + api.put('{{pathify path}}') + {{#ifCond queryParameters queryApiKey}} + .query({ + {{#if queryApiKey}}{{queryApiKey.type}}: process.env.{{queryApiKey.name}}{{#if queryParameters}}, + {{/if}}{{/if}}{{#if queryParameters}}{{#each queryParameters}}{{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}}{{/each}}{{/if}} + }) + {{/ifCond}} + {{#if headerSecurity}} + .set('Authorization', '{{headerSecurity.type}} ' + process.env.{{headerSecurity.name}}) + {{/if}} + .set('Accept', '{{contentType}}') + {{#if headerParameters}} + .set({ + {{#each headerParameters}} + '{{this.name}}': 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/if}} + {{#if headerApiKey}} + .set('{{headerApiKey.type}}', process.env.{{headerApiKey.name}}) + {{/if}} + {{#is contentType 'application/json'}} + .send({ + {{#each bodyParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/is}} + {{#is contentType 'application/x-www-form-urlencoded'}} + .send({ + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/is}} + {{#is contentType 'multipart/form-data'}} + .send({ + {{#each formParameters}} + {{this.name}}: 'DATA GOES HERE'{{#unless @last}},{{/unless}} + {{/each}} + }) + {{/is}} + {{#if default}} + .expect('DEFAULT RESPONSE CODE HERE') + {{else}} + .expect({{responseCode}}) + {{/if}} + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + {{#is assertion 'expect'}} + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'should'}} + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + {{/is}} + {{#is assertion 'assert'}} + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + {{/is}} + done(); + } + }); + }); +{{/if}} diff --git a/test/loadTest/compare/request/assert/.env b/test/loadTest/compare/request/assert/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/loadTest/compare/request/assert/.env @@ -0,0 +1,4 @@ +OAUTH=YOUR_TOKEN_GOES_HERE +KEY=YOUR_TOKEN_GOES_HERE +BASIC_AUTH=YOUR_TOKEN_GOES_HERE + diff --git a/test/loadTest/compare/request/assert/base-path-test.js b/test/loadTest/compare/request/assert/base-path-test.js new file mode 100644 index 0000000..971599e --- /dev/null +++ b/test/loadTest/compare/request/assert/base-path-test.js @@ -0,0 +1,230 @@ +'use strict'; +var chai = require('chai'); +var ZSchema = require('z-schema'); +var validator = new ZSchema({}); +var request = require('request'); +var assert = chai.assert; + +require('dotenv').load(); + +describe('/', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + accessToken: process.env.KEY + }, + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 200); + + assert.true(validator.validate(body, schema)); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "meta": "string", + "data": "number" + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + accessToken: process.env.KEY + }, + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 400); + + assert.true(validator.validate(body, schema)); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "properties": { + "meta": "string", + "data": "number", + "UserObj": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + accessToken: process.env.KEY + }, + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 500); + + assert.true(validator.validate(body, schema)); + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 200); + + assert.true(validator.validate(body, schema)); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "number" + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 400); + + assert.true(validator.validate(body, schema)); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "type": "string" + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 500); + + assert.true(validator.validate(body, schema)); + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/request/assert/user-test.js b/test/loadTest/compare/request/assert/user-test.js new file mode 100644 index 0000000..7f2e52b --- /dev/null +++ b/test/loadTest/compare/request/assert/user-test.js @@ -0,0 +1,495 @@ +'use strict'; +var chai = require('chai'); +var request = require('request'); +var assert = chai.assert; +var arete = require('arete'); + +require('dotenv').load(); + +describe('/user', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 200); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + it('load tests with 200 OK', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 400); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + it('load tests with 400 NOT OK', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 500); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + it('load tests with 500 SERVER ERROR', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 200); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 400); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 500); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + + }); + + describe('put', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 200); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + it('load tests with 200 OK', function(done) { + arete.loadTest({ + name: '_user_put_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 400); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + it('load tests with 400 NOT OK', function(done) { + arete.loadTest({ + name: '_user_put_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 500); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + it('load tests with 500 SERVER ERROR', function(done) { + arete.loadTest({ + name: '_user_put_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + }); + + describe('patch', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 200); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + + }); + + describe('delete', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 200); + + assert.isNull(body); // non-json response or no schema + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/request/expect/.env b/test/loadTest/compare/request/expect/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/loadTest/compare/request/expect/.env @@ -0,0 +1,4 @@ +OAUTH=YOUR_TOKEN_GOES_HERE +KEY=YOUR_TOKEN_GOES_HERE +BASIC_AUTH=YOUR_TOKEN_GOES_HERE + diff --git a/test/loadTest/compare/request/expect/base-path-test.js b/test/loadTest/compare/request/expect/base-path-test.js new file mode 100644 index 0000000..ace8aeb --- /dev/null +++ b/test/loadTest/compare/request/expect/base-path-test.js @@ -0,0 +1,230 @@ +'use strict'; +var chai = require('chai'); +var ZSchema = require('z-schema'); +var validator = new ZSchema({}); +var request = require('request'); +var expect = chai.expect; + +require('dotenv').load(); + +describe('/', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + accessToken: process.env.KEY + }, + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(200); + + expect(validator.validate(body, schema)).to.be.true; + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "meta": "string", + "data": "number" + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + accessToken: process.env.KEY + }, + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(400); + + expect(validator.validate(body, schema)).to.be.true; + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "properties": { + "meta": "string", + "data": "number", + "UserObj": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + accessToken: process.env.KEY + }, + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(500); + + expect(validator.validate(body, schema)).to.be.true; + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(200); + + expect(validator.validate(body, schema)).to.be.true; + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "number" + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(400); + + expect(validator.validate(body, schema)).to.be.true; + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "type": "string" + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(500); + + expect(validator.validate(body, schema)).to.be.true; + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/request/expect/user-test.js b/test/loadTest/compare/request/expect/user-test.js new file mode 100644 index 0000000..fca7dda --- /dev/null +++ b/test/loadTest/compare/request/expect/user-test.js @@ -0,0 +1,375 @@ +'use strict'; +var chai = require('chai'); +var request = require('request'); +var expect = chai.expect; +var arete = require('arete'); + +require('dotenv').load(); + +describe('/user', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(200); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 200 OK', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(400); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 400 NOT OK', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(500); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 500 SERVER ERROR', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(200); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(400); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(500); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('put', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(200); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(400); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(500); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('patch', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(200); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('delete', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(200); + + expect(body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/request/should/.env b/test/loadTest/compare/request/should/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/loadTest/compare/request/should/.env @@ -0,0 +1,4 @@ +OAUTH=YOUR_TOKEN_GOES_HERE +KEY=YOUR_TOKEN_GOES_HERE +BASIC_AUTH=YOUR_TOKEN_GOES_HERE + diff --git a/test/loadTest/compare/request/should/base-path-test.js b/test/loadTest/compare/request/should/base-path-test.js new file mode 100644 index 0000000..54a6900 --- /dev/null +++ b/test/loadTest/compare/request/should/base-path-test.js @@ -0,0 +1,231 @@ +'use strict'; +var chai = require('chai'); +var ZSchema = require('z-schema'); +var validator = new ZSchema({}); +var request = require('request'); + +chai.should(); + +require('dotenv').load(); + +describe('/', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + accessToken: process.env.KEY + }, + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(200); + + validator.validate(body, schema).should.be.true; + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "meta": "string", + "data": "number" + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + accessToken: process.env.KEY + }, + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(400); + + validator.validate(body, schema).should.be.true; + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "properties": { + "meta": "string", + "data": "number", + "UserObj": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + accessToken: process.env.KEY + }, + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(500); + + validator.validate(body, schema).should.be.true; + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(200); + + validator.validate(body, schema).should.be.true; + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "number" + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(400); + + validator.validate(body, schema).should.be.true; + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "type": "string" + }; + + /*eslint-enable*/ + request({ + url: 'https://api.uber.com/', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(500); + + validator.validate(body, schema).should.be.true; + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/request/should/user-test.js b/test/loadTest/compare/request/should/user-test.js new file mode 100644 index 0000000..e23c2cb --- /dev/null +++ b/test/loadTest/compare/request/should/user-test.js @@ -0,0 +1,394 @@ +'use strict'; +var chai = require('chai'); +var request = require('request'); + +chai.should(); +var arete = require('arete'); + +require('dotenv').load(); + +describe('/user', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(200); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(400); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(500); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(200); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 200 OK', function(done) { + arete.loadTest({ + name: '_user_post_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(400); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 400 NOT OK', function(done) { + arete.loadTest({ + name: '_user_post_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(500); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 500 SERVER ERROR', function(done) { + arete.loadTest({ + name: '_user_post_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + callback(error, body); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + }); + + describe('put', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(200); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(400); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/user', + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'PUT', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(500); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('patch', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + process.env.OAUTH + }, + json: { + latitude: 'DATA GOES HERE' + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(200); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('delete', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/user', + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Basic ' + process.env.BASIC_AUTH + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(200); + + body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/supertest/assert/.env b/test/loadTest/compare/supertest/assert/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/loadTest/compare/supertest/assert/.env @@ -0,0 +1,4 @@ +OAUTH=YOUR_TOKEN_GOES_HERE +KEY=YOUR_TOKEN_GOES_HERE +BASIC_AUTH=YOUR_TOKEN_GOES_HERE + diff --git a/test/loadTest/compare/supertest/assert/base-path-test.js b/test/loadTest/compare/supertest/assert/base-path-test.js new file mode 100644 index 0000000..339def4 --- /dev/null +++ b/test/loadTest/compare/supertest/assert/base-path-test.js @@ -0,0 +1,201 @@ +'use strict'; +var chai = require('chai'); +var ZSchema = require('z-schema'); +var validator = new ZSchema({}); +var supertest = require('supertest'); +var api = supertest('https://api.uber.com'); // supertest init; +var assert = chai.assert; + +require('dotenv').load(); + +describe('/', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + }; + + /*eslint-enable*/ + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert.true(validator.validate(res.body, schema)); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "meta": "string", + "data": "number" + } + }; + + /*eslint-enable*/ + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Accept', 'application/json') + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + assert.true(validator.validate(res.body, schema)); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "properties": { + "meta": "string", + "data": "number", + "UserObj": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } + } + }; + + /*eslint-enable*/ + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Accept', 'application/json') + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + assert.true(validator.validate(res.body, schema)); + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + }; + + /*eslint-enable*/ + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert.true(validator.validate(res.body, schema)); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "number" + }; + + /*eslint-enable*/ + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + assert.true(validator.validate(res.body, schema)); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "type": "string" + }; + + /*eslint-enable*/ + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + assert.true(validator.validate(res.body, schema)); + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/supertest/assert/user-test.js b/test/loadTest/compare/supertest/assert/user-test.js new file mode 100644 index 0000000..e5843bc --- /dev/null +++ b/test/loadTest/compare/supertest/assert/user-test.js @@ -0,0 +1,406 @@ +'use strict'; +var chai = require('chai'); +var supertest = require('supertest'); +var api = supertest('https://api.uber.com'); // supertest init; +var assert = chai.assert; +var arete = require('arete'); + +require('dotenv').load(); + +describe('/user', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + it('load tests with 200 OK', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + it('load tests with 400 NOT OK', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(400) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + it('load tests with 500 SERVER ERROR', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(500) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + + }); + + describe('put', function() { + it('should respond with 200 OK', function(done) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + it('load tests with 200 OK', function(done) { + arete.loadTest({ + name: '_user_put_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + it('load tests with 400 NOT OK', function(done) { + arete.loadTest({ + name: '_user_put_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(400) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + it('load tests with 500 SERVER ERROR', function(done) { + arete.loadTest({ + name: '_user_put_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(500) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + assert.equal(report.successfulResponses.length, + report.results.length); + assert.isBelow(report.averageResponseTimeInternal, + 'TIME DATA HERE'); + assert.isBelow(report.timeElapsed, + 'TIME DATA HERE'); + done(); + } + }); + }); + + }); + + describe('patch', function() { + it('should respond with 200 OK', function(done) { + api.patch('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + + }); + + describe('delete', function() { + it('should respond with 200 OK', function(done) { + api.del('/user') + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert.isNull(res.body); // non-json response or no schema + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/supertest/expect/.env b/test/loadTest/compare/supertest/expect/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/loadTest/compare/supertest/expect/.env @@ -0,0 +1,4 @@ +OAUTH=YOUR_TOKEN_GOES_HERE +KEY=YOUR_TOKEN_GOES_HERE +BASIC_AUTH=YOUR_TOKEN_GOES_HERE + diff --git a/test/loadTest/compare/supertest/expect/base-path-test.js b/test/loadTest/compare/supertest/expect/base-path-test.js new file mode 100644 index 0000000..eaf1447 --- /dev/null +++ b/test/loadTest/compare/supertest/expect/base-path-test.js @@ -0,0 +1,201 @@ +'use strict'; +var chai = require('chai'); +var ZSchema = require('z-schema'); +var validator = new ZSchema({}); +var supertest = require('supertest'); +var api = supertest('https://api.uber.com'); // supertest init; +var expect = chai.expect; + +require('dotenv').load(); + +describe('/', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + }; + + /*eslint-enable*/ + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(validator.validate(res.body, schema)).to.be.true; + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "meta": "string", + "data": "number" + } + }; + + /*eslint-enable*/ + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Accept', 'application/json') + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + expect(validator.validate(res.body, schema)).to.be.true; + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "properties": { + "meta": "string", + "data": "number", + "UserObj": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } + } + }; + + /*eslint-enable*/ + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Accept', 'application/json') + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + expect(validator.validate(res.body, schema)).to.be.true; + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + }; + + /*eslint-enable*/ + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(validator.validate(res.body, schema)).to.be.true; + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "number" + }; + + /*eslint-enable*/ + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + expect(validator.validate(res.body, schema)).to.be.true; + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "type": "string" + }; + + /*eslint-enable*/ + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + expect(validator.validate(res.body, schema)).to.be.true; + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/supertest/expect/user-test.js b/test/loadTest/compare/supertest/expect/user-test.js new file mode 100644 index 0000000..a6ddad7 --- /dev/null +++ b/test/loadTest/compare/supertest/expect/user-test.js @@ -0,0 +1,298 @@ +'use strict'; +var chai = require('chai'); +var supertest = require('supertest'); +var api = supertest('https://api.uber.com'); // supertest init; +var expect = chai.expect; +var arete = require('arete'); + +require('dotenv').load(); + +describe('/user', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 200 OK', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 400 NOT OK', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(400) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 500 SERVER ERROR', function(done) { + arete.loadTest({ + name: '_user_get_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(500) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + expect(report.successfulResponses.length). + to.equal(report.results.length); + expect(report.averageResponseTimeInternal). + to.be.lessThan('TIME DATA HERE'); + expect(report.timeElapsed). + to.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('put', function() { + it('should respond with 200 OK', function(done) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('patch', function() { + it('should respond with 200 OK', function(done) { + api.patch('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('delete', function() { + it('should respond with 200 OK', function(done) { + api.del('/user') + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/supertest/should/.env b/test/loadTest/compare/supertest/should/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/loadTest/compare/supertest/should/.env @@ -0,0 +1,4 @@ +OAUTH=YOUR_TOKEN_GOES_HERE +KEY=YOUR_TOKEN_GOES_HERE +BASIC_AUTH=YOUR_TOKEN_GOES_HERE + diff --git a/test/loadTest/compare/supertest/should/base-path-test.js b/test/loadTest/compare/supertest/should/base-path-test.js new file mode 100644 index 0000000..76998a8 --- /dev/null +++ b/test/loadTest/compare/supertest/should/base-path-test.js @@ -0,0 +1,202 @@ +'use strict'; +var chai = require('chai'); +var ZSchema = require('z-schema'); +var validator = new ZSchema({}); +var supertest = require('supertest'); +var api = supertest('https://api.uber.com'); // supertest init; + +chai.should(); + +require('dotenv').load(); + +describe('/', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + }; + + /*eslint-enable*/ + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + validator.validate(res.body, schema).should.be.true; + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "object", + "properties": { + "meta": "string", + "data": "number" + } + }; + + /*eslint-enable*/ + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Accept', 'application/json') + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + validator.validate(res.body, schema).should.be.true; + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "properties": { + "meta": "string", + "data": "number", + "UserObj": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } + } + }; + + /*eslint-enable*/ + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Accept', 'application/json') + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + validator.validate(res.body, schema).should.be.true; + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + }; + + /*eslint-enable*/ + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + validator.validate(res.body, schema).should.be.true; + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + /*eslint-disable*/ + var schema = { + "type": "number" + }; + + /*eslint-enable*/ + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + validator.validate(res.body, schema).should.be.true; + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + /*eslint-disable*/ + var schema = { + "type": "string" + }; + + /*eslint-enable*/ + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + validator.validate(res.body, schema).should.be.true; + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/compare/supertest/should/user-test.js b/test/loadTest/compare/supertest/should/user-test.js new file mode 100644 index 0000000..bdc6d14 --- /dev/null +++ b/test/loadTest/compare/supertest/should/user-test.js @@ -0,0 +1,317 @@ +'use strict'; +var chai = require('chai'); +var supertest = require('supertest'); +var api = supertest('https://api.uber.com'); // supertest init; + +chai.should(); +var arete = require('arete'); + +require('dotenv').load(); + +describe('/user', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.get('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 200 OK', function(done) { + arete.loadTest({ + name: '_user_post_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 400 NOT OK', function(done) { + arete.loadTest({ + name: '_user_post_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(400) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + it('load tests with 500 SERVER ERROR', function(done) { + arete.loadTest({ + name: '_user_post_load_test', + requests: 1000, + concurrentRequests: 100, + targetFunction: function(callback) { + api.post('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(500) + .end(function(err, res) { + callback(err, res); + }); + }, + printResponses: false, // true or false + printReport: true, // true or false + printSteps: true, // true or false + callback: function(error, report) { + if (error) return done(error); + + report.successfulResponses.length. + should.equal(report.results.length); + (report.averageResponseTimeInternal). + should.be.lessThan('TIME DATA HERE'); + (report.timeElapsed). + should.be.lessThan('TIME DATA HERE'); + done(); + } + }); + }); + + }); + + describe('put', function() { + it('should respond with 200 OK', function(done) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.put('/user') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('patch', function() { + it('should respond with 200 OK', function(done) { + api.patch('/user') + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Accept', 'application/json') + .send({ + latitude: 'DATA GOES HERE' + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + + describe('delete', function() { + it('should respond with 200 OK', function(done) { + api.del('/user') + .set('Authorization', 'Basic ' + process.env.BASIC_AUTH) + .set('Accept', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.equal(null); // non-json response or no schema + done(); + }); + }); + + }); + +}); diff --git a/test/loadTest/swagger.json b/test/loadTest/swagger.json new file mode 100644 index 0000000..36f9522 --- /dev/null +++ b/test/loadTest/swagger.json @@ -0,0 +1,284 @@ +{ + "swagger": "2.0", + "info": { + "version": "0.0.0", + "title": "Simple API" + }, + "securityDefinitions": { + "oauth": { + "type": "oauth2", + "authorizationUrl": "http://swagger.io/api/oauth/dialog", + "flow": "implicit", + "scopes": { + "test": "just for test" + } + }, + "key": { + "type": "apiKey", + "in": "query", + "name": "accessToken" + }, + "basicAuth": { + "type": "basic", + "description": "just for basic auth" + } + }, + "host": "api.uber.com", + "schemes": [ + "https" + ], + "paths": { + "/": { + "get": { + "security":[ + { + "key": [] + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + }, + "400": { + "description": "NOT OK", + "schema": { + "type":"object", + "properties": { + "meta": "string", + "data": "number" + } + } + }, + "500": { + "description": "SERVER ERROR", + "schema": { + "properties": { + "meta":"string", + "data":"number", + "UserObj": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } + } + } + } + } + }, + "post": { + "security": [ + { + "oauth": ["test"] + } + ], + "parameters": [ + { + "name": "X-latitude", + "in": "header", + "description": "Latitude component of location.", + "required": true, + "type": "number", + "format": "double" + }, + { + "name": "longitude", + "in": "query", + "description": "longitude component of location.", + "required": true, + "type": "number", + "format": "double" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } + }, + "400": { + "description": "NOT OK", + "schema": { + "type": "number" + } + }, + "500": { + "description": "SERVER ERROR", + "schema": { + "type": "string" + } + } + } + } + }, + "/user": { + "get": { + "security":[ + { + "oauth": ["test"] + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "NOT OK" + }, + "500": { + "description": "SERVER ERROR" + } + } + }, + "post": { + "security": [ + { + "basicAuth": [] + } + ], + "parameters": [ + { + "name": "latitude", + "in": "body", + "description": "Latitude component of location.", + "required": true, + "type": "number", + "format": "double" + }, + { + "name": "longitude", + "in": "query", + "description": "longitude component of location.", + "required": true, + "type": "number", + "format": "double" + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "NOT OK" + }, + "500": { + "description": "SERVER ERROR" + } + } + }, + "put": { + "security": [ + { + "oauth": ["test"] + } + ], + "parameters": [ + { + "name": "latitude", + "in": "body", + "description": "Latitude component of location.", + "required": true, + "type": "number", + "format": "double" + }, + { + "name": "longitude", + "in": "query", + "description": "longitude component of location.", + "required": true, + "type": "number", + "format": "double" + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "NOT OK" + }, + "500": { + "description": "SERVER ERROR" + } + } + }, + "patch": { + "security": [ + { + "oauth": ["test"] + } + ], + "parameters": [ + { + "name": "latitude", + "in": "body", + "description": "Latitude component of location.", + "required": true, + "type": "number", + "format": "double" + } + ], + "responses": { + "200": { + "description": "OK" + } + } + }, + "delete": { + "security":[ + { + "basicAuth": ["test"] + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + } + }, + "definitions": { + "User": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } +} diff --git a/test/loadTest/test.js b/test/loadTest/test.js new file mode 100644 index 0000000..e787c78 --- /dev/null +++ b/test/loadTest/test.js @@ -0,0 +1,301 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2015 Apigee Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +'use strict'; + +var assert = require('chai').assert; +var testGen = require('../../index.js').testGen; +var swagger = require('./swagger.json'); +var linter = require('eslint').linter; +var yaml = require('js-yaml'); +var join = require('path').join; +var rules; +var read = require('fs').readFileSync; + +rules = yaml.safeLoad(read(join(__dirname, '/../../.eslintrc'), 'utf8')); +rules.env = {mocha: true}; + +describe('security swagger', function() { + describe('assert-option', function() { + describe('expect', function() { + var output1 = testGen(swagger, { + assertionFormat: 'expect', + pathName: [], + testModule: 'request', + loadTest: [{pathName: '/user', operation: 'get', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths1 = []; + var ndx; + + for (ndx in output1) { + if (output1) { + paths1.push(join(__dirname, '/compare/request/expect/' + + output1[ndx].name)); + } + } + + it('should have path parameters with an obvious indicator', function() { + + assert.isArray(output1); + assert.lengthOf(output1, 3); + + var generatedCode; + + for (ndx in paths1) { + if (paths1 !== undefined) { + generatedCode = read(paths1[ndx], 'utf8'); + assert.equal(output1[ndx].test, generatedCode); + } + } + + for (ndx in output1) { + if (output1 !== undefined && output1[ndx].name !== '.env') { + assert.lengthOf(linter.verify(output1[ndx].test, rules), 0); + } + } + }); + }); + }); + + describe('supertest-option', function() { + describe('expect', function() { + var output2 = testGen(swagger, { + assertionFormat: 'expect', + pathName: [], + testModule: 'supertest', + loadTest: [{pathName: '/user', operation: 'get', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths2 = []; + var ndx; + + for (ndx in output2) { + if (output2) { + paths2.push(join(__dirname, '/compare/supertest/expect/' + + output2[ndx].name)); + } + } + + it('should have path parameters with an obvious indicator', function() { + + assert.isArray(output2); + assert.lengthOf(output2, 3); + + var generatedCode; + + for (ndx in paths2) { + if (paths2 !== undefined) { + generatedCode = read(paths2[ndx], 'utf8'); + assert.equal(output2[ndx].test, generatedCode); + } + } + + for (ndx in output2 && output2[ndx].name !== '.env') { + if (output2 !== undefined) { + assert.lengthOf(linter.verify(output2[ndx].test, rules), 0); + } + } + }); + }); + }); + + describe('should-option', function() { + describe('should', function() { + var output2 = testGen(swagger, { + assertionFormat: 'should', + pathName: [], + testModule: 'request', + loadTest: [{pathName: '/user', operation: 'post', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths2 = []; + var ndx; + + for (ndx in output2) { + if (output2) { + paths2.push(join(__dirname, '/compare/request/should/' + + output2[ndx].name)); + } + } + + it('should have path parameters with an obvious indicator', function() { + + assert.isArray(output2); + assert.lengthOf(output2, 3); + + var generatedCode; + + for (ndx in paths2) { + if (paths2 !== undefined) { + generatedCode = read(paths2[ndx], 'utf8'); + assert.equal(output2[ndx].test, generatedCode); + } + } + + for (ndx in output2 && output2[ndx].name !== '.env') { + if (output2 !== undefined) { + assert.lengthOf(linter.verify(output2[ndx].test, rules), 0); + } + } + }); + }); + }); + + describe('supertest-option', function() { + describe('should', function() { + var output2 = testGen(swagger, { + assertionFormat: 'should', + pathName: [], + testModule: 'supertest', + loadTest: [{pathName: '/user', operation: 'post', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths2 = []; + var ndx; + + for (ndx in output2) { + if (output2) { + paths2.push(join(__dirname, '/compare/supertest/should/' + + output2[ndx].name)); + } + } + + it('should have path parameters with an obvious indicator', function() { + + assert.isArray(output2); + assert.lengthOf(output2, 3); + + var generatedCode; + + for (ndx in paths2) { + if (paths2 !== undefined) { + generatedCode = read(paths2[ndx], 'utf8'); + assert.equal(output2[ndx].test, generatedCode); + } + } + + for (ndx in output2 && output2[ndx].name !== '.env') { + if (output2 !== undefined) { + assert.lengthOf(linter.verify(output2[ndx].test, rules), 0); + } + } + }); + }); + }); + + describe('assert-option', function() { + describe('assert', function() { + var output2 = testGen(swagger, { + assertionFormat: 'assert', + pathName: [], + testModule: 'request', + loadTest: [{pathName: '/user', operation: 'get', + load: {requests: 1000, concurrent: 100}}, + {pathName: '/user', operation: 'put', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths2 = []; + var ndx; + + for (ndx in output2) { + if (output2) { + paths2.push(join(__dirname, '/compare/request/assert/' + + output2[ndx].name)); + } + } + + it('should have path parameters with an obvious indicator', function() { + + assert.isArray(output2); + assert.lengthOf(output2, 3); + + var generatedCode; + + for (ndx in paths2) { + if (paths2 !== undefined) { + generatedCode = read(paths2[ndx], 'utf8'); + assert.equal(output2[ndx].test, generatedCode); + } + } + + for (ndx in output2 && output2[ndx].name !== '.env') { + if (output2 !== undefined) { + assert.lengthOf(linter.verify(output2[ndx].test, rules), 0); + } + } + }); + }); + }); + + describe('supertest-option', function() { + describe('assert', function() { + var output2 = testGen(swagger, { + assertionFormat: 'assert', + pathName: [], + testModule: 'supertest', + loadTest: [{pathName: '/user', operation: 'get', + load: {requests: 1000, concurrent: 100}}, + {pathName: '/user', operation: 'put', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths2 = []; + var ndx; + + for (ndx in output2) { + if (output2) { + paths2.push(join(__dirname, '/compare/supertest/assert/' + + output2[ndx].name)); + } + } + + it('should have path parameters with an obvious indicator', function() { + + assert.isArray(output2); + assert.lengthOf(output2, 3); + + var generatedCode; + + for (ndx in paths2) { + if (paths2 !== undefined) { + generatedCode = read(paths2[ndx], 'utf8'); + assert.equal(output2[ndx].test, generatedCode); + } + } + + for (ndx in output2 && output2[ndx].name !== '.env') { + if (output2 !== undefined) { + assert.lengthOf(linter.verify(output2[ndx].test, rules), 0); + } + } + }); + }); + }); +}); diff --git a/test/pathParam/compare/output1-{id}-test.js b/test/pathParam/compare/output1-{id}-test.js index e3d2ecb..408903f 100644 --- a/test/pathParam/compare/output1-{id}-test.js +++ b/test/pathParam/compare/output1-{id}-test.js @@ -4,7 +4,6 @@ var request = require('request'); chai.should(); - describe('/{id}', function() { describe('get', function() { it('should respond with 200 OK', function(done) { diff --git a/test/pathParam/compare/output2-{id}-test.js b/test/pathParam/compare/output2-{id}-test.js index 3a82bd3..8d9b62b 100644 --- a/test/pathParam/compare/output2-{id}-test.js +++ b/test/pathParam/compare/output2-{id}-test.js @@ -5,7 +5,6 @@ var api = supertest('http://localhost:10010'); // supertest init; chai.should(); - describe('/{id}', function() { describe('get', function() { it('should respond with 200 OK', function(done) {