Skip to content

Commit f3e4de0

Browse files
committed
Added http.withCredentials option, closes #4
1 parent 2dd5d46 commit f3e4de0

File tree

9 files changed

+1231
-711
lines changed

9 files changed

+1231
-711
lines changed

dist/ref-parser.js

+1,041-578
Large diffs are not rendered by default.

dist/ref-parser.js.map

+20-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js

+91-90
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js.map

+22-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/options.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ $RefParser.dereference("my-schema.yaml", {
3333
|`cache.fs` |number |60 |<a name="caching"></a>The length of time (in seconds) to cache local files. The default is one minute. Setting to zero will cache forever.
3434
|`cache.http` |number |300 |The length of time (in seconds) to cache HTTP URLs. The default is five minutes. Setting to zero will cache forever.
3535
|`cache.https` |number |300 |The length of time (in seconds) to cache HTTPS URLs. The default is five minutes. Setting to zero will cache forever.
36+
|`http.withCredentials` |bool |true |When used in browser specifies `withCredentials` option of `XMLHttpRequest` object. Setting to `false` allows loading via CORS with `Access-Control-Allow-Origin` set to `*`

lib/options.js

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ function $RefParserOptions(options) {
9292
https: 5 * 60 // 5 minutes
9393
};
9494

95+
/* http options */
96+
this.http = {
97+
/* withCredentials option of XMLHttpRequest */
98+
withCredentials: true
99+
}
100+
95101
merge(options, this);
96102
}
97103

lib/read.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ function download(protocol, u, options) {
178178
hostname: u.hostname,
179179
port: u.port,
180180
path: u.path,
181-
auth: u.auth
181+
auth: u.auth,
182+
withCredentials: options.http.withCredentials
182183
},
183184
onResponse
184185
);

tests/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
<script src="specs/callbacks.spec.js"></script>
8484
<script src="specs/yaml.spec.js"></script>
8585

86+
<script src="specs/cors.spec.js"></script>
87+
8688
<script>
8789
mocha.run();
8890
</script>

tests/specs/cors/cors.spec.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
describe('parse from CORS with Access-Control-Allow-Origin: *', function() {
4+
var windowOnError, testDone;
5+
6+
beforeEach(function() {
7+
windowOnError = global.onerror;
8+
global.onerror = function() {
9+
testDone();
10+
return true;
11+
}
12+
});
13+
14+
afterEach(function() {
15+
global.onerror = windowOnError;
16+
});
17+
18+
it('should parse successfully with http.withCredentials = false', function() {
19+
var parser = new $RefParser();
20+
return parser
21+
.parse('http://petstore.swagger.io:80/v2/swagger.json', {
22+
http: { withCredentials: false }
23+
})
24+
.then(function(schema) {
25+
expect(schema).to.be.an('object');
26+
expect(schema).not.to.be.empty;
27+
expect(parser.schema).to.equal(schema);
28+
});
29+
});
30+
31+
if (userAgent.isBrowser) {
32+
it('should throw error in browser if http.withCredentials = true (default)', function(done) {
33+
testDone = done;
34+
var parser = new $RefParser();
35+
return parser
36+
.parse('http://petstore.swagger.io:80/v2/swagger.json', {
37+
http: { withCredentials: true }
38+
})
39+
.then(helper.shouldNotGetCalled)
40+
.catch(function(err) {
41+
expect(err.message).to.contain('Error downloading file');
42+
done();
43+
});
44+
});
45+
}
46+
});

0 commit comments

Comments
 (0)