From 7f9cb814213619667068bc70bcef13d2b238657c Mon Sep 17 00:00:00 2001 From: Ulf Seltmann Date: Mon, 19 Dec 2016 17:52:59 +0100 Subject: [PATCH] added test for stream validation bug #463 --- test/2.0/test-middleware-swagger-validator.js | 51 ++++++++++++++++++ test/image.png | Bin 0 -> 156 bytes 2 files changed, 51 insertions(+) create mode 100644 test/image.png diff --git a/test/2.0/test-middleware-swagger-validator.js b/test/2.0/test-middleware-swagger-validator.js index 45130672b2..7a36102375 100644 --- a/test/2.0/test-middleware-swagger-validator.js +++ b/test/2.0/test-middleware-swagger-validator.js @@ -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')); @@ -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); diff --git a/test/image.png b/test/image.png new file mode 100644 index 0000000000000000000000000000000000000000..bdcadbd1d0391b2589836cef6f5d0ffc08822452 GIT binary patch literal 156 zcmeAS@N?(olHy`uVBq!ia0vp^j6lrI!2~1?-xTZsQk(@Ik;OnUVGw3ym^DWND9B#o z>Fdh=fJc~LSb3_%<$j=$Y-UJAiF1B#Zfaf$kjuc}T$GwvlA5AWo>`Ki;O^-gkfN8$ t4ip#iba4#fxSssu|9^X7W@ctX35NPKW}z#W*#bb?JYD@<);T3K0RW9_BvJqX literal 0 HcmV?d00001