Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Commit

Permalink
added test for stream validation bug #463
Browse files Browse the repository at this point in the history
  • Loading branch information
smoebody committed Dec 19, 2016
1 parent 6851b2a commit f065acf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '2'
services:
node:
image: docker.io/smoebody/dev-nodejs:6
volumes:
- ./:/app:z
- npm-cache:/home/dev/.npm:z
ports:
- 127.0.0.1:5858:5858
environment:
- NODE_ENV=development
command:
- npm test

volumes:
npm-cache: {}
51 changes: 51 additions & 0 deletions test/2.0/test-middleware-swagger-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var async = require('async');
var helpers = require('../helpers');
var request = require('supertest');
var stream = require('stream');
var fs = require('fs');
var crypto = require('crypto');

var petStoreJson = _.cloneDeep(require('../../samples/2.0/petstore.json'));

Expand Down Expand Up @@ -1575,6 +1577,55 @@ describe('Swagger Validator Middleware v2.0', function () {
});
});

it('should validate file and return it (issue #463)', function (done) {
var cPetStore = _.cloneDeep(petStoreJson);

cPetStore.paths['/image'] = {
get: {
summary: 'Get image',
description: 'Retrieves image.',
operationId: 'getImage',
produces: ['image/png'],
responses: {
'200': {
description: 'OK',
schema: {
type: 'file'
}
}
}
}
};

helpers.createServer([cPetStore], {
swaggerRouterOptions: {
controllers: {
getImage: function (req, res) {
res.setHeader('Content-type', 'image/png');
return fs.createReadStream('test/image.png').pipe(res);
}
}
},
swaggerValidatorOptions: {
validateResponse: true
}
}, function (app) {
try {
request(app)
.get('/api/image')
.end(function(err, res) {
assert.strictEqual(
crypto.createHash('sha256').update(fs.readFileSync('test/image.png')).digest('hex'), //expected
crypto.createHash('sha256').update(res.body).digest('hex') //actual
);
done(err);
});
} catch (err) {
done();
}
});
});

it('should not throw an error for empty responses that validate void (new issue)', function (done) {
var cPetStoreJson = _.cloneDeep(petStoreJson);

Expand Down
Binary file added test/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f065acf

Please sign in to comment.