diff --git a/lib/swagger.js b/lib/swagger.js index 8ae188731..0590968aa 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -71,17 +71,17 @@ Object.keys = Object.keys || (function () { var hasOwnProperty = Object.prototype.hasOwnProperty, - hasDontEnumBug = !{ toString: null }.propertyIsEnumerable("toString"), - DontEnums = [ - 'toString', - 'toLocaleString', - 'valueOf', - 'hasOwnProperty', - 'isPrototypeOf', - 'propertyIsEnumerable', - 'constructor' - ], - DontEnumsLength = DontEnums.length; + hasDontEnumBug = !{ toString: null }.propertyIsEnumerable("toString"), + DontEnums = [ + 'toString', + 'toLocaleString', + 'valueOf', + 'hasOwnProperty', + 'isPrototypeOf', + 'propertyIsEnumerable', + 'constructor' + ], + DontEnumsLength = DontEnums.length; return function (o) { if (typeof o != "object" && typeof o != "function" || o === null) @@ -128,6 +128,8 @@ if (options.url != null) this.url = options.url; + this.swaggerRequstHeaders = options.swaggerRequstHeaders || 'application/json,application/json;charset=utf-8,*/*'; + if (options.success != null) this.success = options.success; @@ -149,7 +151,7 @@ } }; - SwaggerApi.prototype.build = function () { + SwaggerApi.prototype.build = function (mock) { if (this.isBuilt) return this; var _this = this; @@ -159,7 +161,7 @@ url: this.url, method: "get", headers: { - accept: "application/json,application/json;charset=utf-8,*/*" + accept: _this.swaggerRequstHeaders }, on: { error: function (response) { @@ -186,6 +188,9 @@ }; var e = (typeof window !== 'undefined' ? window : exports); e.authorizations.apply(obj); + if (mock === true) + return obj; + new SwaggerHttp().execute(obj); return this; }; @@ -360,7 +365,7 @@ var SwaggerResource = function (resourceObj, api) { var _this = this; this.api = api; - this.api = this.api; + this.swaggerRequstHeaders = api.swaggerRequstHeaders; var consumes = (this.consumes | []); var produces = (this.produces | []); this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path; @@ -393,7 +398,7 @@ method: "get", useJQuery: this.useJQuery, headers: { - accept: "application/json,application/json;charset=utf-8,*/*" + accept: this.swaggerRequstHeaders }, on: { response: function (resp) { diff --git a/test/request.js b/test/request.js index ae5bc35aa..fdac446b4 100644 --- a/test/request.js +++ b/test/request.js @@ -1,6 +1,7 @@ var test = require('unit.js') var should = require('should') var mock = require('../test/mock'); +var swagger = require('../lib/swagger'); var sample, instance; describe('request operations', function() { @@ -195,4 +196,19 @@ describe('request operations', function() { test.object(req.body); should(req.body.id).equal(1); }) + + it('verifies headers when fetching the swagger specification', function() { + var sample = new swagger.SwaggerApi('http://localhost:8000/api-docs.json'); + var req = sample.build(true); + should(req.headers['accept']).equal('application/json,application/json;charset=utf-8,*/*'); + }) + + it('allows override of headers when fetching the swagger specification', function() { + var opts = { + swaggerRequstHeaders: 'foo/bar' + }; + var sample = new swagger.SwaggerApi('http://localhost:8000/api-docs.json', opts); + var req = sample.build(true); + should(req.headers['accept']).equal('foo/bar'); + }) }) \ No newline at end of file