Skip to content

Commit 7040403

Browse files
add timeout to xhr
1 parent 978500d commit 7040403

File tree

4 files changed

+84
-15
lines changed

4 files changed

+84
-15
lines changed

lib/requests/xhr.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ var xhrRequest = function (options) {
6565
}
6666
};
6767

68+
if (options.timeout) {
69+
request.timeout = options.timeout;
70+
}
71+
6872
request.onerror = function () {
6973
errorCallback({
7074
status: request.status,

lib/utilities/Utility.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function isNull (value) {
1+
function isNull(value) {
22
return typeof value === "undefined" || typeof value === "unknown" || value == null;
33
}
44

@@ -37,16 +37,12 @@ function getXrmContext() {
3737
function getClientUrl() {
3838
var context = getXrmContext();
3939

40-
if (context) {
41-
var clientUrl = context.getClientUrl();
40+
var clientUrl = context.getClientUrl();
4241

43-
if (clientUrl.match(/\/$/)) {
44-
clientUrl = clientUrl.substring(0, clientUrl.length - 1);
45-
}
46-
return clientUrl;
42+
if (clientUrl.match(/\/$/)) {
43+
clientUrl = clientUrl.substring(0, clientUrl.length - 1);
4744
}
48-
49-
return '';
45+
return clientUrl;
5046
}
5147

5248
function initWebApiUrl(version) {

tests/common-tests.js

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,57 @@ describe("Utility.", function () {
142142
});
143143
});
144144

145+
describe("getXrmContext - Xrm.Page is null", function () {
146+
before(function () {
147+
global.Xrm.Page = undefined;
148+
});
149+
150+
after(function () {
151+
global.Xrm.Page = {
152+
context: {
153+
getClientUrl: function () {
154+
return "http://testorg.crm.dynamics.com";
155+
}
156+
}
157+
};
158+
});
159+
160+
it("throws an error", function () {
161+
expect(function () {
162+
Utility.getXrmContext();
163+
}).to.throw();
164+
});
165+
});
166+
167+
describe("getXrmContext - Xrm.Page.context is null", function () {
168+
before(function () {
169+
global.Xrm.Page.context = null;
170+
});
171+
172+
after(function () {
173+
global.Xrm.Page.context = {
174+
getClientUrl: function () {
175+
return "http://testorg.crm.dynamics.com";
176+
}
177+
};
178+
});
179+
180+
it("returns a correct string", function () {
181+
expect(function () {
182+
Utility.getXrmContext();
183+
}).to.throw();
184+
});
185+
});
186+
145187
describe("getClientUrl - removes a slash at the end", function () {
146188
before(function () {
147-
Xrm.Page.context.getClientUrl = function () {
189+
global.Xrm.Page.context.getClientUrl = function () {
148190
return "http://testorg.crm.dynamics.com/";
149191
};
150192
});
151193

152194
after(function () {
153-
Xrm.Page.context.getClientUrl = function () {
195+
global.Xrm.Page.context.getClientUrl = function () {
154196
return "http://testorg.crm.dynamics.com";
155197
};
156198
});
@@ -161,6 +203,31 @@ describe("Utility.", function () {
161203
expect(result).to.be.eq("http://testorg.crm.dynamics.com");
162204
});
163205
});
206+
207+
describe("getXrmInternal", function () {
208+
before(function () {
209+
global.Xrm.Internal = "internal";
210+
});
211+
212+
after(function () {
213+
global.Xrm.Internal = undefined;
214+
215+
});
216+
217+
it("returns a correct string", function () {
218+
var result = Utility.getXrmInternal();
219+
220+
expect(result).to.be.eq("internal");
221+
});
222+
});
223+
224+
describe("getXrmInternal - returns null if does not exist", function () {
225+
it("returns a correct string", function () {
226+
var result = Utility.getXrmInternal();
227+
228+
expect(result).to.be.undefined;
229+
});
230+
});
164231
});
165232

166233
describe("RequestConverter.convertRequestOptions -", function () {

tests/xhr-tests.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ describe("xhr -", function() {
259259
id: mocks.data.testEntityId,
260260
collection: "tests",
261261
entity: mocks.data.testEntity
262-
}
262+
};
263263

264264
var responseObject;
265265
var responseObject2;
@@ -748,7 +748,7 @@ describe("xhr -", function() {
748748
});
749749
});
750750

751-
describe("xhr - request error", function () {
751+
describe("request error", function () {
752752
var responseObject;
753753
before(function (done) {
754754
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
@@ -795,7 +795,7 @@ describe("xhr -", function() {
795795
});
796796
});
797797

798-
describe("xhr - request timeout", function () {
798+
describe("request timeout", function () {
799799
var responseObject;
800800
before(function (done) {
801801
global.XMLHttpRequest = sinon.useFakeXMLHttpRequest();
@@ -805,7 +805,9 @@ describe("xhr -", function() {
805805
requests.push(xhr);
806806
};
807807

808-
dynamicsWebApiTest.create(mocks.data.testEntity, "tests").then(function (object) {
808+
var dynamicsWebApiTimeout = new DynamicsWebApi({ webApiVersion: "8.2", timeout: 100 });
809+
810+
dynamicsWebApiTimeout.create(mocks.data.testEntity, "tests").then(function (object) {
809811
responseObject = object;
810812
done();
811813
}).catch(function (object) {

0 commit comments

Comments
 (0)