Skip to content

Commit d3c987f

Browse files
author
Vladimir Enchev
committed
XMLHttpRequest separated from http module
1 parent dea2e69 commit d3c987f

File tree

9 files changed

+429
-414
lines changed

9 files changed

+429
-414
lines changed

CrossPlatformModules.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
<TypeScriptCompile Include="apps\tests\frame-tests.ts" />
183183
<TypeScriptCompile Include="apps\tests\gestures-tests.ts" />
184184
<TypeScriptCompile Include="apps\tests\fetch-tests.ts" />
185+
<TypeScriptCompile Include="apps\tests\xhr-tests.ts" />
185186
<TypeScriptCompile Include="apps\tests\layouts\dock-layout-tests.ts" />
186187
<TypeScriptCompile Include="apps\tests\pages\app.ts" />
187188
<TypeScriptCompile Include="apps\tests\pages\file-load-test.ts" />
@@ -1032,6 +1033,7 @@
10321033
</TypeScriptCompile>
10331034
<TypeScriptCompile Include="apps\xml-demo\app.ts" />
10341035
<TypeScriptCompile Include="apps\xml-demo\mainPage.ts" />
1036+
<TypeScriptCompile Include="xhr\xhr.ts" />
10351037
<TypeScriptCompile Include="xml\xml.d.ts" />
10361038
<TypeScriptCompile Include="data\observable-array\observable-array.d.ts" />
10371039
<TypeScriptCompile Include="apps\tests\observable-array-tests.ts" />
@@ -1688,6 +1690,10 @@
16881690
<Content Include="apps\list-view-demo\package.json">
16891691
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16901692
</Content>
1693+
<Content Include="xhr\package.json">
1694+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1695+
</Content>
1696+
<Content Include="xhr\Readme.md" />
16911697
<None Include="js-libs\esprima\LICENSE.BSD" />
16921698
<Content Include="source-control.md" />
16931699
<Content Include="ui\segmented-bar\package.json">

apps/tests/http-tests.ts

Lines changed: 3 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export var test_getImage = function (done) {
110110
// <snippet module="http" title="http">
111111
// ### Get Image from URL
112112
// ``` JavaScript
113-
http.getImage("http://www.google.com/images/errors/logo_sm_2.png").then(function (r) {
113+
http.getImage("https://httpbin.org/image/png").then(function (r) {
114114
//// Argument (r) is Image!
115115
// <hide>
116116
result = r;
@@ -309,7 +309,7 @@ export var test_request_responseContentToJSONShouldReturnJSON = function (done)
309309
export var test_request_responseContentToImageShouldReturnCorrectImage = function (done) {
310310
var result;
311311

312-
http.request({ url: "http://www.google.com/images/errors/logo_sm_2.png", method: "GET" }).then(function (response) {
312+
http.request({ url: "https://httpbin.org/image/png", method: "GET" }).then(function (response) {
313313
response.content.toImage().then((source) => {
314314
result = source;
315315
try {
@@ -452,180 +452,4 @@ function doRequest(url: string, done: Function) {
452452
}, function (e) {
453453
done(e);
454454
});
455-
}
456-
457-
export var test_XMLHttpRequest_isDefined = function () {
458-
TKUnit.assert(types.isDefined(global["XMLHttpRequest"]), "XMLHttpRequest should be defined!");
459-
};
460-
461-
var xhr = new XMLHttpRequest();
462-
463-
export var test_XMLHttpRequest_open_isDefined = function () {
464-
TKUnit.assert(types.isFunction(xhr.open), "XMLHttpRequest.open should be defined!");
465-
};
466-
467-
export var test_XMLHttpRequest_send_isDefined = function () {
468-
TKUnit.assert(types.isFunction(xhr.send), "XMLHttpRequest.send should be defined!");
469-
};
470-
471-
export var test_XMLHttpRequest_setRequestHeader_isDefined = function () {
472-
TKUnit.assert(types.isFunction(xhr.setRequestHeader), "XMLHttpRequest.setRequestHeader should be defined!");
473-
};
474-
475-
export var test_XMLHttpRequest_getAllResponseHeaders_isDefined = function () {
476-
TKUnit.assert(types.isFunction(xhr.getAllResponseHeaders), "XMLHttpRequest.getAllResponseHeaders should be defined!");
477-
};
478-
479-
export var test_XMLHttpRequest_getResponseHeader_isDefined = function () {
480-
TKUnit.assert(types.isFunction(xhr.getResponseHeader), "XMLHttpRequest.getResponseHeader should be defined!");
481-
};
482-
483-
export var test_XMLHttpRequest_overrideMimeType_isDefined = function () {
484-
TKUnit.assert(types.isFunction(xhr.overrideMimeType), "XMLHttpRequest.overrideMimeType should be defined!");
485-
};
486-
487-
export var test_XMLHttpRequest_readyState_isDefined = function () {
488-
TKUnit.assert(types.isDefined(xhr.readyState), "XMLHttpRequest.readyState should be defined!");
489-
};
490-
491-
export var test_XMLHttpRequest_responseText_isDefined = function () {
492-
TKUnit.assert(types.isDefined(xhr.responseText), "XMLHttpRequest.responseText should be defined!");
493-
};
494-
495-
export var test_XMLHttpRequest_readyStateShouldChange = function (done) {
496-
var count = 0;
497-
xhr = new XMLHttpRequest();
498-
499-
TKUnit.assert(xhr.readyState === 0, "xhr.readyState should be UNSENT!");
500-
501-
xhr.onreadystatechange = function () {
502-
try {
503-
504-
if (count === 0) {
505-
TKUnit.assert(xhr.readyState === 1, "xhr.readyState should be OPEN!");
506-
} else if (count === 1) {
507-
TKUnit.assert(xhr.readyState === 2, "xhr.readyState should be HEADERS_RECEIVED!");
508-
} else if (count === 2) {
509-
TKUnit.assert(xhr.readyState === 3, "xhr.readyState should be LOADING!");
510-
} else if (count === 3) {
511-
TKUnit.assert(xhr.readyState === 4, "xhr.readyState should be DONE!");
512-
}
513-
514-
count++;
515-
516-
done(null);
517-
}
518-
catch (err) {
519-
done(err);
520-
}
521-
};
522-
523-
xhr.open("GET", "https://httpbin.org/get");
524-
xhr.send();
525-
};
526-
527-
export var test_XMLHttpRequest_headersSentAndReceivedProperly = function (done) {
528-
xhr = new XMLHttpRequest();
529-
xhr.open("GET", "https://httpbin.org/get");
530-
xhr.setRequestHeader("Content-Type", "application/json");
531-
xhr.onreadystatechange = function () {
532-
if (xhr.readyState > 1) {
533-
try {
534-
TKUnit.assert(xhr.getResponseHeader("Content-Type") === "application/json", "Headers not sent/received properly!");
535-
done(null);
536-
}
537-
catch (err) {
538-
done(err);
539-
}
540-
}
541-
};
542-
xhr.send();
543-
};
544-
545-
export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done) {
546-
xhr = new XMLHttpRequest();
547-
xhr.open("POST", "https://httpbin.org/post");
548-
xhr.setRequestHeader("Content-Type", "application/json");
549-
xhr.onreadystatechange = function () {
550-
if (xhr.readyState > 3) {
551-
var result = JSON.parse(xhr.responseText);
552-
try {
553-
TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
554-
done(null);
555-
}
556-
catch (err) {
557-
done(err);
558-
}
559-
}
560-
};
561-
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
562-
};
563-
564-
export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) {
565-
var flag = false;
566-
567-
xhr = new XMLHttpRequest();
568-
xhr.open("POST", "https://httpbin.org/post");
569-
xhr.setRequestHeader("Content-Type", "application/json");
570-
xhr.onreadystatechange = function () {
571-
flag = true;
572-
};
573-
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
574-
xhr.abort();
575-
576-
TKUnit.assert(flag === false, "Content not sent/received properly!");
577-
done(null);
578-
};
579-
580-
export var test_XMLHttpRequest_requestShouldBePossibleAfterAbort = function (done) {
581-
xhr = new XMLHttpRequest();
582-
xhr.open("POST", "https://httpbin.org/post");
583-
xhr.setRequestHeader("Content-Type", "application/json");
584-
xhr.onreadystatechange = function () {
585-
if (xhr.readyState > 3) {
586-
var result = JSON.parse(xhr.responseText);
587-
try {
588-
TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
589-
done(null);
590-
}
591-
catch (err) {
592-
done(err);
593-
}
594-
}
595-
};
596-
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
597-
xhr.abort();
598-
599-
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
600-
};
601-
602-
export function test_raises_onload_Event(done) {
603-
let xhr = new XMLHttpRequest();
604-
xhr.onload = () => {
605-
done(null);
606-
}
607-
xhr.open("GET", "https://httpbin.org/get");
608-
xhr.send();
609-
}
610-
611-
export function test_raises_onerror_Event(done) {
612-
let xhr = new XMLHttpRequest();
613-
xhr.onerror = () => {
614-
done(null);
615-
}
616-
xhr.open("GET", "https://no-such-domain-httpbin.org");
617-
xhr.send();
618-
}
619-
620-
export function test_responseType(done) {
621-
let xhr = new XMLHttpRequest();
622-
xhr.responseType = "";
623-
xhr.responseType = "text";
624-
625-
TKUnit.assertThrows(
626-
() => xhr.responseType = "json",
627-
"Didn't raise on unsupported type.",
628-
"Response type of 'json' not supported."
629-
);
630-
done(null);
631-
}
455+
}

apps/tests/testRunner.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ allTests["STYLE-PROPERTIES"] = require("./ui/style/style-properties-tests");
3636
allTests["SCROLL-VIEW"] = require("./ui/scroll-view/scroll-view-tests");
3737
allTests["FILE SYSTEM"] = require("./file-system-tests");
3838
allTests["HTTP"] = require("./http-tests");
39+
allTests["XHR"] = require("./xhr-tests");
3940
allTests["FETCH"] = require("./fetch-tests");
4041
allTests["APPLICATION SETTINGS"] = require("./application-settings-tests");
4142
allTests["IMAGE SOURCE"] = require("./image-source-tests");

0 commit comments

Comments
 (0)