Skip to content

Commit

Permalink
fix, tests for #120, overriding headers when fetching swagger spec
Browse files Browse the repository at this point in the history
  • Loading branch information
fehguy committed Jan 6, 2015
1 parent e6276a7 commit 85bede0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
35 changes: 20 additions & 15 deletions lib/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -149,7 +151,7 @@
}
};

SwaggerApi.prototype.build = function () {
SwaggerApi.prototype.build = function (mock) {
if (this.isBuilt)
return this;
var _this = this;
Expand All @@ -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) {
Expand All @@ -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;
};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions test/request.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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');
})
})

0 comments on commit 85bede0

Please sign in to comment.