Skip to content

Commit e2edde9

Browse files
committed
allow json service to accept other content types
1 parent f02feab commit e2edde9

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

src/JsonService.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@ import { Log } from './Log';
55
import { Global } from './Global';
66

77
export class JsonService {
8-
constructor(XMLHttpRequestCtor = Global.XMLHttpRequest) {
8+
constructor(additionalContentTypes = null, XMLHttpRequestCtor = Global.XMLHttpRequest) {
9+
if (additionalContentTypes && Array.isArray(additionalContentTypes))
10+
{
11+
this._contentTypes = additionalContentTypes.slice();
12+
}
13+
else
14+
{
15+
this._contentTypes = [];
16+
}
17+
this._contentTypes.push('application/json');
18+
919
this._XMLHttpRequest = XMLHttpRequestCtor;
1020
}
1121

@@ -22,23 +32,36 @@ export class JsonService {
2232
var req = new this._XMLHttpRequest();
2333
req.open('GET', url);
2434

35+
var allowedContentTypes = this._contentTypes;
36+
2537
req.onload = function() {
2638
Log.debug("JsonService.getJson: HTTP response received, status", req.status);
2739

2840
if (req.status === 200) {
41+
2942
var contentType = req.getResponseHeader("Content-Type");
30-
if (contentType && contentType.startsWith("application/json")) {
31-
try {
32-
resolve(JSON.parse(req.responseText));
33-
}
34-
catch (e) {
35-
Log.error("JsonService.getJson: Error parsing JSON response", e.message);
36-
reject(e);
43+
if (contentType) {
44+
45+
var found = allowedContentTypes.find(item=>{
46+
if (contentType.startsWith(item)) {
47+
return true;
48+
}
49+
});
50+
51+
if (found) {
52+
try {
53+
resolve(JSON.parse(req.responseText));
54+
return;
55+
}
56+
catch (e) {
57+
Log.error("JsonService.getJson: Error parsing JSON response", e.message);
58+
reject(e);
59+
return;
60+
}
3761
}
3862
}
39-
else {
40-
reject(Error("Invalid response Content-Type: " + contentType + ", from URL: " + url));
41-
}
63+
64+
reject(Error("Invalid response Content-Type: " + contentType + ", from URL: " + url));
4265
}
4366
else {
4467
reject(Error(req.statusText + " (" + req.status + ")"));

src/MetadataService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class MetadataService {
1414
}
1515

1616
this._settings = settings;
17-
this._jsonService = new JsonServiceCtor();
17+
this._jsonService = new JsonServiceCtor(['application/jwk-set+json']);
1818
}
1919

2020
get metadataUrl() {

test/unit/JsonService.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("JsonService", function() {
1414

1515
beforeEach(function(){
1616
stubHttpRequest = new StubXMLHttpRequest();
17-
subject = new JsonService(()=>stubHttpRequest);
17+
subject = new JsonService(null, ()=>stubHttpRequest);
1818
});
1919

2020
describe("getJson", function() {
@@ -112,6 +112,21 @@ describe("JsonService", function() {
112112
stubHttpRequest.responseText = JSON.stringify({foo:1, bar:'test'});
113113
stubHttpRequest.onload();
114114
});
115+
116+
it("should accept custom content type in response", function(done) {
117+
subject = new JsonService(['foo/bar'], ()=>stubHttpRequest);
118+
let p = subject.getJson("http://test");
119+
120+
p.then(result => {
121+
result.foo.should.equal(1);
122+
done();
123+
});
124+
125+
stubHttpRequest.status = 200;
126+
stubHttpRequest.responseHeaders.set('Content-Type', 'foo/bar');
127+
stubHttpRequest.responseText = JSON.stringify({foo:1, bar:'test'});
128+
stubHttpRequest.onload();
129+
});
115130
});
116131
});
117132

0 commit comments

Comments
 (0)