diff --git a/tests/specs/mock/content-type.spec.js b/tests/specs/mock/content-type.spec.js deleted file mode 100644 index d59e7d3..0000000 --- a/tests/specs/mock/content-type.spec.js +++ /dev/null @@ -1,433 +0,0 @@ -var swagger = require('../../../'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'), - fs = require('fs'); - -describe('Mock Content-Type header', function() { - 'use strict'; - - var api; - beforeEach(function() { - api = _.cloneDeep(files.parsed.petStore); - }); - - describe('Object responses', function() { - it('should use "application/json" if no "produces" MIME types are defined', - function(done) { - delete api.produces; - delete api.paths['/pets/{PetName}'].get.produces; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "application/json" if the "produces" list is empty', - function(done) { - api.paths['/pets/{PetName}'].get.produces = []; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "application/json" if none of the "produces" MIME types are supported', - function(done) { - api.paths['/pets/{PetName}'].get.produces = ['text/html', 'image/jpeg', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "application/json" if included in the "produces" list', - function(done) { - api.paths['/pets/{PetName}'].get.produces = ['text/html', 'image/jpeg', 'application/json', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use the first "json" type in the "produces" list', - function(done) { - api.paths['/pets/{PetName}'].get.produces = ['text/json', 'application/calendar+json', 'application/json', 'application/merge-patch+json']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'text/json; charset=utf-8') - .expect(200, '{"Name":"Fido","Type":"dog"}') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "text/json" if included in the "produces" list', - function(done) { - api.paths['/pets/{PetName}'].get.produces = ['text/html', 'image/jpeg', 'text/json', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'text/json; charset=utf-8') - .expect(200, '{"Name":"Fido","Type":"dog"}') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "application/calendar+json" if included in the "produces" list', - function(done) { - api.paths['/pets/{PetName}'].get.produces = ['text/html', 'image/jpeg', 'application/calendar+json', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'application/calendar+json; charset=utf-8') - .expect(200, '{"Name":"Fido","Type":"dog"}') - .end(helper.checkResults(done)); - }); - }); - } - ); - }); - - describe('Text responses', function() { - it('should use "text/plain" if no "produces" MIME types are defined', - function(done) { - api.paths['/pets/{PetName}'].get.responses[200].schema = {type: 'string'}; - delete api.produces; - delete api.paths['/pets/{PetName}'].get.produces; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "text/plain" if the "produces" list is empty', - function(done) { - api.paths['/pets/{PetName}'].get.responses[200].schema = {type: 'string'}; - api.paths['/pets/{PetName}'].get.produces = []; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "text/plain" if none of the "produces" MIME types are supported', - function(done) { - api.paths['/pets/{PetName}'].get.responses[200].schema = {type: 'string'}; - api.paths['/pets/{PetName}'].get.produces = ['application/json', 'image/jpeg', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "text/plain" if included in the "produces" list', - function(done) { - api.paths['/pets/{PetName}'].get.responses[200].schema = {type: 'string'}; - api.paths['/pets/{PetName}'].get.produces = ['application/json', 'image/jpeg', 'text/plain', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use the first "text" type in the "produces" list', - function(done) { - api.paths['/pets/{PetName}'].get.responses[200].schema = {type: 'string'}; - api.paths['/pets/{PetName}'].get.produces = ['application/json', 'image/jpeg', 'text/cache-manifest', 'text/html', 'text/xml', 'text/plain']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'text/cache-manifest; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "text/html" if included in the "produces" list', - function(done) { - api.paths['/pets/{PetName}'].get.responses[200].schema = {type: 'string'}; - api.paths['/pets/{PetName}'].get.produces = ['application/json', 'image/jpeg', 'text/html', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'text/html; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "text/xml" if included in the "produces" list', - function(done) { - api.paths['/pets/{PetName}'].get.responses[200].schema = {type: 'string'}; - api.paths['/pets/{PetName}'].get.produces = ['application/json', 'image/jpeg', 'text/xml', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'text/xml; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "application/xml" if included in the "produces" list', - function(done) { - api.paths['/pets/{PetName}'].get.responses[200].schema = {type: 'string'}; - api.paths['/pets/{PetName}'].get.produces = ['application/json', 'image/jpeg', 'application/xml', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido') - .expect('Content-Type', 'application/xml; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - }); - } - ); - }); - - describe('File responses', function() { - var photoBuffer = fs.readFileSync(files.paths.oneMB); - - function isPhoto(res) { - if (res.body instanceof Buffer) { - for (var i = 0; i < photoBuffer.length; i++) { - if (res.body[i] !== photoBuffer[i]) { - return 'Invalid buffer contents (starting at position #' + i + ')'; - } - } - return false; - } - else { - return (res.text === photoBuffer.toString()) ? false : 'Invalid file contents'; - } - } - - it('should use "application/octet-stream" if no "produces" MIME types are defined', - function(done) { - delete api.produces; - delete api.paths['/pets/{PetName}/photos/{ID}'].get.produces; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido/photos', '/12345', photoBuffer); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido/photos/12345') - .expect('Content-Type', 'application/octet-stream') - .expect(200) - .expect(isPhoto) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "application/octet-stream" if the "produces" list is empty', - function(done) { - api.paths['/pets/{PetName}/photos/{ID}'].get.produces = []; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido/photos', '/12345', photoBuffer); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido/photos/12345') - .expect('Content-Type', 'application/octet-stream') - .expect(200) - .expect(isPhoto) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use the first MIME type in the "produces" list', - function(done) { - api.paths['/pets/{PetName}/photos/{ID}'].get.produces = ['text/plain', 'image/jpeg', 'text/cache-manifest', 'text/html', 'text/xml', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido/photos', '/12345', photoBuffer); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido/photos/12345') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200) - .expect(isPhoto) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "application/octet-stream" if it is first in the "produces" list', - function(done) { - api.paths['/pets/{PetName}/photos/{ID}'].get.produces = ['application/octet-stream', 'image/jpeg', 'text/html']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido/photos', '/12345', photoBuffer); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido/photos/12345') - .expect('Content-Type', 'application/octet-stream') - .expect(200) - .expect(isPhoto) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should use "image/jpeg" if it is first in the "produces" list', - function(done) { - api.paths['/pets/{PetName}/photos/{ID}'].get.produces = ['image/jpeg', 'application/xml', 'application/octet-stream']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido/photos', '/12345', photoBuffer); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .get('/api/pets/Fido/photos/12345') - .expect('Content-Type', 'image/jpeg') - .expect(200) - .expect(isPhoto) - .end(helper.checkResults(done)); - }); - }); - } - ); - }); -}); diff --git a/tests/specs/mock/delete-collection.spec.js b/tests/specs/mock/delete-collection.spec.js deleted file mode 100644 index ea06217..0000000 --- a/tests/specs/mock/delete-collection.spec.js +++ /dev/null @@ -1,967 +0,0 @@ -var swagger = require('../../../'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'); - -describe('Query Collection Mock', function() { - describe('DELETE', function() { - 'use strict'; - - var api; - beforeEach(function() { - api = _.cloneDeep(files.parsed.petStore); - api.paths['/pets'].delete = _.cloneDeep(api.paths['/pets'].get); - api.paths['/pets/{PetName}/photos'].delete = _.cloneDeep(api.paths['/pets/{PetName}/photos'].get); - }); - - it('should delete all resources in the collection', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - var resources = [ - new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}), - new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}), - new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}) - ]; - dataStore.save(resources, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets') - .expect(200) - .end(helper.checkResults(done, function() { - // Verify that all resources were deleted - dataStore.getCollection('/api/pets', function(err, resources) { - if (err) { - return done(err); - } - expect(resources).to.have.lengthOf(0); - done(); - }); - })); - }); - }); - } - ); - - it('should delete an empty collection', - function(done) { - helper.initTest(api, function(supertest) { - supertest - .delete('/api/pets') - .expect(200) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return the deleted resources if the Swagger API schema is an array', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - var resources = [ - new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}), - new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}), - new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}) - ]; - dataStore.save(resources, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets') - .expect(200, [ - {Name: 'Fido', Type: 'dog'}, - {Name: 'Fluffy', Type: 'cat'}, - {Name: 'Polly', Type: 'bird'} - ]) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the first deleted resource if the Swagger API schema is an object', - function(done) { - api.paths['/pets'].delete.responses[200].schema = {}; - - var dataStore = new swagger.MemoryDataStore(); - var resources = [ - new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}), - new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}), - new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}) - ]; - dataStore.save(resources, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done, function() { - // Verify that all resources were deleted - dataStore.getCollection('/api/pets', function(err, resources) { - if (err) { - return done(err); - } - expect(resources).to.have.lengthOf(0); - done(); - }); - })); - }); - }); - } - ); - - it('should return the deleted resources if the Swagger API schema is a wrapped array', - function(done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets'].delete.responses[200].schema = { - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {type: 'object'}, - result: {type: 'array', items: _.cloneDeep(api.definitions.pet)} - } - }; - - var dataStore = new swagger.MemoryDataStore(); - var resources = [ - new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}), - new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}), - new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}) - ]; - dataStore.save(resources, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets') - .expect(200, { - code: 42, - message: 'hello world', - result: [ - {Name: 'Fido', Type: 'dog'}, - {Name: 'Fluffy', Type: 'cat'}, - {Name: 'Polly', Type: 'bird'} - ] - }) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the first deleted resource if the Swagger API schema is a wrapped object', - function(done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets'].delete.responses[200].schema = { - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {type: 'object'}, - result: _.cloneDeep(api.definitions.pet) - } - }; - - var dataStore = new swagger.MemoryDataStore(); - var resources = [ - new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}), - new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}), - new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}) - ]; - dataStore.save(resources, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets') - .expect(200, {code: 42, message: 'hello world', result: {Name: 'Fido', Type: 'dog'}}) - .end(helper.checkResults(done, function() { - // Verify that all resources were deleted - dataStore.getCollection('/api/pets', function(err, resources) { - if (err) { - return done(err); - } - expect(resources).to.have.lengthOf(0); - done(); - }); - })); - }); - }); - } - ); - - it('should not return the deleted resources on a 204 response, even if the Swagger API schema is an array', - function(done) { - api.paths['/pets'].delete.responses[204] = api.paths['/pets'].delete.responses[200]; - delete api.paths['/pets'].delete.responses[200]; - - var dataStore = new swagger.MemoryDataStore(); - var resources = [ - new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}), - new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}), - new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}) - ]; - dataStore.save(resources, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets') - .expect(204, '') - .end(helper.checkResults(done, function() { - // Verify that all resources were deleted - dataStore.getCollection('/api/pets', function(err, resources) { - if (err) { - return done(err); - } - expect(resources).to.have.lengthOf(0); - done(); - }); - })); - }); - }); - } - ); - - it('should return an empty array if nothing was deleted', - function(done) { - helper.initTest(api, function(supertest) { - supertest - .delete('/api/pets') - .expect(200, []) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return nothing if nothing was deleted and the Swagger API schema is an object', - function(done) { - api.paths['/pets'].delete.responses[200].schema = {}; - - helper.initTest(api, function(supertest) { - supertest - .delete('/api/pets') - .expect(200, '') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return `res.body` if already set by other middleware', - function(done) { - function messWithTheBody(req, res, next) { - res.body = {message: 'Not the response you expected'}; - next(); - } - - helper.initTest(messWithTheBody, api, function(supertest) { - supertest - .delete('/api/pets') - .expect(200, {message: 'Not the response you expected'}) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return a 500 error if a DataStore open error occurs', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - dataStore.__openDataStore = function(collection, callback) { - setImmediate(callback, new Error('Test Error')); - }; - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets') - .expect(500) - .end(function(err, res) { - if (err) { - return done(err); - } - expect(res.text).to.contain('Error: Test Error'); - done(); - }); - }); - } - ); - - it('should return a 500 error if a DataStore update error occurs', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - dataStore.__saveDataStore = function(collection, data, callback) { - setImmediate(callback, new Error('Test Error')); - }; - - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets') - .expect(500) - .end(function(err, res) { - if (err) { - return done(err); - } - expect(res.text).to.contain('Error: Test Error'); - done(); - }); - }); - }); - } - ); - - describe('different data types', function() { - it('should delete a string', - function(done) { - // Create a 200 response to return a string - api.paths['/pets'].delete.responses['200'] = { - description: '200 response', - schema: { - type: 'array', - items: { - type: 'string' - } - } - }; - - // Create a string resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the string resource - supertest - .delete('/api/pets') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, ['I am Fido']) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should delete an empty string', - function(done) { - // Create a 200 response to return a string - api.paths['/pets'].delete.responses['200'] = { - description: '200 response', - schema: { - type: 'array', - items: { - type: 'string' - } - } - }; - - // Create an empty string resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', ''); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the string resource - supertest - .delete('/api/pets') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, ['']) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should delete a number', - function(done) { - // Create a 200 response to return a number - api.paths['/pets'].delete.responses['200'] = { - description: '200 response', - schema: { - type: 'array', - items: { - type: 'number' - } - } - }; - - // Create a number resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 42.999); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the number resource - supertest - .delete('/api/pets') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, [42.999]) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should delete a date', - function(done) { - // Create a 200 response to return a date - api.paths['/pets'].delete.responses['200'] = { - description: '200 response', - schema: { - type: 'array', - items: { - type: 'string', - format: 'date' - } - } - }; - - // Create a date resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Date(Date.UTC(2000, 1, 2, 3, 4, 5, 6))); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the date resource - supertest - .delete('/api/pets') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, ['2000-02-02']) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should delete a Buffer (as a string)', - function(done) { - // Create a 200 response to return a Buffer - api.paths['/pets'].delete.responses['200'] = { - description: '200 response', - schema: { - type: 'array', - items: { - type: 'string' - } - } - }; - - // Create a Buffer resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Buffer('hello world')); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the Buffer resource - supertest - .delete('/api/pets') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, ['hello world']) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should delete a Buffer (as JSON)', - function(done) { - // Create a 200 response to return a Buffer - api.paths['/pets'].delete.responses['200'] = { - description: '200 response', - schema: { - type: 'array', - items: {} - } - }; - - // Create a Buffer resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Buffer('hello world')); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the Buffer resource - supertest - .delete('/api/pets') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, [ - { - type: 'Buffer', - data: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] - } - ]) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should delete an undefined value', - function(done) { - // Create a 200 response to return an object - api.paths['/pets'].delete.responses['200'] = { - description: '200 response', - schema: { - type: 'array', - items: {} - } - }; - - // Create a resource with no value - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the undefined resource - supertest - .delete('/api/pets') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, [null]) // <--- [undefined] is serialized as [null] in JSON - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should delete a null value', - function(done) { - // Create a 200 response to return an object - api.paths['/pets'].delete.responses['200'] = { - description: '200 response', - schema: { - type: 'array', - items: {} - } - }; - - // Create a resource with a null value - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', null); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the null resource - supertest - .delete('/api/pets') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, [null]) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should delete multipart/form-data', - function(done) { - // Create a 200 response to return an object - api.paths['/pets/{PetName}/photos'].delete.responses[200] = { - description: '200 response', - schema: { - type: 'array', - items: {} - } - }; - - helper.initTest(api, function(supertest) { - // Save a pet photo (multipart/form-data) - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function() { - // Delete the photo - supertest - .delete('/api/pets/Fido/photos') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200) - .end(helper.checkResults(done, function(res2) { - expect(res2.body).to.deep.equal([ - { - ID: res2.body[0].ID, - Label: 'Photo 1', - Description: 'A photo of Fido', - Photo: { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res2.body[0].Photo.name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res2.body[0].Photo.path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - } - ]); - done(); - })); - })); - }); - } - ); - - it('should delete a file', - function(done) { - // Create a 200 response to return a file - api.paths['/pets/{PetName}/photos'].delete.responses[200] = { - description: '200 response', - schema: { - type: 'array', - items: { - type: 'file' - } - } - }; - - helper.initTest(api, function(supertest) { - // Save a pet photo (multipart/form-data) - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function() { - // Delete the photo - supertest - .delete('/api/pets/Fido/photos') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200) - .end(helper.checkResults(done, function(res2) { - // It should NOT be an attachment - expect(res2.headers['content-disposition']).to.be.undefined; - - // There's no such thing as an "array of files", - // so we send back an array of file info - expect(res2.body).to.deep.equal([ - { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res2.body[0].name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res2.body[0].path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - ]); - done(); - })); - })); - }); - } - ); - - it('should delete a file attachment', - function(done) { - // Create a 200 response to return a file - api.paths['/pets/{PetName}/photos'].delete.responses[200] = { - description: '200 response', - schema: { - type: 'array', - items: { - type: 'file' - } - }, - headers: { - 'location': { - type: 'string' - }, - 'content-disposition': { - type: 'string' - } - } - }; - - helper.initTest(api, function(supertest) { - // Save a pet photo (multipart/form-data) - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function() { - // Delete the photo - supertest - .delete('/api/pets/Fido/photos') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200) - - // Since there are multiple files, Content-Disposition is the "file name" of the URL - .expect('Content-Disposition', 'attachment; filename="photos"') - - .end(helper.checkResults(done, function(res2) { - // There's no such thing as an "array of files", - // so we send back an array of file info - expect(res2.body).to.deep.equal([ - { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res2.body[0].name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res2.body[0].path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - ]); - done(); - })); - })); - }); - } - ); - }); - - describe('filter', function() { - var Fido = { - Name: 'Fido', Age: 4, Type: 'dog', Tags: ['big', 'brown'], - Vet: {Name: 'Vet 1', Address: {Street: '123 First St.', City: 'New York', State: 'NY', ZipCode: 55555}} - }; - var Fluffy = { - Name: 'Fluffy', Age: 7, Type: 'cat', Tags: ['small', 'furry', 'white'], - Vet: {Name: 'Vet 2', Address: {Street: '987 Second St.', City: 'Dallas', State: 'TX', ZipCode: 44444}} - }; - var Polly = { - Name: 'Polly', Age: 1, Type: 'bird', Tags: ['small', 'blue'], - Vet: {Name: 'Vet 1', Address: {Street: '123 First St.', City: 'New York', State: 'NY', ZipCode: 55555}} - }; - var Lassie = { - Name: 'Lassie', Age: 7, Type: 'dog', Tags: ['big', 'furry', 'brown'], - Vet: {Name: 'Vet 3', Address: {Street: '456 Pet Blvd.', City: 'Manhattan', State: 'NY', ZipCode: 56565}} - }; - var Spot = { - Name: 'Spot', Age: 4, Type: 'dog', Tags: ['big', 'spotted'], - Vet: {Name: 'Vet 2', Address: {Street: '987 Second St.', City: 'Dallas', State: 'TX', ZipCode: 44444}} - }; - var Garfield = { - Name: 'Garfield', Age: 7, Type: 'cat', Tags: ['orange', 'fat'], - Vet: {Name: 'Vet 4', Address: {Street: '789 Pet Lane', City: 'New York', State: 'NY', ZipCode: 66666}} - }; - var allPets = [Fido, Fluffy, Polly, Lassie, Spot, Garfield]; - - var dataStore; - beforeEach(function(done) { - dataStore = new swagger.MemoryDataStore(); - var resources = allPets.map(function(pet) { - return new swagger.Resource('/api/pets', pet.Name, pet); - }); - dataStore.save(resources, done); - }); - - it('should filter by a string property', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Type=cat') - .expect(200, [Fluffy, Garfield]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fido, Polly, Lassie, Spot]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should filter by a numeric property', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Age=4') - .expect(200, [Fido, Spot]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fluffy, Polly, Lassie, Garfield]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should filter by an array property (single value)', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Tags=big') - .expect(200, [Fido, Lassie, Spot]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fluffy, Polly, Garfield]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should filter by an array property (multiple values, comma-separated)', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Tags=big,brown') - .expect(200, [Fido, Lassie]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fluffy, Polly, Spot, Garfield]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should filter by an array property (multiple values, pipe-separated)', - function(done) { - _.find(api.paths['/pets'].delete.parameters, {name: 'Tags'}).collectionFormat = 'pipes'; - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Tags=big|brown') - .expect(200, [Fido, Lassie]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fluffy, Polly, Spot, Garfield]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should filter by an array property (multiple values, space-separated)', - function(done) { - _.find(api.paths['/pets'].delete.parameters, {name: 'Tags'}).collectionFormat = 'ssv'; - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Tags=big%20brown') - .expect(200, [Fido, Lassie]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fluffy, Polly, Spot, Garfield]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should filter by an array property (multiple values, repeated)', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Tags=big&Tags=brown') - .expect(200, [Fido, Lassie]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fluffy, Polly, Spot, Garfield]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should filter by multiple properties', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Age=7&Type=cat&Tags=orange') - .expect(200, [Garfield]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fido, Fluffy, Polly, Lassie, Spot]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should filter by a deep property', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Vet.Address.State=NY') - .expect(200, [Fido, Polly, Lassie, Garfield]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fluffy, Spot]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should filter by multiple deep properties', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Vet.Address.State=NY&Vet.Address.City=New%20York') - .expect(200, [Fido, Polly, Garfield]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fluffy, Lassie, Spot]) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should not filter by properties that aren\'t defined in the Swagger API', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Name=Lassie&Vet.Address.Street=123%20First%20St.') - .expect(200, allPets) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, []) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should only filter by properties that are defined in the Swagger API', - function(done) { - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets?Age=4&Name=Lassie&Vet.Name=Vet%202&Vet.Address.Street=123%20First%20St.') - .expect(200, [Spot]) - .end(helper.checkResults(done, function() { - // Verify that the right pets were deleted - supertest - .get('/api/pets') - .expect(200, [Fido, Fluffy, Polly, Lassie, Garfield]) - .end(helper.checkResults(done)); - })); - }); - } - ); - }); - }); -}); - diff --git a/tests/specs/mock/delete-resource.spec.js b/tests/specs/mock/delete-resource.spec.js deleted file mode 100644 index 41bffc5..0000000 --- a/tests/specs/mock/delete-resource.spec.js +++ /dev/null @@ -1,628 +0,0 @@ -var swagger = require('../../../'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'); - -describe('Edit Resource Mock', function() { - describe('DELETE', function() { - 'use strict'; - - var api; - beforeEach(function() { - api = _.cloneDeep(files.parsed.petStore); - }); - - it('should delete a resource', - function(done) { - helper.initTest(api, function(supertest) { - // Create a new pet - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201) - .expect('Location', '/api/pets/Fido') - .end(helper.checkResults(done, function(res1) { - // Delete the pet - supertest - .delete('/api/pets/Fido') - .expect(204, '') - .end(helper.checkResults(done, function(res2) { - // Confirm that it was deleted - supertest - .get('/api/pets/Fido') - .expect(404) - .end(done); - })); - })); - }); - } - ); - - it('should delete a non-existent resource', - function(done) { - helper.initTest(api, function(supertest) { - // Delete a pet that doesn't exist - supertest - .delete('/api/pets/Fido') - .expect(204, '') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return the deleted resource if the Swagger API schema is an object', - function(done) { - // Create a 200 response to return the deleted pet - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {} - }; - - helper.initTest(api, function(supertest) { - // Create a new pet - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201) - .end(helper.checkResults(done, function(res1) { - // Delete the pet - supertest - .delete('/api/pets/Fido') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should return the remaining resources in the collection if the Swagger API schema is an array', - function(done) { - // Create a 200 response to return all pets in the collection - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {type: 'array', items: {}} - }; - - // Populate the collection - var dataStore = new swagger.MemoryDataStore(); - var resources = [ - new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}), - new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}), - new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}) - ]; - dataStore.save(resources, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete one of the pets - supertest - .delete('/api/pets/Fido') - .expect(200, [ - // The deleted pet should NOT be returned. Only the rest of the collection - {Name: 'Fluffy', Type: 'cat'}, - {Name: 'Polly', Type: 'bird'} - ]) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the deleted resource if the Swagger API schema is a wrapped object', - function(done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: { - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {}, - result: _.cloneDeep(api.definitions.pet) - } - } - }; - - helper.initTest(api, function(supertest) { - // Create a new pet - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201) - .end(helper.checkResults(done, function(res1) { - // Delete the pet - supertest - .delete('/api/pets/Fido') - .expect(200, {code: 42, message: 'hello world', result: {Name: 'Fido', Type: 'dog'}}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should return the remaining resources in the collection if the Swagger API schema is a wrapped array', - function(done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: { - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {}, - result: {type: 'array', items: _.cloneDeep(api.definitions.pet)} - } - } - }; - - // Populate the collection - var dataStore = new swagger.MemoryDataStore(); - var resources = [ - new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}), - new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}), - new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}) - ]; - dataStore.save(resources, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete one of the pets - supertest - .delete('/api/pets/Fido') - .expect(200, { - code: 42, - message: 'hello world', - result: [ - // The deleted pet should NOT be returned. Only the rest of the collection - {Name: 'Fluffy', Type: 'cat'}, - {Name: 'Polly', Type: 'bird'} - ] - }) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should not return the deleted resource on a 204 response, even if the Swagger API schema is an object', - function(done) { - // 204 responses cannot return data - api.paths['/pets/{PetName}'].delete.responses['204'].schema = {}; - - helper.initTest(api, function(supertest) { - // Create a new pet - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201) - .end(helper.checkResults(done, function(res1) { - // Delete the pet - supertest - .delete('/api/pets/Fido') - .expect(204, '') - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should return nothing if nothing was deleted, even if the Swagger API schema is an object', - function(done) { - // Create a 200 response to return the deleted pet - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {} - }; - - helper.initTest(api, function(supertest) { - // Delete a non-existent pet - supertest - .delete('/api/pets/Fido') - .expect(200, '') // <--- empty results - .end(helper.checkResults(done)); - }); - } - ); - - it('should return an empty collection if nothing was deleted, even if the Swagger API schema is an array', - function(done) { - // Create a 200 response to return all pets in the collection - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {type: 'array', items: {}} - }; - - helper.initTest(api, function(supertest) { - // Delete a non-existent pet from an empty collection - supertest - .delete('/api/pets/Fido') - .expect(200, []) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return `res.body` if already set by other middleware', - function(done) { - // Create a 200 response to return the deleted pet - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {} - }; - - function messWithTheBody(req, res, next) { - res.body = ['Not', 'the', 'response', 'you', 'expected']; - next(); - } - - helper.initTest(messWithTheBody, api, function(supertest) { - supertest - .delete('/api/pets/Fido') - .expect(200, ['Not', 'the', 'response', 'you', 'expected']) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return a 500 error if a DataStore error occurs', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - dataStore.__openDataStore = function(collection, callback) { - setImmediate(callback, new Error('Test Error')); - }; - - helper.initTest(dataStore, api, function(supertest) { - supertest - .delete('/api/pets/Fido') - .expect(500) - .end(function(err, res) { - if (err) { - return done(err); - } - expect(res.text).to.contain('Error: Test Error'); - done(); - }); - }); - } - ); - - describe('different data types', function() { - it('should return a string', - function(done) { - // Create a 200 response to return a string - api.paths['/pets/{PetName}'].delete.produces = ['text/plain']; - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {type: 'string'} - }; - - // Create a string resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the string resource - supertest - .delete('/api/pets/Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return an empty string', - function(done) { - // Create a 200 response to return a string - api.paths['/pets/{PetName}'].delete.produces = ['text/plain']; - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {type: 'string'} - }; - - // Create an empty string resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', ''); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the string resource - supertest - .delete('/api/pets/Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, '') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a number', - function(done) { - // Create a 200 response to return a number - api.paths['/pets/{PetName}'].delete.produces = ['text/plain']; - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {type: 'number'} - }; - - // Create a number resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 42.999); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the number resource - supertest - .delete('/api/pets/Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, '42.999') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a date', - function(done) { - // Create a 200 response to return a date - api.paths['/pets/{PetName}'].delete.produces = ['text/plain']; - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {type: 'string', format: 'date-time'} - }; - - // Create a date resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Date(Date.UTC(2000, 1, 2, 3, 4, 5, 6))); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the date resource - supertest - .delete('/api/pets/Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, '2000-02-02T03:04:05.006Z') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a Buffer (as a string)', - function(done) { - // Create a 200 response to return a Buffer - api.paths['/pets/{PetName}'].delete.produces = ['text/plain']; - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {type: 'string'} - }; - - // Create a Buffer resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Buffer('hello world')); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the Buffer resource - supertest - .delete('/api/pets/Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, 'hello world') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a Buffer (as JSON)', - function(done) { - // Create a 200 response to return a Buffer - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {} - }; - - // Create a Buffer resource - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Buffer('hello world')); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the Buffer resource - supertest - .delete('/api/pets/Fido') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, { - type: 'Buffer', - data: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] - }) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return an undefined value', - function(done) { - // Create a 200 response to return an object - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {} - }; - - // Create a resource with no value - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido'); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the undefined resource - supertest - .delete('/api/pets/Fido') - .expect('Content-Type', 'application/json') - .expect(200, '') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a null value', - function(done) { - // Create a 200 response to return an object - api.paths['/pets/{PetName}'].delete.responses['200'] = { - description: '200 response', - schema: {} - }; - - // Create a resource with a null value - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', null); - dataStore.save(resource, function() { - - helper.initTest(dataStore, api, function(supertest) { - // Delete the null resource - supertest - .delete('/api/pets/Fido') - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, 'null') - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return multipart/form-data', - function(done) { - // Create a 200 response to return an object - api.paths['/pets/{PetName}/photos/{ID}'].delete.responses[200] = { - description: '200 response', - schema: {} - }; - - helper.initTest(api, function(supertest) { - // Save a pet photo (multipart/form-data) - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function(res1) { - - // Delete the photo - supertest - .delete(res1.headers.location) - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200) - .end(helper.checkResults(done, function(res2) { - expect(res2.body).to.deep.equal({ - ID: res2.body.ID, - Label: 'Photo 1', - Description: 'A photo of Fido', - Photo: { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res2.body.Photo.name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res2.body.Photo.path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - }); - done(); - })); - })); - }); - } - ); - - it('should return a file', - function(done) { - // Create a 200 response to return a file - api.paths['/pets/{PetName}/photos/{ID}'].delete.responses[200] = { - description: '200 response', - schema: {type: 'file'} - }; - - helper.initTest(api, function(supertest) { - // Save a pet photo (multipart/form-data) - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function(res1) { - - // Delete the photo - supertest - .delete(res1.headers.location) - .expect('Content-Type', 'image/jpeg') - .expect(200) - .end(helper.checkResults(done, function(res2) { - // It should NOT be an attachment - expect(res2.headers['content-disposition']).to.be.undefined; - - expect(res2.body).to.be.an.instanceOf(Buffer); - expect(res2.body.length).to.equal(683709); - done(); - })); - })); - }); - } - ); - - it('should return a file attachment', - function(done) { - // Create a 200 response to return a file - api.paths['/pets/{PetName}/photos/{ID}'].delete.responses[200] = { - description: '200 response', - schema: {type: 'file'}, - headers: { - 'location': { - type: 'string' - }, - 'content-disposition': { - type: 'string', - default: 'attachment' - } - } - }; - - helper.initTest(api, function(supertest) { - // Save a pet photo (multipart/form-data) - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function(res1) { - // Get the file name from the "Location" HTTP header - var fileName = res1.headers.location.match(/\d+$/)[0]; - - // Delete the photo - supertest - .delete(res1.headers.location) - .expect('Content-Type', 'image/jpeg') - .expect(200) - .expect('Content-Disposition', 'attachment; filename="' + fileName + '"') - .end(helper.checkResults(done, function(res2) { - expect(res2.body).to.be.an.instanceOf(Buffer); - expect(res2.body.length).to.equal(683709); - done(); - })); - })); - }); - } - ); - }); - }); -}); - diff --git a/tests/specs/mock/edit-collection.spec.js b/tests/specs/mock/edit-collection.spec.js deleted file mode 100644 index 1077512..0000000 --- a/tests/specs/mock/edit-collection.spec.js +++ /dev/null @@ -1,1302 +0,0 @@ -var swagger = require('../../../'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'); - -describe('Edit Collection Mock', function() { - ['patch', 'put', 'post'].forEach(function(method) { - describe(method.toUpperCase(), function() { - 'use strict'; - - var api; - beforeEach(function() { - api = _.cloneDeep(files.parsed.petStore); - api.paths['/pets'][method] = api.paths['/pets'].post; - api.paths['/pets/{PetName}/photos'][method] = api.paths['/pets/{PetName}/photos'].post; - }); - - // Modifies the "/pets" schema to allow an array of pets - function arrayify() { - var petParam = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}); - petParam.schema = { - type: 'array', - items: petParam.schema - }; - } - - describe('Shared tests', function() { - it('should add a new resource to the collection', - function(done) { - helper.initTest(api, function(supertest) { - // Create a new pet - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, '') - .end(helper.checkResults(done, function() { - // Retrieve the pet - supertest - .get('/api/pets/Fido') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should add multiple resources to the collection', - function(done) { - arrayify(); - helper.initTest(api, function(supertest) { - // Create some new pets - supertest - [method]('/api/pets') - .send([{Name: 'Fido', Type: 'dog'}, {Name: 'Fluffy', Type: 'cat'}, {Name: 'Polly', Type: 'bird'}]) - .expect(201, '') - .end(helper.checkResults(done, function() { - // Retrieve a pet by name - supertest - .get('/api/pets/Fluffy') - .expect(200, {Name: 'Fluffy', Type: 'cat'}) - .end(helper.checkResults(done, function() { - // Retrieve all the pets - supertest - .get('/api/pets') - .expect(200, [ - {Name: 'Fido', Type: 'dog'}, - {Name: 'Fluffy', Type: 'cat'}, - {Name: 'Polly', Type: 'bird'} - ]) - .end(helper.checkResults(done)); - })); - })); - }); - } - ); - - it('should add zero resources to the collection', - function(done) { - arrayify(); - helper.initTest(api, function(supertest) { - // Save zero pets - supertest - [method]('/api/pets') - .send([]) - .expect(201, '') - .end(helper.checkResults(done, function() { - // Retrieve all the pets (empty array) - supertest - .get('/api/pets') - .expect(200, []) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should not return data if not specified in the Swagger API', - function(done) { - delete api.paths['/pets'][method].responses[201].schema; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, '') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return the new resource if the Swagger API schema is an object', - function(done) { - api.paths['/pets'][method].responses[201].schema = {}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the first new resource if the Swagger API schema is an object', - function(done) { - api.paths['/pets'][method].responses[201].schema = {}; - arrayify(); - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets') - .send([{Name: 'Fido', Type: 'dog'}, {Name: 'Polly', Type: 'bird'}]) - .expect(201, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the first new resource if the Swagger API schema is a wrapped object', - function(done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets'][method].responses[201].schema = { - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {}, - result: _.cloneDeep(api.definitions.pet) - } - }; - arrayify(); - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets') - .send([{Name: 'Fido', Type: 'dog'}, {Name: 'Polly', Type: 'bird'}]) - .expect(201, {code: 42, message: 'hello world', result: {Name: 'Fido', Type: 'dog'}}) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the whole collection (including the new resource) if the Swagger API schema is an array', - function(done) { - api.paths['/pets'][method].responses[201].schema = {type: 'array', items: {}}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, [{Name: 'Fluffy', Type: 'cat'}, {Name: 'Fido', Type: 'dog'}]) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the whole collection (including the new resources) if the Swagger API schema is an array', - function(done) { - arrayify(); - api.paths['/pets'][method].responses[201].schema = {type: 'array', items: {}}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets') - .send([{Name: 'Fido', Type: 'dog'}, {Name: 'Polly', Type: 'bird'}]) - .expect(201, [{Name: 'Fluffy', Type: 'cat'}, {Name: 'Fido', Type: 'dog'}, {Name: 'Polly', Type: 'bird'}]) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the whole collection (including the new resources) if the Swagger API schema is a wrapped array', - function(done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets'][method].responses[201].schema = { - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {}, - result: {type: 'array', items: _.cloneDeep(api.definitions.pet)} - } - }; - arrayify(); - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets') - .send([{Name: 'Fido', Type: 'dog'}, {Name: 'Polly', Type: 'bird'}]) - .expect(201, { - code: 42, - message: 'hello world', - result: [ - {Name: 'Fluffy', Type: 'cat'}, - {Name: 'Fido', Type: 'dog'}, - {Name: 'Polly', Type: 'bird'} - ] - }) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return `res.body` if already set by other middleware', - function(done) { - api.paths['/pets'][method].responses[201].schema = {type: 'array', items: {}}; - - function messWithTheBody(req, res, next) { - res.body = {message: 'Not the response you expected'}; - next(); - } - - helper.initTest(messWithTheBody, api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, {message: 'Not the response you expected'}) - .end(helper.checkResults(done)); - }); - } - ); - - it('should set the "Location" HTTP header to new resource\'s URL', - function(done) { - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, '') - .expect('Location', '/api/pets/Fido') - .end(helper.checkResults(done)); - }); - } - ); - - it('should set the "Location" HTTP header to the collection URL', - function(done) { - arrayify(); - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send([{Name: 'Fido', Type: 'dog'}, {Name: 'Fluffy', Type: 'cat'}, {Name: 'Polly', Type: 'bird'}]) - .expect(201, '') - .expect('Location', '/api/pets') - .end(helper.checkResults(done)); - }); - } - ); - - it('should set the "Location" HTTP header to the collection URL, even though it\'s empty', - function(done) { - arrayify(); - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send([]) - .expect(201, '') - .expect('Location', '/api/pets') - .end(helper.checkResults(done)); - }); - } - ); - - it('should not set the "Location" HTTP header if not specified in the Swagger API (single object)', - function(done) { - delete api.paths['/pets'][method].responses[201].headers; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, '') - .end(helper.checkResults(done, function(res) { - expect(res.headers.location).to.be.undefined; - done(); - })); - }); - } - ); - - it('should not set the "Location" HTTP header if not specified in the Swagger API (array)', - function(done) { - delete api.paths['/pets'][method].responses[201].headers; - arrayify(); - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send([{Name: 'Fido', Type: 'dog'}, {Name: 'Fluffy', Type: 'cat'}, {Name: 'Polly', Type: 'bird'}]) - .expect(201, '') - .end(helper.checkResults(done, function(res) { - expect(res.headers.location).to.be.undefined; - done(); - })); - }); - } - ); - - it('should return a 500 error if a DataStore open error occurs', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - dataStore.__openDataStore = function(collection, callback) { - setImmediate(callback, new Error('Test Error')); - }; - - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(500) - .end(function(err, res) { - if (err) { - return done(err); - } - expect(res.text).to.contain('Error: Test Error'); - done(); - }); - }); - } - ); - - it('should return a 500 error if a DataStore update error occurs', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - dataStore.__saveDataStore = function(collection, data, callback) { - setImmediate(callback, new Error('Test Error')); - }; - - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(500) - .end(function(err, res) { - if (err) { - return done(err); - } - expect(res.text).to.contain('Error: Test Error'); - done(); - }); - }); - } - ); - }); - - describe('Determining resource names (by data type)', function() { - it('should support strings', - function(done) { - _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema = {type: 'string'}; - api.paths['/pets'][method].responses[201].schema = {type: 'string'}; - api.paths['/pets'][method].consumes = ['text/plain']; - api.paths['/pets'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .set('Content-Type', 'text/plain') - .send('I am Fido') - .expect(201, 'I am Fido') - .expect('Location', '/api/pets/I%20am%20Fido') - .end(helper.checkResults(done)); - }); - } - ); - - it('should support empty strings', - function(done) { - _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema = {type: 'string'}; - api.paths['/pets'][method].responses[201].schema = {type: 'string'}; - api.paths['/pets'][method].consumes = ['text/plain']; - api.paths['/pets'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .set('Content-Type', 'text/plain') - .send('') - .expect(201, '') - .expect('Location', '/api/pets/') - .end(helper.checkResults(done)); - }); - } - ); - - it('should support very large strings', - function(done) { - _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema = {type: 'string'}; - api.paths['/pets'][method].responses[201].schema = {type: 'string'}; - api.paths['/pets/{PetName}'].get.responses[200].schema = {type: 'string'}; - api.paths['/pets'][method].consumes = ['text/plain']; - api.paths['/pets'][method].produces = ['text/plain']; - api.paths['/pets/{PetName}'].get.produces = ['text/plain']; - helper.initTest(api, function(supertest) { - var veryLongString = _.repeat('abcdefghijklmnopqrstuvwxyz', 5000); - - supertest - [method]('/api/pets') - .set('Content-Type', 'text/plain') - .send(veryLongString) - - // The full value should be returned - .expect(201, veryLongString) - - // The resource URL should be truncated to 2000 characters, for compatibility with some browsers - .expect('Location', '/api/pets/' + veryLongString.substring(0, 2000)) - .end(helper.checkResults(done, function(res) { - - // Verify that the full value was stored - supertest - .get(res.headers.location) - .expect(200, veryLongString) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should support numbers', - function(done) { - _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema = {type: 'number'}; - api.paths['/pets'][method].responses[201].schema = {type: 'number'}; - api.paths['/pets'][method].consumes = ['text/plain']; - api.paths['/pets'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .set('Content-Type', 'text/plain') - .send('42.999') - .expect(201, '42.999') - .expect('Location', '/api/pets/42.999') - .end(helper.checkResults(done)); - }); - } - ); - - it('should support dates', - function(done) { - _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema = {type: 'string', format: 'date'}; - api.paths['/pets'][method].responses[201].schema = {type: 'string', format: 'date'}; - api.paths['/pets'][method].consumes = ['text/plain']; - api.paths['/pets'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .set('Content-Type', 'text/plain') - .send('2000-01-02') - .expect(201, '2000-01-02') - .expect('Location', '/api/pets/2000-01-02') - .end(helper.checkResults(done)); - }); - } - ); - - it('should support date-times', - function(done) { - _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema = {type: 'string', format: 'date-time'}; - api.paths['/pets'][method].responses[201].schema = {type: 'string', format: 'date-time'}; - api.paths['/pets'][method].consumes = ['text/plain']; - api.paths['/pets'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .set('Content-Type', 'text/plain') - .send('2000-01-02T03:04:05.006Z') - .expect(201, '2000-01-02T03:04:05.006Z') - .expect('Location', '/api/pets/2000-01-02T03%3A04%3A05.006Z') - .end(helper.checkResults(done)); - }); - } - ); - - it('should support Buffers (as a string)', - function(done) { - _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema = {type: 'string'}; - api.paths['/pets'][method].responses[201].schema = {type: 'string'}; - api.paths['/pets'][method].consumes = ['text/plain']; - api.paths['/pets'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .set('Content-Type', 'text/plain') - .send(new Buffer('hello world').toString()) - .expect(201, 'hello world') - .expect('Location', '/api/pets/hello%20world') - .end(helper.checkResults(done)); - }); - } - ); - - it('should support Buffers (as JSON)', - function(done) { - _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema = {}; - api.paths['/pets'][method].responses[201].schema = {}; - api.paths['/pets'][method].consumes = ['application/octet-stream']; - api.paths['/pets'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .set('Content-Type', 'application/octet-stream') - .send(new Buffer('hello world').toString()) - .expect(201, { - type: 'Buffer', - data: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] - }) - .end(helper.checkResults(done, function(res) { - // The "Location" header should be set to an auto-generated value, - // since a Buffer has no "name" field - expect(res.headers.location).to.match(/^\/api\/pets\/\d+$/); - done(); - })); - }); - } - ); - - it('should support undefined values', - function(done) { - var petParam = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}); - petParam.schema = {}; - petParam.required = false; - api.paths['/pets'][method].responses[201].schema = {}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .set('Content-Type', 'text/plain') - .expect(201, '') - .end(helper.checkResults(done, function(res) { - expect(res.headers.location).to.match(/^\/api\/pets\/\w+$/); - done(); - })); - }); - } - ); - - it('should support multipart/form-data', - function(done) { - api.paths['/pets/{PetName}/photos'][method].responses[201].schema = {}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function(res) { - expect(res.headers.location).to.match(/^\/api\/pets\/Fido\/photos\/\d+$/); - expect(res.body).to.deep.equal({ - ID: res.body.ID, - Label: 'Photo 1', - Description: 'A photo of Fido', - Photo: { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res.body.Photo.name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res.body.Photo.path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - }); - done(); - })); - }); - } - ); - - it('should support files', - function(done) { - api.paths['/pets/{PetName}/photos'][method].responses[201].schema = {type: 'file'}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function(res) { - expect(res.headers.location).to.match(/^\/api\/pets\/Fido\/photos\/\d+$/); - expect(res.body).to.be.an.instanceOf(Buffer); - expect(res.body.length).to.equal(683709); - done(); - })); - }); - } - ); - }); - - describe('Determining resource names (by property names)', function() { - it('should determine the resource name from "Name" properties in its schema', - function(done) { - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect('Location', '/api/pets/Fido') - .end(helper.checkResults(done, function() { - supertest - .get('/api/pets/Fido') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should determine the resource name from "Name" properties in its schema, even if they\'re not present in the data', - function(done) { - var schemaProps = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema.properties; - schemaProps.ID = {type: 'integer'}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done, function(res) { - // An "ID" property should have been generated and used for the "Location" header - expect(res.headers.location).to.match(/^\/api\/pets\/\d+$/); - - // Extract the ID from the "Location" HTTP header - var petID = parseInt(res.headers.location.match(/\d+$/)[0]); - expect(petID).not.to.satisfy(isNaN); - expect(petID).to.satisfy(isFinite); - - // Verify that the ID property was set on the object - supertest - .get(res.headers.location) - .expect(200, {ID: petID, Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should determine the resource name from "Name" properties in its data, even if they\'re not in the schema', - function(done) { - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({ID: 12345, Name: 'Fido', Type: 'dog'}) // <--- "ID" is not in the schema. "Name" is. - .expect('Location', '/api/pets/12345') // <--- "ID" is used instead of "Name" - .end(helper.checkResults(done, function() { - supertest - .get('/api/pets/12345') - .expect(200, {ID: 12345, Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should use a "byte" property in the schema as the resource name', - function(done) { - var schemaProps = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema.properties; - schemaProps.ID = {type: 'string', format: 'byte'}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done, function(res) { - // An "ID" property should have been generated and used for the "Location" header - expect(res.headers.location).to.match(/^\/api\/pets\/\d{1,3}$/); - - // Extract the ID from the "Location" HTTP header - var petID = parseInt(res.headers.location.match(/\d+$/)[0]); - expect(petID).not.to.satisfy(isNaN); - expect(petID).to.satisfy(isFinite); - - // Verify that the ID property was set on the object - supertest - .get(res.headers.location) - .expect(200, {ID: petID, Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should use a "boolean" property in the schema as the resource name', - function(done) { - var schemaProps = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema.properties; - schemaProps.ID = {type: 'boolean'}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done, function(res) { - // An "ID" property should have been generated and used for the "Location" header - expect(res.headers.location).to.match(/^\/api\/pets\/(true|false)$/); - - // Extract the ID from the "Location" HTTP header - var petID = res.headers.location.match(/(true|false)$/)[0] === 'true'; - - // Verify that the ID property was set on the object - supertest - .get(res.headers.location) - .expect(200, {ID: petID, Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should use a "boolean" property in the data as the resource name', - function(done) { - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({ID: false, Name: 'Fido', Type: 'dog'}) - .expect('Location', '/api/pets/false') - .end(helper.checkResults(done, function() { - supertest - .get('/api/pets/false') - .expect(200, {ID: false, Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should use a "date" property in the schema as the resource name', - function(done) { - var schemaProps = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema.properties; - schemaProps.Key = { - type: 'string', - format: 'date' - }; - api.paths['/pets/{PetName}'].get.responses[200].schema.properties = schemaProps; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Key: '2005-11-09', Name: 'Fido', Type: 'dog'}) - .expect('Location', '/api/pets/2005-11-09') - .end(helper.checkResults(done, function(res) { - supertest - .get('/api/pets/2005-11-09') - .expect(200, {Key: '2005-11-09', Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should use a "date-time" property in the schema as the resource name', - function(done) { - var schemaProps = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema.properties; - schemaProps.key = { - type: 'string', - format: 'date-time' - }; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({key: '2005-11-09T08:07:06.005Z', Name: 'Fido', Type: 'dog'}) - .expect('Location', '/api/pets/2005-11-09T08%3A07%3A06.005Z') - .end(helper.checkResults(done, function(res) { - supertest - .get('/api/pets/2005-11-09T08%3A07%3A06.005Z') - .expect(200, {key: '2005-11-09T08:07:06.005Z', Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should use a Date property in the data as the resource name', - function(done) { - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({code: new Date(Date.UTC(2000, 1, 2, 3, 4, 5, 6)), Name: 'Fido', Type: 'dog'}) - .expect('Location', '/api/pets/2000-02-02T03%3A04%3A05.006Z') - .end(helper.checkResults(done, function() { - supertest - .get('/api/pets/2000-02-02T03%3A04%3A05.006Z') - .expect(200, {code: '2000-02-02T03:04:05.006Z', Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should use a Date property that was added by other middleware as the resource name', - function(done) { - function messWithTheBody(req, res, next) { - if (req.method === method.toUpperCase()) { - req.body.Id = new Date(Date.UTC(2000, 1, 2, 3, 4, 5, 6)); - } - next(); - } - - helper.initTest(messWithTheBody, api, function(supertest) { - supertest - [method]('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect('Location', '/api/pets/2000-02-02T03%3A04%3A05.006Z') - .end(helper.checkResults(done, function(res) { - supertest - .get('/api/pets/2000-02-02T03%3A04%3A05.006Z') - .expect(200, {Name: 'Fido', Type: 'dog', Id: '2000-02-02T03:04:05.006Z'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should NOT use object or array properties as the resource name', - function(done) { - var petParam = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}); - petParam.schema.properties.Name.type = 'object'; - petParam.schema.required = ['Name']; - api.paths['/pets'].get.responses[200].schema.items = petParam.schema; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({ID: [1, 2, 3], Name: {fido: true}, Type: 'dog'}) // <-- Neither "ID" nor "Name" is a valid resource name - .end(helper.checkResults(done, function(res) { - // A resource name was auto-generated, since ID and Name weren't valid - expect(res.headers.location).to.match(/^\/api\/pets\/\d+$/); - - // Verify that the object remained unchanged - supertest - .get('/api/pets') - .expect(200, [{ID: [1, 2, 3], Name: {fido: true}, Type: 'dog'}]) - .end(helper.checkResults(done)); - })); - }); - } - ); - }); - - describe('Determining resource names (by required properties)', function() { - it('should use the first required property as the resource name', - function(done) { - _.remove(api.paths['/pets/{PetName}/photos'][method].parameters, {name: 'ID'}); - _.find(api.paths['/pets/{PetName}/photos/{ID}'].parameters, {name: 'ID'}).type = 'string'; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .attach('Photo', files.paths.oneMB) - .expect('Location', '/api/pets/Fido/photos/Photo%201') - .end(helper.checkResults(done, function(res) { - supertest - .get('/api/pets/Fido/photos/Photo%201') - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.be.an.instanceOf(Buffer); - expect(res.body.length).to.equal(683709); - done(); - })); - })); - }); - } - ); - - it('should NOT use object or array properties as the resource name', - function(done) { - _.remove(api.paths['/pets/{PetName}/photos'][method].parameters, {name: 'ID'}); - _.find(api.paths['/pets/{PetName}/photos/{ID}'].parameters, {name: 'ID'}).type = 'string'; - var labelParam = _.find(api.paths['/pets/{PetName}/photos'][method].parameters, {name: 'Label'}); - labelParam.type = 'array'; - labelParam.items = { - type: 'string' - }; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos') - .field('Label', 'a, b, c') - .attach('Photo', files.paths.oneMB) - .expect('Location', '/api/pets/Fido/photos/1MB.jpg') - .end(helper.checkResults(done, function(res) { - supertest - .get('/api/pets/Fido/photos/1MB.jpg') - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.be.an.instanceOf(Buffer); - expect(res.body.length).to.equal(683709); - done(); - })); - })); - }); - } - ); - }); - - describe('Determining resource names (by file name)', function() { - it('should use the client-side file name as the resource name', - function(done) { - _.remove(api.paths['/pets/{PetName}/photos'][method].parameters, {name: 'ID'}); - _.find(api.paths['/pets/{PetName}/photos'][method].parameters, {name: 'Label'}).required = false; - _.find(api.paths['/pets/{PetName}/photos/{ID}'].parameters, {name: 'ID'}).type = 'string'; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .attach('Photo', files.paths.oneMB) - .expect('Location', '/api/pets/Fido/photos/1MB.jpg') - .end(helper.checkResults(done, function(res) { - supertest - .get('/api/pets/Fido/photos/1MB.jpg') - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.be.an.instanceOf(Buffer); - expect(res.body.length).to.equal(683709); - done(); - })); - })); - }); - } - ); - - it('should use the server-side file name as the resource name', - function(done) { - _.remove(api.paths['/pets/{PetName}/photos'][method].parameters, {name: 'ID'}); - _.find(api.paths['/pets/{PetName}/photos'][method].parameters, {name: 'Label'}).required = false; - _.find(api.paths['/pets/{PetName}/photos/{ID}'].parameters, {name: 'ID'}).type = 'string'; - - function messWithTheBody(req, res, next) { - if (req.method === method.toUpperCase()) { - // Mimic the filename not being included in the Content-Disposition header - req.files.Photo.originalname = null; - } - next(); - } - - helper.initTest(messWithTheBody, api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .attach('Photo', files.paths.oneMB) - .end(helper.checkResults(done, function(res) { - expect(res.headers.location).not.to.equal('/api/pets/Fido/photos/1MB.jpg'); - expect(res.headers.location).to.match(/^\/api\/pets\/Fido\/photos\/\w+\.jpg$/); - - supertest - .get(res.headers.location) - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.be.an.instanceOf(Buffer); - expect(res.body.length).to.equal(683709); - done(); - })); - })); - }); - } - ); - - it('should use an auto-generated resource name if no file was uploaded', - function(done) { - var params = api.paths['/pets/{PetName}/photos'][method].parameters; - _.remove(api.paths['/pets/{PetName}/photos'][method].parameters, {name: 'ID'}); - _.find(params, {name: 'Label'}).required = false; - _.find(params, {name: 'Photo'}).required = false; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .end(helper.checkResults(done, function(res) { - // A resource name was auto-generated, since no file was uploaded - expect(res.headers.location).to.match(/^\/api\/pets\/Fido\/photos\/\d+$/); - - supertest - .get(res.headers.location) - .expect(410) - .end(done); - })); - }); - } - ); - - it('should use an auto-generated resource name if the body is empty', - function(done) { - var params = api.paths['/pets/{PetName}/photos'][method].parameters; - _.remove(api.paths['/pets/{PetName}/photos'][method].parameters, {name: 'ID'}); - _.find(params, {name: 'Label'}).required = false; - _.find(params, {name: 'Photo'}).required = false; - api.paths['/pets/{PetName}/photos'][method].consumes = ['text/plain', 'multipart/form-data']; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos') - .set('Content-Type', 'text/plain') - .end(helper.checkResults(done, function(res) { - // A resource name was auto-generated, since no file was uploaded - expect(res.headers.location).to.match(/^\/api\/pets\/Fido\/photos\/\d+$/); - - supertest - .get(res.headers.location) - .expect(410) - .end(done); - })); - }); - } - ); - - it('should use an auto-generated resource name if there is more than one file param', - function(done) { - var params = api.paths['/pets/{PetName}/photos'][method].parameters; - - _.remove(params, {name: 'ID'}); - _.find(params, {name: 'Label'}).required = false; - _.find(params, {name: 'Photo'}).required = false; - params.push({ - name: 'Photo2', - in: 'formData', - type: 'file' - }); - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .attach('Photo2', files.paths.oneMB) // <--- Only sending one file. But there are 2 file params - .end(helper.checkResults(done, function(res) { - // A resource name was auto-generated, since there are multiple file params - expect(res.headers.location).to.match(/^\/api\/pets\/Fido\/photos\/\d+$/); - - supertest - .get(res.headers.location) - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.be.an.instanceOf(Buffer); - expect(res.body.length).to.equal(683709); - done(); - })); - })); - }); - } - ); - }); - - describe('Auto-generated resource names', function() { - it('should generate a unique ID if no "Name" property can be determined', - function(done) { - // The schema is an empty object (no "name" properties) - _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}).schema = {}; - api.paths['/pets'][method].responses[201].schema = {}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({age: 42, dob: new Date(Date.UTC(2000, 1, 2, 3, 4, 5, 6))}) // <--- No "name" properties - .expect(201, {age: 42, dob: '2000-02-02T03:04:05.006Z'}) - .end(helper.checkResults(done, function(res) { - // The "Location" header should be set to an auto-generated value - expect(res.headers.location).to.match(/^\/api\/pets\/\d+$/); - done(); - })); - }); - } - ); - - it('should generate a string value for the resource\'s "Name" property, if not set', - function(done) { - // Make "Name" property optional - var petParam = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}); - petParam.schema.required = []; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Type: 'dog', Age: 4}) // <--- The "Name" property isn't set - .end(helper.checkResults(done, function(res) { - // A "Name" should have been generated, and used as the resource's URL - expect(res.headers.location).to.match(/^\/api\/pets\/\w+$/); - - // Extract the pet's Name from the "Location" header - var petName = res.headers.location.match(/([^\/]+)$/)[0]; - - supertest - .get(res.headers.location) - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.deep.equal({ - Type: 'dog', - Age: 4, - Name: petName - }); - done(); - })); - })); - }); - } - ); - - it('should generate a string value for the resource\'s "Name" property, even if the body is empty', - function(done) { - // Make all data optional - var petParam = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}); - petParam.required = false; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') // <--- No data was sent at all - .end(helper.checkResults(done, function(res) { - // A "Name" should have been generated, and used as the resource's URL - expect(res.headers.location).to.match(/^\/api\/pets\/\w+$/); - - // Extract the pet's Name from the "Location" header - var petName = res.headers.location.match(/([^\/]+)$/)[0]; - - supertest - .get(res.headers.location) - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.deep.equal({ - Name: petName // <--- A "Name" property was generated and added to an empty object - }); - done(); - })); - })); - }); - } - ); - - it('should generate an integer value for the resource\'s "Name" property, if not set', - function(done) { - // Make the "Name" property optional, and an integer - var petParam = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}); - petParam.schema.required = []; - petParam.schema.properties.Name.type = 'integer'; - api.paths['/pets/{PetName}'].get.responses[200].schema = petParam.schema; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Type: 'dog', Age: 4}) // <--- The "Name" property isn't set - .end(helper.checkResults(done, function(res) { - // A "Name" should have been generated, and used as the resource's URL - expect(res.headers.location).to.match(/^\/api\/pets\/\d+$/); - - // Extract the pet's Name from the "Location" header - var petNameAsString = res.headers.location.match(/([^\/]+)$/)[0]; - var petNameAsInteger = parseInt(petNameAsString); - expect(petNameAsInteger).not.to.satisfy(isNaN); - expect(petNameAsInteger).to.satisfy(isFinite); - - supertest - .get(res.headers.location) - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.deep.equal({ - Type: 'dog', - Age: 4, - Name: petNameAsInteger - }); - done(); - })); - })); - }); - } - ); - - it('should generate a date value for the resource\'s "Name" property, if not set', - function(done) { - // Make the "Name" property optional, and an integer - var petParam = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}); - petParam.schema.required = []; - petParam.schema.properties.Name.type = 'string'; - petParam.schema.properties.Name.format = 'date'; - api.paths['/pets/{PetName}'].get.responses[200].schema = petParam.schema; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Type: 'dog', Age: 4}) // <--- The "Name" property isn't set - .end(helper.checkResults(done, function(res) { - // A "Name" should have been generated, and used as the resource's URL - expect(res.headers.location).to.match(/^\/api\/pets\/\d{4}-\d\d-\d\d$/); - - // Extract the pet's Name from the "Location" header - var petNameAsString = res.headers.location.match(/([^\/]+)$/)[0]; - var petNameAsDate = new Date(petNameAsString); - expect(petNameAsDate.valueOf()).not.to.satisfy(isNaN); - expect(petNameAsDate.valueOf()).to.satisfy(isFinite); - - supertest - .get(res.headers.location) - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.deep.equal({ - Type: 'dog', - Age: 4, - Name: petNameAsString - }); - done(); - })); - })); - }); - } - ); - - it('should generate a date-time value for the resource\'s "Name" property, if not set', - function(done) { - // Make the "Name" property optional, and an integer - var petParam = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}); - petParam.schema.required = []; - petParam.schema.properties.Name.type = 'string'; - petParam.schema.properties.Name.format = 'date-time'; - api.paths['/pets/{PetName}'].get.responses[200].schema = petParam.schema; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Type: 'dog', Age: 4}) // <--- The "Name" property isn't set - .end(helper.checkResults(done, function(res) { - // A "Name" should have been generated, and used as the resource's URL - expect(res.headers.location).to.match(/^\/api\/pets\/\d{4}-\d\d-\d\dT\d\d\%3A\d\d\%3A\d\d\.\d\d\dZ$/); - - // Extract the pet's Name from the "Location" header - var petNameAsString = decodeURIComponent(res.headers.location.match(/([^\/]+)$/)[0]); - var petNameAsDate = new Date(petNameAsString); - expect(petNameAsDate.valueOf()).not.to.satisfy(isNaN); - expect(petNameAsDate.valueOf()).to.satisfy(isFinite); - - supertest - .get(res.headers.location) - .expect(200) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.deep.equal({ - Type: 'dog', - Age: 4, - Name: petNameAsString - }); - done(); - })); - })); - }); - } - ); - - it('should NOT generate an array value for the resource\'s "Name" property, if not set', - function(done) { - // Make the "Name" property optional, and an integer - var petParam = _.find(api.paths['/pets'][method].parameters, {name: 'PetData'}); - petParam.schema.required = []; - petParam.schema.properties.Name.type = 'array'; - petParam.schema.properties.Name.items = {type: 'string'}; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets') - .send({Type: 'dog', Age: 4}) // <--- The "Name" property isn't set - .end(helper.checkResults(done, function(res) { - // A "Name" property should have been auto-generated, but it should NOT be an array - expect(res.headers.location).to.match(/^\/api\/pets\/\d+$/); - done(); - })); - }); - } - ); - }); - }); - }); -}); - - diff --git a/tests/specs/mock/edit-resource.spec.js b/tests/specs/mock/edit-resource.spec.js deleted file mode 100644 index 8c18364..0000000 --- a/tests/specs/mock/edit-resource.spec.js +++ /dev/null @@ -1,696 +0,0 @@ -var swagger = require('../../../'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'); - -describe('Edit Resource Mock', function() { - ['put', 'patch', 'post'].forEach(function(method) { - describe(method.toUpperCase(), function() { - 'use strict'; - - var api; - beforeEach(function() { - api = _.cloneDeep(files.parsed.petStore); - - var operation = api.paths['/pets/{PetName}'].patch; - delete api.paths['/pets/{PetName}'].patch; - api.paths['/pets/{PetName}'][method] = operation; - - operation = api.paths['/pets/{PetName}/photos'].post; - delete api.paths['/pets/{PetName}/photos'].post; - api.paths['/pets/{PetName}/photos/{ID}'][method] = operation; - }); - - describe('Shared tests', function() { - it('should create a new resource', - function(done) { - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog', Tags: ['fluffy', 'brown']}) - .expect(200) - .end(helper.checkResults(done, function(res1) { - supertest - .get('/api/pets/Fido') - .expect(200, {Name: 'Fido', Type: 'dog', Tags: ['fluffy', 'brown']}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should create a new resource at the specified URL, even if the primary key is different', - function(done) { - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') // <-- The URL is "Fido" - .send({Name: 'Fluffy', Type: 'cat'}) // <-- The pet name is "Fluffy" - .expect(200) - .end(helper.checkResults(done, function(res1) { - - // Verify that "/api/pets/Fido" was created - supertest - .get('/api/pets/Fido') - .expect(200, {Name: 'Fluffy', Type: 'cat'}) - .end(helper.checkResults(done, function() { - - // Verify that "/api/pets/Fluffy" was NOT created - supertest - .get('/api/pets/Fluffy') - .expect(404) - .end(done); - })); - })); - }); - } - ); - - it('should create a new resource using default value in the JSON schema', - function(done) { - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.required = false; - petParam.schema.default = {Name: 'Fido', Type: 'dog'}; - petParam.schema.properties.Tags.default = 'fluffy,brown'; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .set('Content-Type', 'application/json') - .expect(200) - .end(helper.checkResults(done, function(res1) { - supertest - .get('/api/pets/Fido') - .expect(200, {Name: 'Fido', Type: 'dog', Tags: ['fluffy', 'brown']}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should create a new resource using default property values in the JSON schema', - function(done) { - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.schema.required = []; - petParam.schema.properties.Name.default = 'Fido'; - petParam.schema.properties.Type.default = 'dog'; - petParam.schema.properties.Tags.default = 'fluffy,brown'; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Age: 4}) - .expect(200) - .end(helper.checkResults(done, function(res1) { - supertest - .get('/api/pets/Fido') - .expect(200, {Name: 'Fido', Type: 'dog', Age: 4, Tags: ['fluffy', 'brown']}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('should create a new resource using data tha was added by other middleware', - function(done) { - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.required = false; - - function messWithTheBody(req, res, next) { - if (req.method === method.toUpperCase()) { - req.body = {Name: 'Fido', Type: 'dog'}; - } - next(); - } - - helper.initTest(messWithTheBody, api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .set('Content-Type', 'application/json') - .expect(200) - .end(helper.checkResults(done, function(res1) { - supertest - .get('/api/pets/Fido') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - })); - }); - } - ); - - /** - * This test is different than the "merge vs. overwrite" tests that we do later for "PUT vs. PATCH". - * Here, all we're doing is verifying that it doesn't create two resources with the same URL. - */ - it('should replace an existing resource at the URL', - function(done) { - // Create a pet at the URL "/api/pets/Fido" - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - helper.initTest(api, function(supertest) { - // Create another pet at the URL "/api/pets/Fido" - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fluffy', Type: 'cat'}) - .expect(200) - .end(helper.checkResults(done, function(res1) { - - // Make sure there's only ONE "/api/pets/Fido" resource - supertest - .get('/api/pets') - .expect(200, [{Name: 'Fluffy', Type: 'cat'}]) - .end(helper.checkResults(done)); - })); - }); - }); - } - ); - - it('should not return data if not specified in the Swagger API', - function(done) { - delete api.paths['/pets/{PetName}'][method].responses[200].schema; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog'}) - .expect(200, '') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return the saved resource if the Swagger API schema is an object', - function(done) { - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog'}) - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return the whole collection if the Swagger API schema is an array', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema = {type: 'array', items: {}}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog'}) - .expect(200, [{Name: 'Fluffy', Type: 'cat'}, {Name: 'Fido', Type: 'dog'}]) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the saved resource if the Swagger API schema is a wrapped object', - function(done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets/{PetName}'][method].responses[200].schema = { - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {}, - result: _.cloneDeep(api.definitions.pet) - } - }; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog'}) - .expect(200, {code: 42, message: 'hello world', result: {Name: 'Fido', Type: 'dog'}}) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return the whole collection if the Swagger API schema is a wrapped array', - function(done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets/{PetName}'][method].responses[200].schema = { - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {}, - result: {type: 'array', items: _.cloneDeep(api.definitions.pet)} - } - }; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog'}) - .expect(200, { - code: 42, - message: 'hello world', - result: [ - {Name: 'Fluffy', Type: 'cat'}, - {Name: 'Fido', Type: 'dog'} - ] - }) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return `res.body` if already set by other middleware', - function(done) { - function messWithTheBody(req, res, next) { - res.body = ['Not', 'the', 'response', 'you', 'expected']; - next(); - } - - helper.initTest(messWithTheBody, api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog'}) - .expect(200, ['Not', 'the', 'response', 'you', 'expected']) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return a 500 error if a DataStore error occurs', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - dataStore.__saveDataStore = function(collection, data, callback) { - setImmediate(callback, new Error('Test Error')); - }; - - helper.initTest(dataStore, api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog'}) - .expect(500) - .end(function(err, res) { - if (err) { - return done(err); - } - expect(res.text).to.contain('Error: Test Error'); - done(); - }); - } - ); - } - ); - }); - - describe('Data type tests', function() { - it('should return an object', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'object'; - - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.schema = {}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog'}) - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, {Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return a string', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'string'; - - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.schema = {type: 'string'}; - api.paths['/pets/{PetName}'][method].consumes = ['text/plain']; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .set('Content-Type', 'text/plain') - .send('I am Fido') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, 'I am Fido') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return an empty string response', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'string'; - - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.schema = {type: 'string'}; - api.paths['/pets/{PetName}'][method].consumes = ['text/plain']; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .set('Content-Type', 'text/plain') - .send('') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, '') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return a number', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'number'; - - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.schema = {type: 'number'}; - api.paths['/pets/{PetName}'][method].consumes = ['text/plain']; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .set('Content-Type', 'text/plain') - .send('42.999') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, '42.999') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return a date', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'string'; - api.paths['/pets/{PetName}'][method].responses[200].schema.format = 'date-time'; - - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.schema = {type: 'string', format: 'date-time'}; - api.paths['/pets/{PetName}'][method].consumes = ['text/plain']; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .set('Content-Type', 'text/plain') - .send('2000-01-02T03:04:05.006Z') - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, '2000-01-02T03:04:05.006Z') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return a Buffer (as a string)', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'string'; - - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.schema = {}; - api.paths['/pets/{PetName}'][method].consumes = ['application/octet-stream']; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .set('Content-Type', 'application/octet-stream') - .send(new Buffer('hello world').toString()) - .expect('Content-Type', 'text/plain; charset=utf-8') - .expect(200, 'hello world') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return a Buffer (as JSON)', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'object'; - - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.schema = {}; - api.paths['/pets/{PetName}'][method].consumes = ['application/octet-stream']; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .set('Content-Type', 'application/octet-stream') - .send(new Buffer('hello world').toString()) - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(200, { - type: 'Buffer', - data: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] - }) - .end(helper.checkResults(done)); - }); - } - ); - - it('should return an undefined value', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'object'; - - var petParam = _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}); - petParam.schema = {}; - petParam.required = false; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .set('Content-Type', 'application/json') - .expect('Content-Type', 'application/json') - .expect(200, '') - .end(helper.checkResults(done)); - }); - } - ); - - it('should return multipart/form-data', - function(done) { - api.paths['/pets/{PetName}/photos/{ID}'][method].responses[201].schema = {}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos/12345') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect('Content-Type', 'application/json; charset=utf-8') - .expect(201) - .end(helper.checkResults(done, function(res) { - expect(res.body).to.deep.equal({ - Label: 'Photo 1', - Description: 'A photo of Fido', - Photo: { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res.body.Photo.name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res.body.Photo.path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - }); - done(); - })); - }); - } - ); - - it('should return a file', - function(done) { - api.paths['/pets/{PetName}/photos/{ID}'][method].responses[201].schema = {type: 'file'}; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos/12345') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect('Content-Type', 'image/jpeg') - .expect(201) - .end(helper.checkResults(done, function(res) { - // It should NOT be an attachment - expect(res.headers['content-disposition']).to.be.undefined; - - expect(res.body).to.be.an.instanceOf(Buffer); - expect(res.body.length).to.equal(683709); - done(); - })); - }); - } - ); - - it('should return a file attachment', - function(done) { - _.find(api.paths['/pets/{PetName}/photos/{ID}'].parameters, {name: 'ID'}).type = 'string'; - api.paths['/pets/{PetName}/photos/{ID}'][method].responses[201].schema = {type: 'file'}; - api.paths['/pets/{PetName}/photos/{ID}'][method].responses[201].headers = { - 'content-disposition': { - type: 'string' - } - }; - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido/photos/Photo%20Of%20Fido.jpg') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect('Content-Type', 'image/jpeg') - .expect(201) - - // `res.sendFile` automatically sets the Content-Disposition header, - // and includes a safe UTF-8 filename, since our filename includes spaces - .expect('Content-Disposition', 'attachment; filename="Photo%20Of%20Fido.jpg"; filename*=UTF-8\'\'Photo%2520Of%2520Fido.jpg') - - .end(helper.checkResults(done, function(res) { - expect(res.body).to.be.an.instanceOf(Buffer); - expect(res.body.length).to.equal(683709); - done(); - })); - } - ); - } - ); - }); - - describe('PUT tests', function() { - if (method !== 'put') { - return; - } - - it('should overwrite the existing resource rather than merging it', - function(done) { - _.find(api.paths['/pets/{PetName}'].put.parameters, {name: 'PetData'}).schema.properties.Vet.required = []; - helper.initTest(api, function(supertest) { - supertest - .put('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog', Tags: ['fluffy', 'brown'], Vet: {Name: 'Vet Name'}}) - .expect(200) - .end(helper.checkResults(done, function(res1) { - supertest - .put('/api/pets/Fido') - .send({ - Name: 'Fido', Type: 'cat', Tags: ['furry'], Vet: { - Address: {Street: '123 First St.', City: 'New York', State: 'NY', ZipCode: 12345} - } - }) - .expect(200) - .end(helper.checkResults(done, function(res2) { - // The original resource - expect(res1.body).to.deep.equal({ - Name: 'Fido', - Type: 'dog', - Tags: ['fluffy', 'brown'], - Vet: { - Name: 'Vet Name' - } - }); - - // The new resource - expect(res2.body).to.deep.equal({ - Name: 'Fido', - Type: 'cat', - Tags: ['furry'], - Vet: { - Address: { - Street: '123 First St.', - City: 'New York', - State: 'NY', - ZipCode: 12345 - } - } - }); - - done(); - })); - })); - }); - } - ); - - it('should return a 500 error if a DataStore error occurs', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - dataStore.__openDataStore = function(collection, callback) { - setImmediate(callback, new Error('Test Error')); - }; - - helper.initTest(dataStore, api, function(supertest) { - supertest - .put('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog'}) - .expect(500) - .end(function(err, res) { - if (err) { - return done(err); - } - expect(res.text).to.contain('Error: Test Error'); - done(); - }); - } - ); - } - ); - }); - - describe('PATCH/POST tests', function() { - if (method !== 'patch' && method !== 'post') { - return; - } - - it('should merge the new resource with the existing resource', - function(done) { - _.find(api.paths['/pets/{PetName}'][method].parameters, {name: 'PetData'}).schema.properties.Vet.required = []; - - helper.initTest(api, function(supertest) { - supertest - [method]('/api/pets/Fido') - .send({Name: 'Fido', Type: 'dog', Tags: ['fluffy', 'brown'], Vet: {Name: 'Vet Name'}}) - .expect(200) - .end(helper.checkResults(done, function(res1) { - supertest - [method]('/api/pets/Fido') - .send({ - Name: 'Fido', Type: 'cat', Tags: ['furry'], Vet: { - Address: {Street: '123 First St.', City: 'New York', State: 'NY', ZipCode: 12345} - } - }) - .expect(200) - .end(helper.checkResults(done, function(res2) { - // The original resource - expect(res1.body).to.deep.equal({ - Name: 'Fido', - Type: 'dog', - Tags: ['fluffy', 'brown'], - Vet: { - Name: 'Vet Name' - } - }); - - // The merged resource - expect(res2.body).to.deep.equal({ - Name: 'Fido', - Type: 'cat', - Tags: ['furry', 'brown'], - Vet: { - Name: 'Vet Name', - Address: { - Street: '123 First St.', - City: 'New York', - State: 'NY', - ZipCode: 12345 - } - } - }); - - done(); - })); - })); - }); - } - ); - }); - }); - }); -}); diff --git a/tests/specs/mock/helper.js b/tests/specs/mock/helper.js deleted file mode 100644 index d6a63d3..0000000 --- a/tests/specs/mock/helper.js +++ /dev/null @@ -1,68 +0,0 @@ -var swagger = require('../../../'), - helper = require('../../fixtures/helper'), - util = require('../../../lib/helpers/util'), - _ = require('lodash'); - -_.extend(exports, helper); - -/** - * Helper function for Mock tests. - * - * @param {e.app} [express] - The Express App to use for the test - * @param {DataStore} [dataStore] - The DataStore to use for the test - * @param {function[]} [fns] - Middleware functions to add to Express - * @param {object} api - The Swagger API for the test - * @param {function} test - The actual unit test - */ -exports.initTest = function(express, dataStore, fns, api, test) { - switch (arguments.length) { - case 2: - test = arguments[1]; - api = arguments[0]; - fns = undefined; - dataStore = undefined; - express = undefined; - break; - case 3: - test = arguments[2]; - api = arguments[1]; - if (arguments[0] instanceof swagger.DataStore) { - dataStore = arguments[0]; - fns = undefined; - express = undefined; - } - else if (util.isExpressRouter(arguments[0])) { - express = arguments[0]; - dataStore = undefined; - fns = undefined; - } - else { - fns = arguments[0]; - dataStore = undefined; - express = undefined; - } - break; - case 4: - test = arguments[3]; - api = arguments[2]; - fns = arguments[1]; - dataStore = arguments[0]; - express = undefined; - } - - express = express || helper.express(); - var supertest = helper.supertest(express.app || express); - - swagger(api, express, function(err, middleware) { - express.use( - middleware.metadata(), - middleware.CORS(), - middleware.parseRequest(), - middleware.validateRequest(), - fns || [], - middleware.mock(dataStore) - ); - - test(supertest); - }); -}; diff --git a/tests/specs/mock/mock.spec.js b/tests/specs/mock/mock.spec.js deleted file mode 100644 index 203c393..0000000 --- a/tests/specs/mock/mock.spec.js +++ /dev/null @@ -1,322 +0,0 @@ -var swagger = require('../../../'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'); - -describe('Mock middleware', function() { - 'use strict'; - - it('should do nothing if no other middleware is used', - function(done) { - swagger(files.parsed.petStore, function(err, middleware) { - var express = helper.express(middleware.mock()); - - helper.supertest(express) - .get('/api/pets') - .end(helper.checkSpyResults(done)); - - express.get('/api/pets', helper.spy(function(req, res, next) { - expect(req.swagger).to.be.undefined; - expect(res.swagger).to.be.undefined; - })); - }); - } - ); - - it('should do nothing if the Metadata middleware is not used', - function(done) { - swagger(files.parsed.petStore, function(err, middleware) { - var express = helper.express( - middleware.CORS(), middleware.parseRequest(), middleware.validateRequest(), middleware.mock() - ); - - helper.supertest(express) - .get('/api/pets') - .end(helper.checkSpyResults(done)); - - express.get('/api/pets', helper.spy(function(req, res, next) { - expect(req.swagger).to.be.undefined; - expect(res.swagger).to.be.undefined; - })); - }); - } - ); - - it('should do nothing if the API is not valid', - function(done) { - swagger(files.parsed.blank, function(err, middleware) { - var express = helper.express( - middleware.metadata(), middleware.CORS(), middleware.parseRequest(), middleware.mock() - ); - - helper.supertest(express) - .get('/api/pets') - .end(helper.checkSpyResults(done)); - - express.get('/api/pets', helper.spy(function(req, res, next) { - expect(req.swagger).to.deep.equal({ - api: { - "swagger": "2.0", - "info": { - "title": "Test Swagger", - "version": "1.0" - }, - "paths": {} - }, - pathName: '', - path: null, - operation: null, - params: [], - security: [] - }); - expect(res.swagger).to.be.undefined; - })); - }); - } - ); - - it('should do nothing if "mock" is disabled in Express', - function(done) { - var express = helper.express(); - swagger(files.parsed.petStore, express, function(err, middleware) { - express.use( - middleware.metadata(), middleware.CORS(), middleware.parseRequest(), - middleware.validateRequest(), middleware.mock() - ); - - // Disable the mock middleware - express.disable('mock'); - - helper.supertest(express) - .get('/api/pets') - .end(helper.checkSpyResults(done)); - - express.get('/api/pets', helper.spy(function(req, res, next) { - expect(req.swagger).to.deep.equal({ - api: files.parsed.petStore, - pathName: '/pets', - path: files.parsed.petsPath, - operation: files.parsed.petsGetOperation, - params: files.parsed.petsGetParams, - security: [] - }); - expect(res.swagger).to.be.undefined; - })); - }); - } - ); - - it('can be passed an Express Application', - function(done) { - swagger(files.parsed.petStore, function(err, middleware) { - var express = helper.express(); - var supertest = helper.supertest(express); - - // NOTE: Only passing the Express App to the mock middleware - // All other middleware will always default to case-insensitive - express.use( - middleware.metadata(), middleware.CORS(), middleware.parseRequest(), - middleware.validateRequest(), middleware.mock(express) - ); - - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, '') - .end(helper.checkResults(done, function() { - // Change the case-sensitivity setting. - express.enable('case sensitive routing'); - - // Now this request will return nothing, because the path is not a case-sensitive match - supertest - .get('/API/PETS') - .expect(200, []) - .end(helper.checkResults(done)); - })); - }); - } - ); - - it('can be passed a data store', - function(done) { - swagger(files.parsed.petStore, function(err, middleware) { - var express = helper.express(); - var supertest = helper.supertest(express); - var dataStore = new swagger.MemoryDataStore(); - - express.use( - middleware.metadata(), middleware.CORS(), middleware.parseRequest(), - middleware.validateRequest(), middleware.mock(dataStore) - ); - - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, '') - .end(helper.checkResults(done, function() { - // Remove the item from the data store - dataStore.delete(new swagger.Resource('/api/pets/fido'), function() { - // Now this request will return nothing, because the resource is no longer in the data store - supertest - .get('/API/PETS') - .expect(200, []) - .end(helper.checkResults(done)); - }); - })); - }); - } - ); - - it('can be passed an Express App and a data store', - function(done) { - swagger(files.parsed.petStore, function(err, middleware) { - var express = helper.express(); - var supertest = helper.supertest(express); - var dataStore = new swagger.MemoryDataStore(); - - // NOTE: Only passing the Express App to the mock middleware - // All other middleware will always default to case-insensitive - express.use( - middleware.metadata(), middleware.CORS(), middleware.parseRequest(), - middleware.validateRequest(), middleware.mock(express, dataStore) - ); - - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201, '') - .end(helper.checkResults(done, function() { - // Change the case-sensitivity setting. - express.enable('case sensitive routing'); - - // Now this request will return nothing, because the path is not a case-sensitive match - supertest - .get('/API/PETS') - .expect(200, []) - .end(helper.checkResults(done, function() { - // Remove the item from the data store - dataStore.delete(new swagger.Resource('/api/pets/Fido'), function() { - // Now this request will return nothing, because the resource is no longer in the data store - supertest - .get('/api/pets') - .expect(200, []) - .end(helper.checkResults(done)); - }); - })); - })); - }); - } - ); - - it('should get the data store from the Express App', - function(done) { - var express = helper.express(); - var supertest = helper.supertest(express); - swagger(files.parsed.petStore, express, function(err, middleware) { - var dataStore = new swagger.MemoryDataStore(); - - // Setting the "mock data store" on the Express App - express.set('mock data store', dataStore); - - express.use( - middleware.metadata(), middleware.CORS(), middleware.parseRequest(), - middleware.validateRequest(), middleware.mock() - ); - - // Add a resource to the data store - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - - // Make sure the Mock middleware is using the data store - supertest - .get('/api/pets') - .expect(200, [{Name: 'Fido', Type: 'dog'}]) - .end(helper.checkResults(done)); - }); - }); - } - ); - - it('should get the data store from the Express App instead of the param', - function(done) { - var express = helper.express(); - var supertest = helper.supertest(express); - swagger(files.parsed.petStore, express, function(err, middleware) { - var dataStore1 = new swagger.MemoryDataStore(); - var dataStore2 = new swagger.MemoryDataStore(); - - // Set the "mock data store" to data store #1 - express.set('mock data store', dataStore1); - - express.use( - middleware.metadata(), middleware.CORS(), - middleware.parseRequest(), middleware.validateRequest(), - - // Pass data store #2 - middleware.mock(dataStore2) - ); - - // Add different resources to each data store - var resource1 = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore1.save(resource1, function() { - var resource2 = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore2.save(resource2, function() { - - // Make sure the Mock middleware is using data store #1 - supertest - .get('/api/pets') - .expect(200, [{Name: 'Fido', Type: 'dog'}]) - .end(helper.checkResults(done)); - }); - }); - }); - } - ); - - it('should detect changes to the data store from the Express App', - function(done) { - var express = helper.express(); - var supertest = helper.supertest(express); - swagger(files.parsed.petStore, express, function(err, middleware) { - var dataStore1 = new swagger.MemoryDataStore(); - var dataStore2 = new swagger.MemoryDataStore(); - - express.use( - middleware.metadata(), middleware.CORS(), - middleware.parseRequest(), middleware.validateRequest(), - - // Start-out using data store #1 - middleware.mock(dataStore1) - ); - - // Add different resources to each data store - var resource1 = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore1.save(resource1, function() { - var resource2 = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore2.save(resource2, function() { - - // Switch to data store #2 - express.set('mock data store', dataStore2); - - // Make sure the Mock middleware is using data store #2 - supertest - .get('/api/pets') - .expect(200, [{Name: 'Fluffy', Type: 'cat'}]) - .end(helper.checkResults(done, function() { - - // Disable data store #2, so data store #1 will be used - express.disable('mock data store'); - - // Make sure the Mock middleware is using data store #1 - supertest - .get('/api/pets') - .expect(200, [{Name: 'Fido', Type: 'dog'}]) - .end(helper.checkResults(done)); - })); - }); - }); - }); - } - ); -}); diff --git a/tests/specs/mock/query-collection.spec.js b/tests/specs/mock/query-collection.spec.js deleted file mode 100644 index b34e1bf..0000000 --- a/tests/specs/mock/query-collection.spec.js +++ /dev/null @@ -1,845 +0,0 @@ -var swagger = require('../../../'), - util = require('../../../lib/helpers/util'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'); - -describe('Query Collection Mock', function () { - var availableContentTypes = _.intersection(files.parsed.petStore.consumes, files.parsed.petStore.produces); - - availableContentTypes.forEach(function (contentType) { - ['get', 'head', 'options'].forEach(function (method) { - describe(contentType, function () { - describe(method.toUpperCase(), function () { - testCases(contentType, method); - }); - }); - }); - }); -}); - -function testCases (contentType, method) { - 'use strict'; - - var contentTypePattern = new RegExp('^' + contentType + '; charset=utf-8'); - - var api, noBody, noHeaders; - beforeEach(function () { - api = _.cloneDeep(files.parsed.petStore); - noBody = method === 'head' || method === 'options'; - noHeaders = method === 'options'; - - // Change the HTTP method of GET /pets - var operation = api.paths['/pets'].get; - delete api.paths['/pets'].get; - api.paths['/pets'][method] = operation; - - // Change the HTTP method of GET /pets/{PetName}/photos - operation = api.paths['/pets/{PetName}/photos'].get; - delete api.paths['/pets/{PetName}/photos'].get; - api.paths['/pets/{PetName}/photos'][method] = operation; - }); - - it('should return an empty array if there is no data in the collection', - function (done) { - helper.initTest(api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 2); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : []); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should return a single-item array if there is one item in the collection', - function (done) { - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 30); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [{Name: 'Fido', Type: 'dog'}]); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a single-item array containing the root item in the collection', - function (done) { - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets', '/', 'This is the root resource'); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 29); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : ['This is the root resource']); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return an array of all items in the collection', - function (done) { - var dataStore = new swagger.MemoryDataStore(); - var res1 = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - var res2 = new swagger.Resource('/api/pets/String', 'I am Fido'); - var res3 = new swagger.Resource('/api/pets/Buffer', new Buffer('hello world')); - dataStore.save(res1, res2, res3, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 112); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [ - {Name: 'Fido', Type: 'dog'}, - 'I am Fido', - { - type: 'Buffer', - data: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] - } - ]); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a wrapped array of all items in the collection', - function (done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets'][method].responses[200].schema = { - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {}, - result: {type: 'array', items: _.cloneDeep(api.definitions.pet)} - } - }; - - var dataStore = new swagger.MemoryDataStore(); - var res1 = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - var res2 = new swagger.Resource('/api/pets/String', 'I am Fido'); - var res3 = new swagger.Resource('/api/pets/Buffer', new Buffer('hello world')); - dataStore.save(res1, res2, res3, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 157); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : { - code: 42, - message: 'hello world', - result: [ - {Name: 'Fido', Type: 'dog'}, - 'I am Fido', - { - type: 'Buffer', - data: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] - } - ] - }); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return an array of all items in the collection, including the root resource', - function (done) { - var dataStore = new swagger.MemoryDataStore(); - var res1 = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - var res2 = new swagger.Resource('/api/pets', '/', 'This is the root resource'); - var res3 = new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}); - dataStore.save(res1, res2, res3, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 89); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [ - {Name: 'Fido', Type: 'dog'}, - 'This is the root resource', - {Name: 'Polly', Type: 'bird'} - ]); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should not return anything if no response schema is specified in the Swagger API', - function (done) { - delete api.paths['/pets'][method].responses[200].schema; - helper.initTest(api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - request.expect(200, ''); - request.end(helper.checkResults(done, function (res) { - expect(res.headers['content-length']).to.satisfy(function (contentLength) { - // This is the difference between returning an empty array vs. nothing at all - return contentLength === undefined || contentLength === '0'; - }); - done(); - })); - }); - } - ); - - it('should return `res.body` if already set by other middleware', - function (done) { - function messWithTheBody (req, res, next) { - res.body = {message: 'Not the response you expected'}; - next(); - } - - helper.initTest(messWithTheBody, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 43); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : {message: 'Not the response you expected'}); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should return the default value instead of an empty array', - function (done) { - api.paths['/pets'][method].responses[200].schema.default = ['The default value']; - - helper.initTest(api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 21); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : ['The default value']); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should return the example value instead of an empty array', - function (done) { - api.paths['/pets'][method].responses[200].schema.example = ['The example value']; - - helper.initTest(api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 21); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : ['The example value']); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should set the Last-Modified date to Now() if the results are empty', - function (done) { - var before = new Date(); - api.paths['/pets'][method].responses[200].headers = { - 'Last-Modified': {type: 'string'} - }; - - helper.initTest(api, function (supertest) {// Wait 1 second, since the "Last-Modified" header is only precise to the second - setTimeout(function () { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 2); - noBody || request.expect('Content-Type', contentTypePattern); - request.end(helper.checkResults(done, function (res) { - if (!noHeaders) { - var lastModified = new Date(res.headers['last-modified']); - expect(lastModified).to.be.afterTime(before); - } - done(); - })); - }, 1000); - }); - } - ); - - it('should set the Last-Modified date to the ModifiedOn date of the only item in the collection', - function (done) { - api.paths['/pets'][method].responses[200].headers = { - 'Last-Modified': {type: 'string'} - }; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets', '/', 'This is the root resource'); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) {// Wait 1 second, since the "Last-Modified" header is only precise to the second - setTimeout(function () { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 29); - noHeaders || request.expect('Last-Modified', util.rfc1123(resource.modifiedOn)); - noBody || request.expect('Content-Type', contentTypePattern); - request.end(helper.checkResults(done)); - }, 1000); - }); - }); - } - ); - - it('should set the Last-Modified date to the max ModifiedOn date in the collection', - function (done) { - api.paths['/pets'][method].responses[200].headers = { - 'Last-Modified': {type: 'string'} - }; - - var dataStore = new swagger.MemoryDataStore(); - - // Save resource1 - var resource1 = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource1, function () { - setTimeout(function () { - // Save resource2 - var resource2 = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - dataStore.save(resource2, function () { - setTimeout(function () { - // Update resource1 - resource1.data.foo = 'bar'; - dataStore.save(resource1, function () { - helper.initTest(dataStore, api, function (supertest) { - setTimeout(function () { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 73); - noHeaders || request.expect('Last-Modified', util.rfc1123(resource1.modifiedOn)); - noBody || request.expect('Content-Type', contentTypePattern); - request.end(helper.checkResults(done)); - }, 1000); - }); - }); - }, 1000); - }); - }, 1000); - }); - } - ); - - if (method !== 'options') { - it('should return a 500 error if a DataStore error occurs', - function (done) { - var dataStore = new swagger.MemoryDataStore(); - dataStore.__openDataStore = function (collection, callback) { - setImmediate(callback, new Error('Test Error')); - }; - - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - request.expect(500); - request.end(function (err, res) { - if (err) { - return done(err); - } - - // The content-length will vary slightly, depending on the stack trace - expect(res.headers['content-length']).to.match(/^\d{4,5}$/); - - if (!noBody) { - expect(res.text).to.contain('Error: Test Error'); - } - done(); - }); - }); - } - ); - } - - describe('different data types', function () { - it('should return a string', - function (done) { - api.paths['/pets'][method].responses[200].schema.items = {type: 'string'}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 13); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : ['I am Fido']); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return an empty string', - function (done) { - api.paths['/pets'][method].responses[200].schema.items = {type: 'string'}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', ''); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 4); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : ['']); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a number', - function (done) { - api.paths['/pets'][method].responses[200].schema.items = {type: 'number'}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 42.999); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 8); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [42.999]); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a date', - function (done) { - api.paths['/pets'][method].responses[200].schema.items = {type: 'string', format: 'date'}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Date(Date.UTC(2000, 1, 2, 3, 4, 5, 6))); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 14); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : ['2000-02-02']); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a date-time', - function (done) { - api.paths['/pets'][method].responses[200].schema.items = {type: 'string', format: 'date-time'}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Date(Date.UTC(2000, 1, 2, 3, 4, 5, 6))); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 28); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : ['2000-02-02T03:04:05.006Z']); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a Buffer (as a string)', - function (done) { - api.paths['/pets'][method].responses[200].schema.items = {type: 'string'}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Buffer('hello world')); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 15); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : ['hello world']); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a Buffer (as JSON)', - function (done) { - api.paths['/pets'][method].responses[200].schema.items = {}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Buffer('hello world')); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 71); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [ - { - type: 'Buffer', - data: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] - } - ]); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a null value', - function (done) { - api.paths['/pets'][method].responses[200].schema.items = {}; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido'); - dataStore.save(resource, function () { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 6); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [null]); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return multipart/form-data', - function (done) { - helper.initTest(api, function (supertest) { - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .end(helper.checkResults(done, function (res) { - var photoID = parseInt(res.headers.location.match(/(\d+)$/)[0]); - - var request = supertest[method]('/api/pets/Fido/photos').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - request.end(helper.checkResults(done, function (res) { - noHeaders || expect(res.headers['content-length']).to.match(/^\d{3}$/); - - if (noBody) { - expect(res.body).to.be.empty; - expect(res.text).to.be.empty; - } - else { - request.expect('Content-Type', contentTypePattern); - expect(res.body).to.deep.equal([ - { - ID: photoID, - Label: 'Photo 1', - Description: 'A photo of Fido', - Photo: { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res.body[0].Photo.name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res.body[0].Photo.path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - } - ]); - } - done(); - })); - })); - }); - } - ); - - it('should return a file', - function (done) { - api.paths['/pets/{PetName}/photos'][method].responses[200].schema.items = {type: 'file'}; - helper.initTest(api, function (supertest) { - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function () { - var request = supertest[method]('/api/pets/Fido/photos').set('Accept', contentType); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200); - request.end(helper.checkResults(done, function (res) { - noHeaders || expect(res.headers['content-length']).to.match(/^\d{3}$/); - - // It should NOT be an attachment - expect(res.headers['content-disposition']).to.be.undefined; - - if (noBody) { - expect(res.body).to.be.empty; - expect(res.text).to.be.empty; - } - else { - // There's no such thing as an "array of files", - // so we send back an array of file info - expect(res.body).to.deep.equal([ - { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res.body[0].name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res.body[0].path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - ]); - } - done(); - })); - })); - }); - } - ); - - it('should return a file attachment', - function (done) { - api.paths['/pets/{PetName}/photos'][method].responses[200].schema.items = {type: 'file'}; - api.paths['/pets/{PetName}/photos'][method].responses[200].headers = { - 'Content-Disposition': { - type: 'string' - } - }; - helper.initTest(api, function (supertest) { - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .expect(201) - .end(helper.checkResults(done, function () { - var request = supertest[method]('/api/pets/Fido/photos'); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - request.expect(200); - - // Since there are multiple files, Content-Disposition is the "file name" of the URL - noHeaders || request.expect('Content-Disposition', 'attachment; filename="photos"'); - - request.end(helper.checkResults(done, function (res) { - noHeaders || expect(res.headers['content-length']).to.match(/^\d{3}$/); - - if (noBody) { - expect(res.body).to.be.empty; - expect(res.text).to.be.empty; - } - else { - // There's no such thing as an "array of files", - // so we send back an array of file info - expect(res.body).to.deep.equal([ - { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res.body[0].name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res.body[0].path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - ]); - } - done(); - })); - })); - }); - } - ); - }); - - describe('filter', function () { - var Fido = { - Name: 'Fido', Age: 4, Type: 'dog', Tags: ['big', 'brown'], - Vet: {Name: 'Vet 1', Address: {Street: '123 First St.', City: 'New York', State: 'NY', ZipCode: 55555}} - }; - var Fluffy = { - Name: 'Fluffy', Age: 7, Type: 'cat', Tags: ['small', 'furry', 'white'], - Vet: {Name: 'Vet 2', Address: {Street: '987 Second St.', City: 'Dallas', State: 'TX', ZipCode: 44444}} - }; - var Polly = { - Name: 'Polly', Age: 1, Type: 'bird', Tags: ['small', 'blue'], - Vet: {Name: 'Vet 1', Address: {Street: '123 First St.', City: 'New York', State: 'NY', ZipCode: 55555}} - }; - var Lassie = { - Name: 'Lassie', Age: 7, Type: 'dog', Tags: ['big', 'furry', 'brown'], - Vet: {Name: 'Vet 3', Address: {Street: '456 Pet Blvd.', City: 'Manhattan', State: 'NY', ZipCode: 56565}} - }; - var Spot = { - Name: 'Spot', Age: 4, Type: 'dog', Tags: ['big', 'spotted'], - Vet: {Name: 'Vet 2', Address: {Street: '987 Second St.', City: 'Dallas', State: 'TX', ZipCode: 44444}} - }; - var Garfield = { - Name: 'Garfield', Age: 7, Type: 'cat', Tags: ['orange', 'fat'], - Vet: {Name: 'Vet 4', Address: {Street: '789 Pet Lane', City: 'New York', State: 'NY', ZipCode: 66666}} - }; - var allPets = [Fido, Fluffy, Polly, Lassie, Spot, Garfield]; - - var dataStore; - beforeEach(function (done) { - dataStore = new swagger.MemoryDataStore(); - var resources = allPets.map(function (pet) { - return new swagger.Resource('/api/pets', pet.Name, pet); - }); - dataStore.save(resources, done); - }); - - it('should filter by a string property', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Type=cat').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 350); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Fluffy, Garfield]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should filter by a numeric property', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Age=4').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 336); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Fido, Spot]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should filter by an array property (single value)', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Tags=big').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 514); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Fido, Lassie, Spot]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should filter by an array property (multiple values, comma-separated)', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Tags=big,brown').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 346); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Fido, Lassie]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should filter by an array property (multiple values, pipe-separated)', - function (done) { - _.find(api.paths['/pets'][method].parameters, {name: 'Tags'}).collectionFormat = 'pipes'; - - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Tags=big|brown').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 346); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Fido, Lassie]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should filter by an array property (multiple values, space-separated)', - function (done) { - _.find(api.paths['/pets'][method].parameters, {name: 'Tags'}).collectionFormat = 'ssv'; - - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Tags=big%20brown').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 346); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Fido, Lassie]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should filter by an array property (multiple values, repeated)', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Tags=big&Tags=brown').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 346); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Fido, Lassie]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should filter by multiple properties', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Age=7&Type=cat&Tags=orange').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 172); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Garfield]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should filter by a deep property', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Vet.Address.State=NY').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 687); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Fido, Polly, Lassie, Garfield]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should filter by multiple deep properties', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Vet.Address.State=NY&Vet.Address.City=New%20York').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 509); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Fido, Polly, Garfield]); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should not filter by properties that aren\'t defined in the Swagger API', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Name=Lassie&Vet.Address.Street=123%20First%20St.').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 1033); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : allPets); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should only filter by properties that are defined in the Swagger API', - function (done) { - helper.initTest(dataStore, api, function (supertest) { - var request = supertest[method]('/api/pets?Age=4&Name=Lassie&Vet.Name=Vet%202&Vet.Address.Street=123%20First%20St.').set('Accept', contentType); - noHeaders || request.expect('Content-Length', 169); - noBody || request.expect('Content-Type', contentTypePattern); - request.expect(200, noBody ? '' : [Spot]); - request.end(helper.checkResults(done)); - } - ); - } - ); - }); -} diff --git a/tests/specs/mock/query-resource.spec.js b/tests/specs/mock/query-resource.spec.js deleted file mode 100644 index 76395e2..0000000 --- a/tests/specs/mock/query-resource.spec.js +++ /dev/null @@ -1,658 +0,0 @@ -var swagger = require('../../../'), - util = require('../../../lib/helpers/util'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'), - isWindows = /^win/.test(process.platform); - -describe('Query Resource Mock', function() { - ['head', 'options', 'get'].forEach(function(method) { - describe(method.toUpperCase(), function() { - 'use strict'; - - var api, noBody, noHeaders; - beforeEach(function() { - api = _.cloneDeep(files.parsed.petStore); - noBody = method === 'head' || method === 'options'; - noHeaders = method === 'options'; - - // Change the HTTP method of GET /pets/{PetName} - var operation = api.paths['/pets/{PetName}'].get; - delete api.paths['/pets/{PetName}'].get; - api.paths['/pets/{PetName}'][method] = operation; - - // Change the HTTP method of GET /pets/{PetName}/photos/{ID} - operation = api.paths['/pets/{PetName}/photos/{ID}'].get; - delete api.paths['/pets/{PetName}/photos/{ID}'].get; - api.paths['/pets/{PetName}/photos/{ID}'][method] = operation; - }); - - it('should return only the requested resource', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - var res1 = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - var res2 = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - var res3 = new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}); - - dataStore.save(res1, res2, res3, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fluffy'); - noHeaders || request.expect('Content-Length', 30); - request.expect(200, noBody ? '' : {Name: 'Fluffy', Type: 'cat'}); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a wrapped resource', - function(done) { - // Wrap the "pet" definition in an envelope object - api.paths['/pets/{PetName}'][method].responses[200].schema = { - type: 'object', - properties: { - code: {type: 'integer', default: 42}, - message: {type: 'string', default: 'hello world'}, - error: {type: 'object'}, - result: _.cloneDeep(api.definitions.pet) - } - }; - - var dataStore = new swagger.MemoryDataStore(); - var res1 = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - var res2 = new swagger.Resource('/api/pets/Fluffy', {Name: 'Fluffy', Type: 'cat'}); - var res3 = new swagger.Resource('/api/pets/Polly', {Name: 'Polly', Type: 'bird'}); - - dataStore.save(res1, res2, res3, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fluffy'); - noHeaders || request.expect('Content-Length', 75); - request.expect(200, noBody ? '' : {code: 42, message: 'hello world', result: {Name: 'Fluffy', Type: 'cat'}}); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should not return anything if no response schema is specified in the Swagger API', - function(done) { - delete api.paths['/pets/{PetName}'][method].responses[200].schema; - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - request.expect(200, ''); - request.end(helper.checkResults(done, function(res) { - expect(res.headers['content-length']).to.satisfy(function(contentLength) { - // This is the difference between returning an empty array vs. nothing at all - return contentLength === undefined || contentLength === '0'; - }); - done(); - })); - }); - }); - } - ); - - it('should return `res.body` if already set by other middleware', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - function messWithTheBody(req, res, next) { - res.body = ['Not', 'the', 'response', 'you', 'expected']; - next(); - } - - helper.initTest(dataStore, messWithTheBody, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Length', 41); - request.expect(200, noBody ? '' : ['Not', 'the', 'response', 'you', 'expected']); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return `res.body` instead of a 404', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.default = {default: 'The default value'}; - api.paths['/pets/{PetName}'][method].responses[200].schema.example = {example: 'The example value'}; - - function messWithTheBody(req, res, next) { - res.body = ['Not', 'the', 'response', 'you', 'expected']; - next(); - } - - helper.initTest(messWithTheBody, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Length', 41); - request.expect(200, noBody ? '' : ['Not', 'the', 'response', 'you', 'expected']); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should return the default value instead of a 404', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.default = {default: 'The default value'}; - api.paths['/pets/{PetName}'][method].responses[200].schema.example = {example: 'The example value'}; - - helper.initTest(api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Length', 31); - request.expect(200, noBody ? '' : {default: 'The default value'}); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should return the example value instead of a 404', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.example = {example: 'The example value'}; - - helper.initTest(api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Length', 31); - request.expect(200, noBody ? '' : {example: 'The example value'}); - request.end(helper.checkResults(done)); - }); - } - ); - - it('should set the Last-Modified date to the ModifiedOn date of the resource', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].headers = { - 'Last-Modified': {type: 'string'} - }; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am fido'); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - // Wait 1 second, since the "Last-Modified" header is only precise to the second - setTimeout(function() { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Length', 11); - noHeaders || request.expect('Last-Modified', util.rfc1123(resource.modifiedOn)); - request.end(helper.checkResults(done)); - }, 1000); - }); - }); - } - ); - - if (method !== 'options') { - it('should throw a 404 if the resource does not exist', - function(done) { - helper.initTest(api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - request.expect(404); - request.end(function(err, res) { - if (err) { - return done(err); - } - - // The content-length will vary slightly, depending on the stack trace - expect(res.headers['content-length']).to.match(/^\d{3,4}$/); - done(); - }); - }); - } - ); - - it('should return a 500 error if a DataStore error occurs', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - dataStore.__openDataStore = function(collection, callback) { - setImmediate(callback, new Error('Test Error')); - }; - - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - request.expect(500); - request.end(function(err, res) { - if (err) { - return done(err); - } - - // The content-length will vary slightly, depending on the stack trace - expect(res.headers['content-length']).to.match(/^\d{4,5}$/); - - if (!noBody) { - expect(res.text).to.contain('Error: Test Error'); - } - done(); - }); - } - ); - } - ); - } - - describe('different data types', function() { - it('should return an object', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'object'; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 28); - request.expect(200, noBody ? '' : {Name: 'Fido', Type: 'dog'}); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a string', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'string'; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 'I am Fido'); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'text/plain; charset=utf-8'); - noHeaders || request.expect('Content-Length', 9); - request.expect(200, noBody ? '' : 'I am Fido'); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return an empty string response', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'string'; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', ''); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'text/plain; charset=utf-8'); - noHeaders || request.expect('Content-Length', '0'); - request.expect(200, ''); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a number', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'number'; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', 42.999); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'text/plain; charset=utf-8'); - noHeaders || request.expect('Content-Length', 6); - request.expect(200, noBody ? '' : '42.999'); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a date', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'string'; - api.paths['/pets/{PetName}'][method].responses[200].schema.format = 'date-time'; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Date(Date.UTC(2000, 1, 2, 3, 4, 5, 6))); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'text/plain; charset=utf-8'); - noHeaders || request.expect('Content-Length', 24); - request.expect(200, noBody ? '' : '2000-02-02T03:04:05.006Z'); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a Buffer (as a string)', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'string'; - api.paths['/pets/{PetName}'][method].produces = ['text/plain']; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Buffer('hello world')); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'text/plain; charset=utf-8'); - noHeaders || request.expect('Content-Length', 11); - request.expect(200, noBody ? '' : 'hello world'); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a Buffer (as JSON)', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'object'; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', new Buffer('hello world')); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 69); - request.expect(200, noBody ? '' : { - type: 'Buffer', - data: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] - }); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return an undefined value', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'object'; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido'); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'application/json'); - request.expect(200, ''); - request.end(helper.checkResults(done, function(res) { - expect(res.headers['content-length']).to.satisfy(function(contentLength) { - // This is the difference between returning an empty array vs. nothing at all - return contentLength === undefined || contentLength === '0'; - }); - done(); - })); - }); - }); - } - ); - - it('should return the default value instead of undefined', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.default = {default: 'The default value'}; - api.paths['/pets/{PetName}'][method].responses[200].schema.example = {example: 'The example value'}; - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'object'; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido'); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 31); - request.expect(200, noBody ? '' : {default: 'The default value'}); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return the example value instead of undefined', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.example = {example: 'The example value'}; - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'object'; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido'); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 31); - request.expect(200, noBody ? '' : {example: 'The example value'}); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return a null value', - function(done) { - api.paths['/pets/{PetName}'][method].responses[200].schema.type = 'object'; - - var dataStore = new swagger.MemoryDataStore(); - var resource = new swagger.Resource('/api/pets/Fido', null); - dataStore.save(resource, function() { - helper.initTest(dataStore, api, function(supertest) { - var request = supertest[method]('/api/pets/Fido'); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - noHeaders || request.expect('Content-Length', 4); - request.expect(200, noBody ? '' : 'null'); - request.end(helper.checkResults(done)); - }); - }); - } - ); - - it('should return multipart/form-data', - function(done) { - // Set the response schemas to return the full multipart/form-data object - api.paths['/pets/{PetName}/photos'].post.responses[201].schema = {type: 'object'}; - api.paths['/pets/{PetName}/photos/{ID}'][method].responses[200].schema.type = 'object'; - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .end(helper.checkResults(done, function(res1) { - var photoID = parseInt(res1.headers.location.match(/(\d+)$/)[0]); - - var request = supertest[method]('/api/pets/Fido/photos/' + photoID); - noHeaders || request.expect('Content-Type', 'application/json; charset=utf-8'); - request.end(helper.checkResults(done, function(res2) { - if (noBody) { - expect(res2.body).to.be.empty; - expect(res2.text).to.be.empty; - } - else { - expect(res2.body).to.deep.equal({ - ID: photoID, - Label: 'Photo 1', - Description: 'A photo of Fido', - Photo: { - fieldname: 'Photo', - originalname: '1MB.jpg', - name: res1.body.Photo.name, - encoding: '7bit', - mimetype: 'image/jpeg', - path: res1.body.Photo.path, - extension: 'jpg', - size: 683709, - truncated: false, - buffer: null - } - }); - } - done(); - })); - })); - }); - } - ); - - it('should return a file', - function(done) { - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.PDF) - .end(helper.checkResults(done, function(res1) { - var request = supertest[method](res1.headers.location); - noHeaders || request.expect('Content-Length', 263287); - noHeaders || request.expect('Content-Type', 'application/pdf'); - request.end(helper.checkResults(done, function(res2) { - // It should NOT be an attachment - expect(res2.headers['content-disposition']).to.be.undefined; - - if (noBody) { - expect(res2.body).to.be.empty; - expect(res2.text).to.be.empty; - } - else { - expect(res2.body).to.be.empty; - expect(res2.text).to.have.lengthOf(258441); - } - done(); - })); - })); - }); - } - ); - - it('should return a file attachment (using the basename of the URL)', - function(done) { - api.paths['/pets/{PetName}/photos/{ID}'][method].responses[200].headers = { - 'content-disposition': { - type: 'string' - } - }; - - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.text) - .end(helper.checkResults(done, function(res1) { - var photoID = parseInt(res1.headers.location.match(/(\d+)$/)[0]); - - var request = supertest[method](res1.headers.location); - noHeaders || request.expect('Content-Length', isWindows ? 95 : 87); // CRLF vs LF - noHeaders || request.expect('Content-Type', 'text/plain; charset=UTF-8'); - - // The filename is set to the basename of the URL by default - noHeaders || request.expect('Content-Disposition', 'attachment; filename="' + photoID + '"'); - - request.end(helper.checkResults(done, function(res2) { - if (noBody) { - expect(res2.body).to.be.empty; - expect(res2.text).to.be.empty; - } - else { - expect(res2.body).to.be.empty; - expect(res2.text).to.have.lengthOf(isWindows ? 95 : 87); // CRLF vs LF - } - done(); - })); - })); - }); - } - ); - - it('should return a file attachment (using the default filename in the Swagger API)', - function(done) { - api.paths['/pets/{PetName}/photos/{ID}'][method].responses[200].headers = { - 'content-disposition': { - type: 'string', - default: 'attachment; filename="MyCustomFileName.xyz"' - } - }; - - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.PDF) - .end(helper.checkResults(done, function(res1) { - var request = supertest[method](res1.headers.location); - noHeaders || request.expect('Content-Length', 263287); - noHeaders || request.expect('Content-Type', 'application/pdf'); - - // The filename comes from the Swagger API - noHeaders || request.expect('Content-Disposition', 'attachment; filename="MyCustomFileName.xyz"'); - - request.end(helper.checkResults(done, function(res2) { - if (noBody) { - expect(res2.body).to.be.empty; - expect(res2.text).to.be.empty; - } - else { - expect(res2.body).to.be.empty; - expect(res2.text).to.have.lengthOf(258441); - } - done(); - })); - })); - }); - } - ); - - it('should return a file attachment (using the basename of the URL when there\'s no default filename in the Swagger API)', - function(done) { - api.paths['/pets/{PetName}/photos/{ID}'][method].responses[200].headers = { - 'content-disposition': { - type: 'string', - default: 'attachment' // <--- No filename was specified - } - }; - - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets/Fido/photos') - .field('Label', 'Photo 1') - .field('Description', 'A photo of Fido') - .attach('Photo', files.paths.oneMB) - .end(helper.checkResults(done, function(res1) { - var photoID = parseInt(res1.headers.location.match(/(\d+)$/)[0]); - - var request = supertest[method](res1.headers.location); - noHeaders || request.expect('Content-Length', 683709); - noHeaders || request.expect('Content-Type', 'image/jpeg'); - - // The filename is the basename of the URL, since it wasn't specified in the Swagger API - noHeaders || request.expect('Content-Disposition', 'attachment; filename="' + photoID + '"'); - - request.end(helper.checkResults(done, function(res2) { - if (noBody) { - expect(res2.text).to.be.empty; - - if (method === 'options') { - expect(res2.body).to.be.empty; - } - else { - expect(res2.body).to.be.an.instanceOf(Buffer).with.lengthOf(0); - } - } - else { - expect(res2.body).to.be.an.instanceOf(Buffer); - expect(res2.body.length).to.equal(683709); - } - done(); - })); - })); - } - ); - }); - }); - }); - }); -}); diff --git a/tests/specs/mock/response-headers.spec.js b/tests/specs/mock/response-headers.spec.js deleted file mode 100644 index 5bf089f..0000000 --- a/tests/specs/mock/response-headers.spec.js +++ /dev/null @@ -1,500 +0,0 @@ -var swagger = require('../../../'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'); - -describe('Mock response headers', function() { - 'use strict'; - - var api; - beforeEach(function() { - api = _.cloneDeep(files.parsed.petStore); - }); - - it('should set headers to the default values specified in the Swagger API', - function(done) { - api.paths['/pets'].get.responses[200].headers = { - location: { - type: 'string', - default: 'hello world' - }, - 'Last-Modified': { - type: 'string', - default: 'hi there' - }, - 'Set-cookie': { - type: 'string', - default: 'foo=bar;biz=baz' - }, - 'Content-disposition': { - type: 'string', - default: 'attachment' - }, - 'pragma': { - type: 'string', - default: 'no-cache' - }, - 'myMadeUpHeader': { - type: 'integer', - default: 42 - } - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .expect('Location', 'hello world') - .expect('Last-Modified', 'hi there') - .expect('Set-Cookie', 'foo=bar;biz=baz') - .expect('Content-Disposition', 'attachment') - .expect('Pragma', 'no-cache') - .expect('MyMadeUpHeader', '42') - .end(helper.checkResults(done)); - }); - } - ); - - it('should not override headers that were already set by other middleware', - function(done) { - api.paths['/pets'].get.responses[200].headers = { - location: { - type: 'string', - default: 'hello world' - }, - 'Last-Modified': { - type: 'string', - default: 'hi there' - }, - 'Set-cookie': { - type: 'string', - default: 'foo=bar;biz=baz' - }, - 'Content-disposition': { - type: 'string', - default: 'attachment' - }, - 'pragma': { - type: 'string', - default: 'no-cache' - }, - 'myMadeUpHeader': { - type: 'integer', - default: 42 - } - }; - - var express = helper.express(); - express.use(function(req, res, next) { - res.set('location', 'foo'); - res.set('last-modified', 'bar'); - res.set('set-cookie', 'hello=world'); - res.set('content-disposition', 'not-an-attachment'); - res.set('pragma', 'cache-money'); - res.set('mymadeupheader', 'forty-two'); - next(); - }); - - helper.initTest(express, api, function(supertest) { - supertest - .get('/api/pets') - .expect('Location', 'foo') - .expect('Last-Modified', 'bar') - .expect('Set-Cookie', 'hello=world') - .expect('Content-Disposition', 'not-an-attachment') - .expect('Pragma', 'cache-money') - .expect('MyMadeUpHeader', 'forty-two') - .end(helper.checkResults(done)); - }); - } - ); - - it('should generate sample values for headers that have no values', - function(done) { - api.paths['/pets'].get.responses[200].headers = { - 'pragma': { - type: 'string' - }, - 'myMadeUpHeader': { - type: 'number' - }, - 'content-length': { - type: 'integer' - }, - 'expires': { - type: 'string', - format: 'date-time' - }, - 'date': { - type: 'string', - format: 'date' - } - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .end(helper.checkResults(done, function(res) { - var floatRegExp = /^-?\d+\.\d+(e[+-]\d+)$/; - var integerRegExp = /^-?\d+$/; - var dateTimeRegExp = /^\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} GMT/; - var dateRegExp = /^\w{3}, \d{2} \w{3} \d{4} 00:00:00 GMT/; - - expect(res.headers.pragma).to.be.a('string').and.not.empty; - expect(res.headers.mymadeupheader).to.match(floatRegExp); - expect(res.headers['content-length']).to.match(integerRegExp); - expect(res.headers.expires).to.match(dateTimeRegExp); - expect(res.headers.date).to.match(dateRegExp); - done(); - })); - }); - } - ); - - describe('Location header', function() { - it('should set the Location header to the newly-created resource', - function(done) { - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect('Location', '/api/pets/Fido') - .end(helper.checkResults(done)); - }); - } - ); - - it('should set the Location header to the existing resource', - function(done) { - api.paths['/pets'].get.responses[200].headers = { - location: { - type: 'string' - } - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .expect('Location', '/api/pets') - .end(helper.checkResults(done)); - }); - } - ); - - it('should set the Location header to the newly-created resource for a nested router', - function(done) { - var app = helper.express(); - var router1 = helper.router(); - var router2 = helper.router(); - app.use('/nested/path/', router1); - router1.use('/deeply/nested/path', router2); - router2.app = app; - - helper.initTest(router2, api, function(supertest) { - supertest - .post('/nested/path/deeply/nested/path/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect('Location', '/nested/path/deeply/nested/path/api/pets/Fido') - .end(helper.checkResults(done)); - }); - } - ); - - it('should set the Location header to the existing resource for a nested router', - function(done) { - api.paths['/pets'].get.responses[200].headers = { - location: { - type: 'string' - } - }; - - var app = helper.express(); - var router1 = helper.router(); - var router2 = helper.router(); - app.use('/nested/path/', router1); - router1.use('/deeply/nested/path', router2); - router2.app = app; - - helper.initTest(router2, api, function(supertest) { - supertest - .get('/nested/path/deeply/nested/path/api/pets') - .expect('Location', '/nested/path/deeply/nested/path/api/pets') - .end(helper.checkResults(done)); - }); - } - ); - - it('should not set the Location header if not specified in the Swagger API', - function(done) { - delete api.paths['/pets'].post.responses[201].headers; - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .end(helper.checkResults(done, function(res) { - expect(res.headers.location).to.be.undefined; - done(); - })); - }); - } - ); - }); - - describe('Last-Modified header', function() { - it('should set the Last-Modified header to the current date/time if no data exists', - function(done) { - var before = new Date(Date.now() - 1000); // one second ago - - api.paths['/pets'].get.responses[200].headers = { - 'Last-modified': { - type: 'string' - } - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .end(helper.checkResults(done, function(res) { - var lastModified = new Date(res.headers['last-modified']); - expect(lastModified).to.be.afterTime(before); - expect(lastModified).to.be.beforeTime(new Date()); - done(); - })); - }); - } - ); - - it('should set the Last-Modified header to the date/time that the data was created', - function(done) { - api.paths['/pets'].post.responses[201].headers = { - 'Last-modified': { - type: 'string' - } - }; - api.paths['/pets'].get.responses[200].headers = { - 'Last-modified': { - type: 'string' - } - }; - - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201) - .end(helper.checkResults(done, function(res) { - var dateCreated = new Date(res.headers['last-modified']); - - // Wait 1 second before re-querying the data, - // to make sure the Last-Modified header isn't just the current date/time - setTimeout(function() { - supertest - .get('/api/pets') - .end(helper.checkResults(done, function(res) { - var lastModified = new Date(res.headers['last-modified']); - expect(lastModified).to.equalTime(dateCreated); - done(); - })); - }, 1000); - })); - }); - } - ); - - it('should set the Last-Modified header to the date/time that the data was last modified', - function(done) { - var dataStore = new swagger.MemoryDataStore(); - api.paths['/pets'].get.responses[200].headers = { - 'Last-modified': { - type: 'string' - } - }; - - helper.initTest(dataStore, api, function(supertest) { - var resource = new swagger.Resource('/api/pets/Fido', {Name: 'Fido', Type: 'dog'}); - dataStore.save(resource, function(err) { - if (err) { - return done(err); - } - - // Remove the milliseconds, since the Last-Modified header is only precise to the second - resource.modifiedOn.setUTCMilliseconds(0); - - // Wait 1 second before re-querying the data, - // to make sure the Last-Modified header isn't just the current date/time - setTimeout(function() { - supertest - .get('/api/pets') - .end(helper.checkResults(done, function(res) { - var lastModified = new Date(res.headers['last-modified']); - expect(lastModified).to.equalTime(resource.modifiedOn); - done(); - })); - }, 1000); - }); - }); - } - ); - - it('should not set the Last-Modified header if not specified in the Swagger API', - function(done) { - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .end(helper.checkResults(done, function(res) { - expect(res.headers['last-modified']).to.be.undefined; - done(); - })); - }); - } - ); - }); - - describe('Content-Disposition header', function() { - it('should set the Content-Disposition header to basename of the Location URL', - function(done) { - api.paths['/pets'].post.responses[201].headers = { - 'content-disposition': { - type: 'string' - } - }; - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect('Content-Disposition', 'attachment; filename="Fido"') - .end(helper.checkResults(done)); - }); - } - ); - - it('should set the Content-Disposition header to basename of the request URL', - function(done) { - api.paths['/pets'].get.responses[200].headers = { - 'content-disposition': { - type: 'string' - } - }; - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .expect('Content-Disposition', 'attachment; filename="pets"') - .end(helper.checkResults(done)); - }); - } - ); - - it('should set the Content-Disposition header to the basename of the request URL for a nested router', - function(done) { - var app = helper.express(); - var router1 = helper.router(); - var router2 = helper.router(); - app.use('/nested/path/', router1); - router1.use('/deeply/nested/path', router2); - router2.app = app; - - api.paths['/pets'].get.responses[200].headers = { - 'content-disposition': { - type: 'string' - } - }; - - helper.initTest(router2, api, function(supertest) { - supertest - .get('/nested/path/deeply/nested/path/api/pets') - .expect('Content-Disposition', 'attachment; filename="pets"') - .end(helper.checkResults(done)); - }); - } - ); - - it('should not set the Content-Disposition header if not specified in the Swagger API', - function(done) { - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .end(helper.checkResults(done, function(res) { - expect(res.headers['content-disposition']).to.be.undefined; - done(); - })); - }); - } - ); - }); - - describe('Set-Cookie header', function() { - it('should set the Set-Cookie header to a random value', - function(done) { - api.paths['/pets'].get.responses[200].headers = { - 'Set-Cookie': { - type: 'string' - } - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .end(helper.checkResults(done, function(res) { - expect(res.headers['set-cookie']).to.have.lengthOf(1); - expect(res.headers['set-cookie'][0]).to.match(/^swagger=random\d+/); - done(); - })); - }); - } - ); - - it('should set the Set-Cookie header to the same value, if it already exists', - function(done) { - api.paths['/pets'].get.responses[200].headers = { - 'Set-Cookie': { - type: 'string' - } - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .set('Cookie', 'swagger=foo') - .expect('Set-Cookie', 'swagger=foo; Path=/') - .end(helper.checkResults(done)); - }); - } - ); - - it('should not set the Set-Cookie header if already set by other middleware', - function(done) { - api.paths['/pets'].get.responses[200].headers = { - 'Set-Cookie': { - type: 'string' - } - }; - - var express = helper.express(); - express.use(function(req, res, next) { - res.cookie('myCookie', 'some value'); - next(); - }); - - helper.initTest(express, api, function(supertest) { - supertest - .get('/api/pets') - .expect('Set-Cookie', 'myCookie=some%20value; Path=/') - .end(helper.checkResults(done)); - }); - } - ); - - it('should not set the Set-Cookie header if not specified in the Swagger API', - function(done) { - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .end(function(err, res) { - expect(res.headers['set-cookie']).to.be.undefined; - done(err); - }); - }); - } - ); - }); -}); diff --git a/tests/specs/mock/response.spec.js b/tests/specs/mock/response.spec.js deleted file mode 100644 index 4711ad9..0000000 --- a/tests/specs/mock/response.spec.js +++ /dev/null @@ -1,237 +0,0 @@ -var swagger = require('../../../'), - expect = require('chai').expect, - _ = require('lodash'), - files = require('../../fixtures/files'), - helper = require('./helper'); - -describe('Mock Response', function() { - 'use strict'; - - var api; - beforeEach(function() { - api = _.cloneDeep(files.parsed.petStore); - }); - - it('should use the 200 response, if it exists', - function(done) { - api.paths['/pets'].get.responses = { - '100': {description: ''}, - '204': {description: ''}, - 'default': {description: ''}, - '300': {description: ''}, - '200': {description: ''}, - '400': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .expect(200) - .end(helper.checkResults(done)); - }); - } - ); - - it('should use the lowest 2XX response that exists', - function(done) { - api.paths['/pets'].get.responses = { - '100': {description: ''}, - '204': {description: ''}, - 'default': {description: ''}, - '203': {description: ''}, - '201': {description: ''}, - '102': {description: ''}, - '404': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .expect(201) - .end(helper.checkResults(done)); - }); - } - ); - - it('should use the lowest 3XX response that exists', - function(done) { - api.paths['/pets'].get.responses = { - '100': {description: ''}, - '304': {description: ''}, - 'default': {description: ''}, - '302': {description: ''}, - '303': {description: ''}, - '400': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .expect(302) - .end(helper.checkResults(done)); - }); - } - ); - - it('should use the lowest 1XX response that exists', - function(done) { - api.paths['/pets'].get.responses = { - '102': {description: ''}, - '404': {description: ''}, - '500': {description: ''}, - '101': {description: ''}, - '400': {description: ''}, - '504': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .expect(101) - .end(helper.checkResults(done)); - }); - } - ); - - it('should use a 200 response if "default" exists', - function(done) { - api.paths['/pets'].get.responses = { - '100': {description: ''}, - '400': {description: ''}, - 'default': {description: ''}, - '402': {description: ''}, - '500': {description: ''}, - '102': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .get('/api/pets') - .expect(200) - .end(helper.checkResults(done)); - }); - } - ); - - it('should use a 201 response for POST operations if "default" exists', - function(done) { - api.paths['/pets'].post.responses = { - '100': {description: ''}, - '400': {description: ''}, - 'default': {description: ''}, - '402': {description: ''}, - '500': {description: ''}, - '102': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201) - .end(helper.checkResults(done)); - }); - } - ); - - it('should not use a 201 response for POST operations if "default" does not exist', - function(done) { - api.paths['/pets'].post.responses = { - '400': {description: ''}, - '402': {description: ''}, - '500': {description: ''}, - '102': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .post('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(102) - .end(helper.checkResults(done)); - }); - } - ); - - it('should use a 201 response for PUT operations if "default" exists', - function(done) { - api.paths['/pets'].put = api.paths['/pets'].post; - api.paths['/pets'].put.responses = { - '100': {description: ''}, - '400': {description: ''}, - 'default': {description: ''}, - '402': {description: ''}, - '500': {description: ''}, - '102': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .put('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(201) - .end(helper.checkResults(done)); - }); - } - ); - - it('should not use a 201 response for PUT operations if "default" does not exist', - function(done) { - api.paths['/pets'].put = api.paths['/pets'].post; - api.paths['/pets'].put.responses = { - '101': {description: ''}, - '400': {description: ''}, - '402': {description: ''}, - '500': {description: ''}, - '102': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .put('/api/pets') - .send({Name: 'Fido', Type: 'dog'}) - .expect(101) - .end(helper.checkResults(done)); - }); - } - ); - - it('should use a 204 response for DELETE operations if "default" exists', - function(done) { - api.paths['/pets/{PetName}'].delete.responses = { - '100': {description: ''}, - '400': {description: ''}, - 'default': {description: ''}, - '402': {description: ''}, - '500': {description: ''}, - '102': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .delete('/api/pets/Fido') - .expect(204) - .end(helper.checkResults(done)); - }); - } - ); - - it('should not use a 204 response for DELETE operations if "default" does not exist', - function(done) { - api.paths['/pets/{PetName}'].delete.responses = { - '101': {description: ''}, - '400': {description: ''}, - '402': {description: ''}, - '500': {description: ''}, - '102': {description: ''} - }; - - helper.initTest(api, function(supertest) { - supertest - .delete('/api/pets/Fido') - .expect(101) - .end(helper.checkResults(done)); - }); - } - ); -});