diff --git a/index.js b/index.js index dea33d7..e12157a 100644 --- a/index.js +++ b/index.js @@ -559,6 +559,7 @@ handlebars.registerHelper('printJSON', helpers.printJSON); handlebars.registerHelper('requestDataParamFormatter', helpers.requestDataParamFormatter); handlebars.registerHelper('isJsonRepresentation', helpers.isJsonRepresentation); handlebars.registerHelper('isJsonMediaType', helpers.isJsonMediaType); +handlebars.registerHelper('isPdfMediaType', helpers.isPdfMediaType); module.exports = { diff --git a/lib/helpers.js b/lib/helpers.js index ba695f9..1267ce7 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -14,9 +14,11 @@ module.exports = { printJSON: printJSON, len: len, setLen: setLen, + isPdfMediaType : isPdfMediaType, requestDataParamFormatter : requestDataParamFormatter, isJsonRepresentation : isJsonRepresentation, isJsonMediaType : isJsonMediaType, + mediaTypeContainsPdf : mediaTypeContainsPdf, mediaTypeContainsJson : mediaTypeContainsJson }; @@ -77,6 +79,14 @@ function isJsonMediaType(type, options) { return mediaTypeContainsJson(type) ? options.fn(this) : options.inverse(this); } +/** + * mustache helper method to determine if a mediaType is PDF + * @param {string} type content type to be evaluated + */ +function isPdfMediaType(type, options) { + return mediaTypeContainsPdf(type) ? options.fn(this) : options.inverse(this); +} + /** * decides if this request/response has a JSON representation * @param {string} contentType the media type of the request @@ -94,6 +104,14 @@ function mediaTypeContainsJson(type) { return /\bjson\b/i.test(type); } +/** + * determines if the mediatype is pdf + * @param {string} type content type to be evaluated + */ +function mediaTypeContainsPdf(type) { + return /\bpdf\b/i.test(type); +} + /** * replaces path params with obvious indicator for filling values * (i.e. if any part of the path is surrounded in curly braces {}) diff --git a/templates/request/get/get.handlebars b/templates/request/get/get.handlebars index a4ce72f..a8c07c8 100644 --- a/templates/request/get/get.handlebars +++ b/templates/request/get/get.handlebars @@ -61,6 +61,17 @@ assert.true(validator.validate(body, schema)); {{/is}} {{else}} + {{#isPdfMediaType returnType}} + {{#is assertion 'expect'}} + expect(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'should'}} + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'assert'}} + assert.deepEqual(body, new Buffer(Number(res.header['content-length']))); + {{/is}} + {{else}} {{#is assertion 'expect'}} expect(body).to.equal(null); // non-json response or no schema {{/is}} @@ -70,6 +81,7 @@ {{#is assertion 'assert'}} assert.isNull(body); // non-json response or no schema {{/is}} + {{/isPdfMediaType}} {{/validateResponse}} done(); }); diff --git a/templates/request/patch/patch.handlebars b/templates/request/patch/patch.handlebars index 46f8087..e72bc10 100644 --- a/templates/request/patch/patch.handlebars +++ b/templates/request/patch/patch.handlebars @@ -90,6 +90,17 @@ assert.true(validator.validate(body, schema)); {{/is}} {{else}} + {{#isPdfMediaType returnType}} + {{#is assertion 'expect'}} + expect(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'should'}} + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'assert'}} + assert.deepEqual(body, new Buffer(Number(res.header['content-length']))); + {{/is}} + {{else}} {{#is assertion 'expect'}} expect(body).to.equal(null); // non-json response or no schema {{/is}} @@ -99,6 +110,7 @@ {{#is assertion 'assert'}} assert.isNull(body); // non-json response or no schema {{/is}} + {{/isPdfMediaType}} {{/validateResponse}} done(); }); diff --git a/templates/request/post/post.handlebars b/templates/request/post/post.handlebars index cd330b9..0b1d8b0 100644 --- a/templates/request/post/post.handlebars +++ b/templates/request/post/post.handlebars @@ -88,6 +88,17 @@ assert.true(validator.validate(body, schema)); {{/is}} {{else}} + {{#isPdfMediaType returnType}} + {{#is assertion 'expect'}} + expect(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'should'}} + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'assert'}} + assert.deepEqual(body, new Buffer(Number(res.header['content-length']))); + {{/is}} + {{else}} {{#is assertion 'expect'}} expect(body).to.equal(null); // non-json response or no schema {{/is}} @@ -97,6 +108,7 @@ {{#is assertion 'assert'}} assert.isNull(body); // non-json response or no schema {{/is}} + {{/isPdfMediaType}} {{/validateResponse}} done(); }); diff --git a/templates/request/put/put.handlebars b/templates/request/put/put.handlebars index 7df9e3e..d0bcc4c 100644 --- a/templates/request/put/put.handlebars +++ b/templates/request/put/put.handlebars @@ -88,6 +88,17 @@ assert.true(validator.validate(body, schema)); {{/is}} {{else}} + {{#isPdfMediaType returnType}} + {{#is assertion 'expect'}} + expect(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'should'}} + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'assert'}} + assert.deepEqual(body, new Buffer(Number(res.header['content-length']))); + {{/is}} + {{else}} {{#is assertion 'expect'}} expect(body).to.equal(null); // non-json response or no schema {{/is}} @@ -97,6 +108,7 @@ {{#is assertion 'assert'}} assert.isNull(body); // non-json response or no schema {{/is}} + {{/isPdfMediaType}} {{/validateResponse}} done(); }); diff --git a/templates/supertest/get/get.handlebars b/templates/supertest/get/get.handlebars index 821dc4d..c217952 100644 --- a/templates/supertest/get/get.handlebars +++ b/templates/supertest/get/get.handlebars @@ -46,6 +46,17 @@ assert.true(validator.validate(res.body, schema)); {{/is}} {{else}} + {{#isPdfMediaType returnType}} + {{#is assertion 'expect'}} + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'should'}} + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'assert'}} + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + {{/is}} + {{else}} {{#is assertion 'expect'}} expect(res.body).to.equal(null); // non-json response or no schema {{/is}} @@ -55,6 +66,7 @@ {{#is assertion 'assert'}} assert.isNull(res.body); // non-json response or no schema {{/is}} + {{/isPdfMediaType}} {{/validateResponse}} done(); }); diff --git a/templates/supertest/patch/patch.handlebars b/templates/supertest/patch/patch.handlebars index 6ac40e7..2f6553b 100644 --- a/templates/supertest/patch/patch.handlebars +++ b/templates/supertest/patch/patch.handlebars @@ -71,6 +71,17 @@ assert.true(validator.validate(res.body, schema)); {{/is}} {{else}} + {{#isPdfMediaType returnType}} + {{#is assertion 'expect'}} + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'should'}} + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'assert'}} + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + {{/is}} + {{else}} {{#is assertion 'expect'}} expect(res.body).to.equal(null); // non-json response or no schema {{/is}} @@ -80,6 +91,7 @@ {{#is assertion 'assert'}} assert.isNull(res.body); // non-json response or no schema {{/is}} + {{/isPdfMediaType}} {{/validateResponse}} done(); }); diff --git a/templates/supertest/post/post.handlebars b/templates/supertest/post/post.handlebars index 2c2087e..f5672d5 100644 --- a/templates/supertest/post/post.handlebars +++ b/templates/supertest/post/post.handlebars @@ -70,6 +70,17 @@ assert.true(validator.validate(res.body, schema)); {{/is}} {{else}} + {{#isPdfMediaType returnType}} + {{#is assertion 'expect'}} + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'should'}} + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'assert'}} + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + {{/is}} + {{else}} {{#is assertion 'expect'}} expect(res.body).to.equal(null); // non-json response or no schema {{/is}} @@ -79,6 +90,7 @@ {{#is assertion 'assert'}} assert.isNull(res.body); // non-json response or no schema {{/is}} + {{/isPdfMediaType}} {{/validateResponse}} done(); }); diff --git a/templates/supertest/put/put.handlebars b/templates/supertest/put/put.handlebars index 2a93d88..05bddd2 100644 --- a/templates/supertest/put/put.handlebars +++ b/templates/supertest/put/put.handlebars @@ -70,6 +70,17 @@ assert.true(validator.validate(res.body, schema)); {{/is}} {{else}} + {{#isPdfMediaType returnType}} + {{#is assertion 'expect'}} + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'should'}} + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + {{/is}} + {{#is assertion 'assert'}} + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + {{/is}} + {{else}} {{#is assertion 'expect'}} expect(res.body).to.equal(null); // non-json response or no schema {{/is}} @@ -79,6 +90,7 @@ {{#is assertion 'assert'}} assert.isNull(res.body); // non-json response or no schema {{/is}} + {{/isPdfMediaType}} {{/validateResponse}} done(); }); diff --git a/test/pdfConsumeTypeTest/compare/request/assert/.env b/test/pdfConsumeTypeTest/compare/request/assert/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/pdfConsumeTypeTest/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/pdfConsumeTypeTest/compare/request/assert/base-path-test.js b/test/pdfConsumeTypeTest/compare/request/assert/base-path-test.js new file mode 100644 index 0000000..2333208 --- /dev/null +++ b/test/pdfConsumeTypeTest/compare/request/assert/base-path-test.js @@ -0,0 +1,159 @@ +'use strict'; +var chai = require('chai'); +var request = require('request'); +var assert = chai.assert; + +require('dotenv').load(); + +describe('/', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + 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.deepEqual(body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + 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.deepEqual(body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + 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.deepEqual(body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + body: { + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 200); + + assert.deepEqual(body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + body: { + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 400); + + assert.deepEqual(body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + body: { + } + }, + function(error, res, body) { + if (error) return done(error); + + assert.equal(res.statusCode, 500); + + assert.deepEqual(body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + +}); diff --git a/test/pdfConsumeTypeTest/compare/request/expect/.env b/test/pdfConsumeTypeTest/compare/request/expect/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/pdfConsumeTypeTest/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/pdfConsumeTypeTest/compare/request/expect/base-path-test.js b/test/pdfConsumeTypeTest/compare/request/expect/base-path-test.js new file mode 100644 index 0000000..06caa3b --- /dev/null +++ b/test/pdfConsumeTypeTest/compare/request/expect/base-path-test.js @@ -0,0 +1,159 @@ +'use strict'; +var chai = require('chai'); +var request = require('request'); +var expect = chai.expect; + +require('dotenv').load(); + +describe('/', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + 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(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + 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(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + 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(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + body: { + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(200); + + expect(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + body: { + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(400); + + expect(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + body: { + } + }, + function(error, res, body) { + if (error) return done(error); + + expect(res.statusCode).to.equal(500); + + expect(body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + +}); diff --git a/test/pdfConsumeTypeTest/compare/request/should/.env b/test/pdfConsumeTypeTest/compare/request/should/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/pdfConsumeTypeTest/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/pdfConsumeTypeTest/compare/request/should/base-path-test.js b/test/pdfConsumeTypeTest/compare/request/should/base-path-test.js new file mode 100644 index 0000000..5e07c37 --- /dev/null +++ b/test/pdfConsumeTypeTest/compare/request/should/base-path-test.js @@ -0,0 +1,160 @@ +'use strict'; +var chai = require('chai'); +var request = require('request'); + +chai.should(); + +require('dotenv').load(); + +describe('/', function() { + describe('get', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + 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); + + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + 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); + + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + 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); + + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + body: { + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(200); + + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + body: { + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(400); + + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + request({ + url: 'https://api.uber.com/', + json: true, + qs: { + longitude: 'DATA GOES HERE' + }, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-latitude': 'DATA GOES HERE', + Authorization: 'Bearer ' + process.env.OAUTH + }, + body: { + } + }, + function(error, res, body) { + if (error) return done(error); + + res.statusCode.should.equal(500); + + body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + +}); diff --git a/test/pdfConsumeTypeTest/compare/supertest/assert/.env b/test/pdfConsumeTypeTest/compare/supertest/assert/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/pdfConsumeTypeTest/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/pdfConsumeTypeTest/compare/supertest/assert/base-path-test.js b/test/pdfConsumeTypeTest/compare/supertest/assert/base-path-test.js new file mode 100644 index 0000000..34c7017 --- /dev/null +++ b/test/pdfConsumeTypeTest/compare/supertest/assert/base-path-test.js @@ -0,0 +1,124 @@ +'use strict'; +var chai = require('chai'); +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) { + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Content-Type', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Content-Type', 'application/json') + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Content-Type', 'application/json') + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Content-Type', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Content-Type', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Content-Type', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + assert.deepEqual(res.body, new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + +}); diff --git a/test/pdfConsumeTypeTest/compare/supertest/expect/.env b/test/pdfConsumeTypeTest/compare/supertest/expect/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/pdfConsumeTypeTest/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/pdfConsumeTypeTest/compare/supertest/expect/base-path-test.js b/test/pdfConsumeTypeTest/compare/supertest/expect/base-path-test.js new file mode 100644 index 0000000..c6a1f92 --- /dev/null +++ b/test/pdfConsumeTypeTest/compare/supertest/expect/base-path-test.js @@ -0,0 +1,124 @@ +'use strict'; +var chai = require('chai'); +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) { + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Content-Type', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Content-Type', 'application/json') + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Content-Type', 'application/json') + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Content-Type', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Content-Type', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Content-Type', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + expect(res.body).to.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + +}); diff --git a/test/pdfConsumeTypeTest/compare/supertest/should/.env b/test/pdfConsumeTypeTest/compare/supertest/should/.env new file mode 100644 index 0000000..bb56aba --- /dev/null +++ b/test/pdfConsumeTypeTest/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/pdfConsumeTypeTest/compare/supertest/should/base-path-test.js b/test/pdfConsumeTypeTest/compare/supertest/should/base-path-test.js new file mode 100644 index 0000000..7802396 --- /dev/null +++ b/test/pdfConsumeTypeTest/compare/supertest/should/base-path-test.js @@ -0,0 +1,125 @@ +'use strict'; +var chai = require('chai'); +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) { + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Content-Type', 'application/json') + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Content-Type', 'application/json') + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.get('/') + .query({ + accessToken: process.env.KEY + }) + .set('Content-Type', 'application/json') + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + + describe('post', function() { + it('should respond with 200 OK', function(done) { + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Content-Type', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(200) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 400 NOT OK', function(done) { + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Content-Type', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(400) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + it('should respond with 500 SERVER ERROR', function(done) { + api.post('/') + .query({ + longitude: 'DATA GOES HERE' + }) + .set('Authorization', 'Bearer ' + process.env.OAUTH) + .set('Content-Type', 'application/json') + .set({ + 'X-latitude': 'DATA GOES HERE' + }) + .send({ + }) + .expect(500) + .end(function(err, res) { + if (err) return done(err); + + res.body.should.deep.equal(new Buffer(Number(res.header['content-length']))); + done(); + }); + }); + + }); + +}); diff --git a/test/pdfConsumeTypeTest/swagger.json b/test/pdfConsumeTypeTest/swagger.json new file mode 100644 index 0000000..8732101 --- /dev/null +++ b/test/pdfConsumeTypeTest/swagger.json @@ -0,0 +1,160 @@ +{ + "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": { + "produces": ["application/pdf"], + "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": { + "produces": ["application/pdf"], + "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" + } + } + } + } + } + }, + "definitions": { + "User": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "username": { + "type": "string" + } + } + } + } +} diff --git a/test/pdfConsumeTypeTest/test.js b/test/pdfConsumeTypeTest/test.js new file mode 100644 index 0000000..7c42cec --- /dev/null +++ b/test/pdfConsumeTypeTest/test.js @@ -0,0 +1,296 @@ +/* + * 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('test xml mime type', function() { + describe('request-option', function() { + + describe('assert', function() { + var output = 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 paths = []; + var ndx; + + for (ndx in output) { + if (output) { + paths.push(join(__dirname, '/compare/request/assert/' + + output[ndx].name)); + } + } + + it('should be able to generate test for request that produces xml', function() { + assert.isArray(output); + assert.lengthOf(output, 2); + + var generatedCode; + + for (ndx in paths) { + if (paths !== undefined) { + generatedCode = read(paths[ndx], 'utf8').replace(/\r\n/g, '\n'); + assert.equal(output[ndx].test.replace(/\r\n/g, '\n'), + generatedCode); + } + } + + for (ndx in output && output[ndx].name !== '.env') { + if (output !== undefined) { + assert.lengthOf(linter.verify(output[ndx].test, rules), 0); + } + } + }); + }); + + describe('expect', function() { + var output = testGen(swagger, { + assertionFormat: 'expect', + pathName: [], + testModule: 'request', + loadTest: [{pathName: '/user', operation: 'get', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths = []; + var ndx; + + for (ndx in output) { + if (output) { + paths.push(join(__dirname, '/compare/request/expect/' + + output[ndx].name)); + } + } + + it('should be able to generate test for request that produces xml', function() { + assert.isArray(output); + assert.lengthOf(output, 2); + + var generatedCode; + + for (ndx in paths) { + if (paths !== undefined) { + generatedCode = read(paths[ndx], 'utf8').replace(/\r\n/g, '\n'); + assert.equal(output[ndx].test.replace(/\r\n/g, '\n'), + generatedCode); + } + } + + for (ndx in output) { + if (output !== undefined && output[ndx].name !== '.env') { + assert.lengthOf(linter.verify(output[ndx].test, rules), 0); + } + } + + }); + }); + + describe('should', function() { + var output = testGen(swagger, { + assertionFormat: 'should', + pathName: [], + testModule: 'request', + loadTest: [{pathName: '/user', operation: 'post', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths = []; + var ndx; + + for (ndx in output) { + if (output) { + paths.push(join(__dirname, '/compare/request/should/' + + output[ndx].name)); + } + } + + it('should be able to generate test for request that produces xml', function() { + assert.isArray(output); + assert.lengthOf(output, 2); + + var generatedCode; + + for (ndx in paths) { + if (paths !== undefined) { + generatedCode = read(paths[ndx], 'utf8').replace(/\r\n/g, '\n'); + assert.equal(output[ndx].test.replace(/\r\n/g, '\n'), + generatedCode); + } + } + + for (ndx in output && output[ndx].name !== '.env') { + if (output !== undefined) { + assert.lengthOf(linter.verify(output[ndx].test, rules), 0); + } + } + }); + }); + }); + + describe('supertest-option', function() { + + describe('assert', function() { + var output = 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 paths = []; + var ndx; + + for (ndx in output) { + if (output) { + paths.push(join(__dirname, '/compare/supertest/assert/' + + output[ndx].name)); + } + } + + it('should be able to generate test for request that produces xml', function() { + assert.isArray(output); + assert.lengthOf(output, 2); + + var generatedCode; + + for (ndx in paths) { + if (paths !== undefined) { + generatedCode = read(paths[ndx], 'utf8').replace(/\r\n/g, '\n'); + assert.equal(output[ndx].test.replace(/\r\n/g, '\n'), + generatedCode); + } + } + + for (ndx in output && output[ndx].name !== '.env') { + if (output !== undefined) { + assert.lengthOf(linter.verify(output[ndx].test, rules), 0); + } + } + }); + }); + + describe('expect', function() { + var output = testGen(swagger, { + assertionFormat: 'expect', + pathName: [], + testModule: 'supertest', + loadTest: [{pathName: '/user', operation: 'get', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths = []; + var ndx; + + for (ndx in output) { + if (output) { + paths.push(join(__dirname, '/compare/supertest/expect/' + + output[ndx].name)); + } + } + + it('should be able to generate test for request that produces xml', function() { + assert.isArray(output); + assert.lengthOf(output, 2); + + var generatedCode; + + for (ndx in paths) { + if (paths !== undefined) { + generatedCode = read(paths[ndx], 'utf8').replace(/\r\n/g, '\n'); + assert.equal(output[ndx].test.replace(/\r\n/g, '\n'), + generatedCode); + } + } + + for (ndx in output && output[ndx].name !== '.env') { + if (output !== undefined) { + assert.lengthOf(linter.verify(output[ndx].test, rules), 0); + } + } + }); + }); + + describe('should', function() { + var output = testGen(swagger, { + assertionFormat: 'should', + pathName: [], + testModule: 'supertest', + loadTest: [{pathName: '/user', operation: 'post', + load: {requests: 1000, concurrent: 100}}] + }); + + var paths = []; + var ndx; + + for (ndx in output) { + if (output) { + paths.push(join(__dirname, '/compare/supertest/should/' + + output[ndx].name)); + } + } + + it('should be able to generate test for request that produces xml', function() { + assert.isArray(output); + assert.lengthOf(output, 2); + + var generatedCode; + + for (ndx in paths) { + if (paths !== undefined) { + generatedCode = read(paths[ndx], 'utf8').replace(/\r\n/g, '\n'); + assert.equal(output[ndx].test.replace(/\r\n/g, '\n'), + generatedCode); + } + } + + for (ndx in output && output[ndx].name !== '.env') { + if (output !== undefined) { + assert.lengthOf(linter.verify(output[ndx].test, rules), 0); + } + } + }); + }); + }); +});