Skip to content

Commit

Permalink
swagger_raw - add e2e test with huge doc
Browse files Browse the repository at this point in the history
  • Loading branch information
osher authored Dec 4, 2016
1 parent 6f61ed8 commit 8a67d79
Showing 1 changed file with 113 additions and 15 deletions.
128 changes: 113 additions & 15 deletions test/fittings/swagger_raw.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
var should = require('should');
var SwaggerRunner = require('../../');
var swagger_raw = require('../../fittings/swagger_raw');
var fs = require('fs');
var YAML = require('js-yaml');
var path = require('path');
var _ = require('lodash');
var request = require('supertest');


describe('swagger_raw', function() {

Expand All @@ -16,19 +19,7 @@ describe('swagger_raw', function() {
var bagpipes = { config: { swaggerNodeRunner: { swagger: swagger }}};
swaggerDoc = swagger_raw({}, bagpipes);

var filteredSwagger = _.cloneDeep(swagger);
delete(filteredSwagger.paths['/invalid_header']);
delete(filteredSwagger.paths['/hello'].get.parameters[0]['x-remove-me'])

// hokey algorithm, but at least it's different than the one it's testing
var OMIT = ['x-swagger-router-controller', 'x-swagger-pipe', 'x-hidden', 'x-private', 'x-controller-interface'];
_.forEach(filteredSwagger.paths, function(element, name) {
filteredSwagger.paths[name] = _.omit(element, OMIT);
_.forEach(filteredSwagger.paths[name], function(element, subName) {
filteredSwagger.paths[name][subName] = _.omit(element, OMIT);
});
});

var filteredSwagger = filterDoc(swagger);
yaml = YAML.safeDump(filteredSwagger, { indent: 2 });
json = JSON.stringify(filteredSwagger, null, 2);
});
Expand Down Expand Up @@ -76,7 +67,7 @@ describe('swagger_raw', function() {
}
};

config = {
var config = {
filter: '.*'
};

Expand Down Expand Up @@ -106,7 +97,7 @@ describe('swagger_raw', function() {
}
};

config = {
var config = {
filter: '.*',
privateTags: [ 'x-private', 'x-hidden' ]
};
Expand All @@ -127,4 +118,111 @@ describe('swagger_raw', function() {
done();
});
});

describe("end-to-end", function() {
var app, filteredSwagger;
before(function(done) {
var data = fs.readFileSync(path.resolve(__dirname, '../assets/project/api/swagger/swagger.yaml'), 'utf8');
var attrs;

//brings the size of the doc to
// yaml - 125425
// json - 120868
var swagger = inflateDocWithAVeryBigTypeDefinition(YAML.safeLoad(data));

filteredSwagger = filterDoc(swagger);
yaml = YAML.safeDump(filteredSwagger, { indent: 2 });

app = createServer(swagger, done)
});

describe('when requested with accept:application/json', function() {
it('should yield the document as json', function(done) {
request(app)
.put('/swagger')
.set('accept', 'application/json')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
should.not.exist(err);

should(res.body).eql(filteredSwagger);

done();
})
})
});

describe('when requested with accept:application/yaml', function() {
it('should yield the document as json', function(done) {
request(app)
.put('/swagger')
.set('accept', 'application/yaml')
.expect(200)
.expect('Content-Type', /yaml/)
.end(function(err, res) {
should.not.exist(err);

should(res.text).eql(yaml);

done();
})
})
})
})
});


function createServer(swagger, done) {
var TEST_PROJECT_ROOT = path.resolve(__dirname, '..', 'assets', 'project');
var config = {
appRoot: TEST_PROJECT_ROOT,
swagger: swagger
};

var app = require('connect')();
SwaggerRunner.create(config, function(err, r) {
if (err) {
console.error(err);
return done(err);
}
r.connectMiddleware().register(app);
done();
});
return app
}

function filterDoc(swagger) {
var filteredSwagger = _.cloneDeep(swagger);
delete(filteredSwagger.paths['/invalid_header']);
delete(filteredSwagger.paths['/hello'].get.parameters[0]['x-remove-me'])

// hokey algorithm, but at least it's different than the one it's testing
var OMIT = ['x-swagger-router-controller', 'x-swagger-pipe', 'x-hidden', 'x-private', 'x-controller-interface'];
_.forEach(filteredSwagger.paths, function(element, name) {
filteredSwagger.paths[name] = _.omit(element, OMIT);
_.forEach(filteredSwagger.paths[name], function(element, subName) {
filteredSwagger.paths[name][subName] = _.omit(element, OMIT);
});
});

return filteredSwagger
}

function inflateDocWithAVeryBigTypeDefinition(swagger) {
var props;
swagger.definitions.veryVeryBigCompoundType = {
type: "object",
properties: props = {
some_string: {
type: "string",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
}
}
};

var i = 200;
while(--i) props["some_string" + i] = _.clone(props.some_string);

return swagger
}

0 comments on commit 8a67d79

Please sign in to comment.