Skip to content

Commit eb73219

Browse files
committed
consolidate response parsing into url utility
1 parent a4be1c8 commit eb73219

4 files changed

Lines changed: 74 additions & 98 deletions

File tree

‎src/AuthorizationResultParser.js‎

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎src/UrlUtility.js‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
1+
import Log from './Log';
2+
import Window from './Window';
3+
14
export default class UrlUtility {
25
static addQueryParam(url, name, value) {
3-
if (url.indexOf('?') < 0){
6+
if (url.indexOf('?') < 0) {
47
url += "?";
58
}
6-
7-
if (url[url.length - 1] !== "?"){
9+
10+
if (url[url.length - 1] !== "?") {
811
url += "&";
912
}
10-
13+
1114
url += encodeURIComponent(name);
1215
url += "=";
1316
url += encodeURIComponent(value);
14-
17+
1518
return url;
1619
}
20+
21+
static parseUrlFragment(value, window = Window) {
22+
Log.info("UrlUtility.parseUrlFragment");
23+
24+
value = value || window.location.hash;
25+
26+
var idx = value.lastIndexOf("#");
27+
if (idx >= 0) {
28+
value = value.substr(idx + 1);
29+
}
30+
31+
var params = {},
32+
regex = /([^&=]+)=([^&]*)/g,
33+
m;
34+
35+
var counter = 0;
36+
while (m = regex.exec(value)) {
37+
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
38+
if (counter++ > 50) {
39+
Log.error("response exceeded expected number of parameters", value);
40+
return {
41+
error: "Response exceeded expected number of parameters"
42+
};
43+
}
44+
}
45+
46+
for (var prop in params) {
47+
return params;
48+
}
49+
}
1750
}

‎test/AuthorizationResultParser.spec.js‎

Lines changed: 0 additions & 49 deletions
This file was deleted.

‎test/UrlUtility.spec.js‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ describe("UrlUtility", function() {
1111
it("should add ? if not present", function() {
1212
UrlUtility.addQueryParam("url", "foo", "test").should.equal("url?foo=test");
1313
});
14-
14+
1515
it("should not add ? if already present", function() {
1616
UrlUtility.addQueryParam("url?", "foo", "test").should.equal("url?foo=test");
1717
});
18-
18+
1919
it("should add & if needed", function() {
2020
UrlUtility.addQueryParam("url?x=1", "foo", "test").should.equal("url?x=1&foo=test");
2121
});
@@ -24,5 +24,38 @@ describe("UrlUtility", function() {
2424
UrlUtility.addQueryParam("url", "#", "#").should.equal("url?%23=%23");
2525
});
2626
});
27-
27+
28+
describe("parseUrlFragment", function() {
29+
30+
it("should parse key/value pairs", function() {
31+
let result = UrlUtility.parseUrlFragment("a=apple&b=banana&c=carrot");
32+
result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" });
33+
});
34+
35+
it("should parse any order", function() {
36+
let result = UrlUtility.parseUrlFragment("b=banana&c=carrot&a=apple");
37+
result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" });
38+
});
39+
40+
it("should parse past host name and hash fragment", function() {
41+
let result = UrlUtility.parseUrlFragment("http://server?test1=xoxo&test2=xoxo/#a=apple&b=banana&c=carrot");
42+
result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" });
43+
});
44+
45+
it("should return error for long values", function() {
46+
let result = UrlUtility.parseUrlFragment("a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple&a=apple");
47+
result.should.have.property('error');
48+
});
49+
50+
it("should use window.location.hash when no value is passed", function() {
51+
let w = {
52+
location: {
53+
hash: "a=apple&b=banana&c=carrot"
54+
}
55+
};
56+
let result = UrlUtility.parseUrlFragment(null, w);
57+
result.should.deep.equal({ a: "apple", b: "banana", c: "carrot" });
58+
});
59+
});
60+
2861
});

0 commit comments

Comments
 (0)