diff --git a/CrossPlatformModules.csproj b/CrossPlatformModules.csproj index 4ccb31161..a0a81df7a 100644 --- a/CrossPlatformModules.csproj +++ b/CrossPlatformModules.csproj @@ -182,6 +182,7 @@ + @@ -1032,6 +1033,7 @@ + @@ -1688,6 +1690,10 @@ PreserveNewest + + PreserveNewest + + diff --git a/apps/tests/http-tests.ts b/apps/tests/http-tests.ts index 58abade52..15e125b7d 100644 --- a/apps/tests/http-tests.ts +++ b/apps/tests/http-tests.ts @@ -110,7 +110,7 @@ export var test_getImage = function (done) { // // ### Get Image from URL // ``` JavaScript - http.getImage("http://www.google.com/images/errors/logo_sm_2.png").then(function (r) { + http.getImage("https://httpbin.org/image/png").then(function (r) { //// Argument (r) is Image! // result = r; @@ -309,7 +309,7 @@ export var test_request_responseContentToJSONShouldReturnJSON = function (done) export var test_request_responseContentToImageShouldReturnCorrectImage = function (done) { var result; - http.request({ url: "http://www.google.com/images/errors/logo_sm_2.png", method: "GET" }).then(function (response) { + http.request({ url: "https://httpbin.org/image/png", method: "GET" }).then(function (response) { response.content.toImage().then((source) => { result = source; try { @@ -452,180 +452,4 @@ function doRequest(url: string, done: Function) { }, function (e) { done(e); }); -} - -export var test_XMLHttpRequest_isDefined = function () { - TKUnit.assert(types.isDefined(global["XMLHttpRequest"]), "XMLHttpRequest should be defined!"); -}; - -var xhr = new XMLHttpRequest(); - -export var test_XMLHttpRequest_open_isDefined = function () { - TKUnit.assert(types.isFunction(xhr.open), "XMLHttpRequest.open should be defined!"); -}; - -export var test_XMLHttpRequest_send_isDefined = function () { - TKUnit.assert(types.isFunction(xhr.send), "XMLHttpRequest.send should be defined!"); -}; - -export var test_XMLHttpRequest_setRequestHeader_isDefined = function () { - TKUnit.assert(types.isFunction(xhr.setRequestHeader), "XMLHttpRequest.setRequestHeader should be defined!"); -}; - -export var test_XMLHttpRequest_getAllResponseHeaders_isDefined = function () { - TKUnit.assert(types.isFunction(xhr.getAllResponseHeaders), "XMLHttpRequest.getAllResponseHeaders should be defined!"); -}; - -export var test_XMLHttpRequest_getResponseHeader_isDefined = function () { - TKUnit.assert(types.isFunction(xhr.getResponseHeader), "XMLHttpRequest.getResponseHeader should be defined!"); -}; - -export var test_XMLHttpRequest_overrideMimeType_isDefined = function () { - TKUnit.assert(types.isFunction(xhr.overrideMimeType), "XMLHttpRequest.overrideMimeType should be defined!"); -}; - -export var test_XMLHttpRequest_readyState_isDefined = function () { - TKUnit.assert(types.isDefined(xhr.readyState), "XMLHttpRequest.readyState should be defined!"); -}; - -export var test_XMLHttpRequest_responseText_isDefined = function () { - TKUnit.assert(types.isDefined(xhr.responseText), "XMLHttpRequest.responseText should be defined!"); -}; - -export var test_XMLHttpRequest_readyStateShouldChange = function (done) { - var count = 0; - xhr = new XMLHttpRequest(); - - TKUnit.assert(xhr.readyState === 0, "xhr.readyState should be UNSENT!"); - - xhr.onreadystatechange = function () { - try { - - if (count === 0) { - TKUnit.assert(xhr.readyState === 1, "xhr.readyState should be OPEN!"); - } else if (count === 1) { - TKUnit.assert(xhr.readyState === 2, "xhr.readyState should be HEADERS_RECEIVED!"); - } else if (count === 2) { - TKUnit.assert(xhr.readyState === 3, "xhr.readyState should be LOADING!"); - } else if (count === 3) { - TKUnit.assert(xhr.readyState === 4, "xhr.readyState should be DONE!"); - } - - count++; - - done(null); - } - catch (err) { - done(err); - } - }; - - xhr.open("GET", "https://httpbin.org/get"); - xhr.send(); -}; - -export var test_XMLHttpRequest_headersSentAndReceivedProperly = function (done) { - xhr = new XMLHttpRequest(); - xhr.open("GET", "https://httpbin.org/get"); - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.onreadystatechange = function () { - if (xhr.readyState > 1) { - try { - TKUnit.assert(xhr.getResponseHeader("Content-Type") === "application/json", "Headers not sent/received properly!"); - done(null); - } - catch (err) { - done(err); - } - } - }; - xhr.send(); -}; - -export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done) { - xhr = new XMLHttpRequest(); - xhr.open("POST", "https://httpbin.org/post"); - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.onreadystatechange = function () { - if (xhr.readyState > 3) { - var result = JSON.parse(xhr.responseText); - try { - TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!"); - done(null); - } - catch (err) { - done(err); - } - } - }; - xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); -}; - -export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) { - var flag = false; - - xhr = new XMLHttpRequest(); - xhr.open("POST", "https://httpbin.org/post"); - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.onreadystatechange = function () { - flag = true; - }; - xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); - xhr.abort(); - - TKUnit.assert(flag === false, "Content not sent/received properly!"); - done(null); -}; - -export var test_XMLHttpRequest_requestShouldBePossibleAfterAbort = function (done) { - xhr = new XMLHttpRequest(); - xhr.open("POST", "https://httpbin.org/post"); - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.onreadystatechange = function () { - if (xhr.readyState > 3) { - var result = JSON.parse(xhr.responseText); - try { - TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!"); - done(null); - } - catch (err) { - done(err); - } - } - }; - xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); - xhr.abort(); - - xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); -}; - -export function test_raises_onload_Event(done) { - let xhr = new XMLHttpRequest(); - xhr.onload = () => { - done(null); - } - xhr.open("GET", "https://httpbin.org/get"); - xhr.send(); -} - -export function test_raises_onerror_Event(done) { - let xhr = new XMLHttpRequest(); - xhr.onerror = () => { - done(null); - } - xhr.open("GET", "https://no-such-domain-httpbin.org"); - xhr.send(); -} - -export function test_responseType(done) { - let xhr = new XMLHttpRequest(); - xhr.responseType = ""; - xhr.responseType = "text"; - - TKUnit.assertThrows( - () => xhr.responseType = "json", - "Didn't raise on unsupported type.", - "Response type of 'json' not supported." - ); - done(null); -} +} \ No newline at end of file diff --git a/apps/tests/testRunner.ts b/apps/tests/testRunner.ts index d38d8f33e..63178d4f5 100644 --- a/apps/tests/testRunner.ts +++ b/apps/tests/testRunner.ts @@ -36,6 +36,7 @@ allTests["STYLE-PROPERTIES"] = require("./ui/style/style-properties-tests"); allTests["SCROLL-VIEW"] = require("./ui/scroll-view/scroll-view-tests"); allTests["FILE SYSTEM"] = require("./file-system-tests"); allTests["HTTP"] = require("./http-tests"); +allTests["XHR"] = require("./xhr-tests"); allTests["FETCH"] = require("./fetch-tests"); allTests["APPLICATION SETTINGS"] = require("./application-settings-tests"); allTests["IMAGE SOURCE"] = require("./image-source-tests"); diff --git a/apps/tests/xhr-tests.ts b/apps/tests/xhr-tests.ts new file mode 100644 index 000000000..3836be4a3 --- /dev/null +++ b/apps/tests/xhr-tests.ts @@ -0,0 +1,183 @@ +/* tslint:disable:no-unused-variable */ +import TKUnit = require("./TKUnit"); +import types = require("utils/types"); + +export var test_XMLHttpRequest_isDefined = function () { + TKUnit.assert(types.isDefined(global["XMLHttpRequest"]), "XMLHttpRequest should be defined!"); +}; + +var xhr = new XMLHttpRequest(); + +export var test_XMLHttpRequest_open_isDefined = function () { + TKUnit.assert(types.isFunction(xhr.open), "XMLHttpRequest.open should be defined!"); +}; + +export var test_XMLHttpRequest_send_isDefined = function () { + TKUnit.assert(types.isFunction(xhr.send), "XMLHttpRequest.send should be defined!"); +}; + +export var test_XMLHttpRequest_setRequestHeader_isDefined = function () { + TKUnit.assert(types.isFunction(xhr.setRequestHeader), "XMLHttpRequest.setRequestHeader should be defined!"); +}; + +export var test_XMLHttpRequest_getAllResponseHeaders_isDefined = function () { + TKUnit.assert(types.isFunction(xhr.getAllResponseHeaders), "XMLHttpRequest.getAllResponseHeaders should be defined!"); +}; + +export var test_XMLHttpRequest_getResponseHeader_isDefined = function () { + TKUnit.assert(types.isFunction(xhr.getResponseHeader), "XMLHttpRequest.getResponseHeader should be defined!"); +}; + +export var test_XMLHttpRequest_overrideMimeType_isDefined = function () { + TKUnit.assert(types.isFunction(xhr.overrideMimeType), "XMLHttpRequest.overrideMimeType should be defined!"); +}; + +export var test_XMLHttpRequest_readyState_isDefined = function () { + TKUnit.assert(types.isNumber(xhr.readyState), "XMLHttpRequest.readyState should be defined!"); +}; + +export var test_XMLHttpRequest_responseText_isDefined = function () { + TKUnit.assert(types.isString(xhr.responseText), "XMLHttpRequest.responseText should be defined!"); +}; + +export var test_XMLHttpRequest_responseType_isDefined = function () { + TKUnit.assert(types.isString(xhr.responseType), "XMLHttpRequest.responseType should be defined!"); +}; + +export var test_XMLHttpRequest_readyStateShouldChange = function (done) { + var count = 0; + xhr = new XMLHttpRequest(); + + TKUnit.assert(xhr.readyState === 0, "xhr.readyState should be UNSENT!"); + + xhr.onreadystatechange = function () { + try { + + if (count === 0) { + TKUnit.assert(xhr.readyState === 1, "xhr.readyState should be OPEN!"); + } else if (count === 1) { + TKUnit.assert(xhr.readyState === 2, "xhr.readyState should be HEADERS_RECEIVED!"); + } else if (count === 2) { + TKUnit.assert(xhr.readyState === 3, "xhr.readyState should be LOADING!"); + } else if (count === 3) { + TKUnit.assert(xhr.readyState === 4, "xhr.readyState should be DONE!"); + } + + count++; + + done(null); + } + catch (err) { + done(err); + } + }; + + xhr.open("GET", "https://httpbin.org/get"); + xhr.send(); +}; + +export var test_XMLHttpRequest_headersSentAndReceivedProperly = function (done) { + xhr = new XMLHttpRequest(); + xhr.open("GET", "https://httpbin.org/get"); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.onreadystatechange = function () { + if (xhr.readyState > 1) { + try { + TKUnit.assert(xhr.getResponseHeader("Content-Type") === "application/json", "Headers not sent/received properly!"); + done(null); + } + catch (err) { + done(err); + } + } + }; + xhr.send(); +}; + +export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done) { + xhr = new XMLHttpRequest(); + xhr.open("POST", "https://httpbin.org/post"); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.onreadystatechange = function () { + if (xhr.readyState > 3) { + var result = JSON.parse(xhr.responseText); + try { + TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!"); + done(null); + } + catch (err) { + done(err); + } + } + }; + xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); +}; + +export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) { + var flag = false; + + xhr = new XMLHttpRequest(); + xhr.open("POST", "https://httpbin.org/post"); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.onreadystatechange = function () { + flag = true; + }; + xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); + xhr.abort(); + + TKUnit.assert(flag === false, "Content not sent/received properly!"); + done(null); +}; + +export var test_XMLHttpRequest_requestShouldBePossibleAfterAbort = function (done) { + xhr = new XMLHttpRequest(); + xhr.open("POST", "https://httpbin.org/post"); + xhr.setRequestHeader("Content-Type", "application/json"); + xhr.onreadystatechange = function () { + if (xhr.readyState > 3) { + var result = JSON.parse(xhr.responseText); + try { + TKUnit.assert(result["json"]["MyVariableOne"] === "ValueOne" && result["json"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!"); + done(null); + } + catch (err) { + done(err); + } + } + }; + xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); + xhr.abort(); + + xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); +}; + +export function test_raises_onload_Event(done) { + let xhr = new XMLHttpRequest(); + xhr.onload = () => { + done(null); + } + xhr.open("GET", "https://httpbin.org/get"); + xhr.send(); +} + +export function test_raises_onerror_Event(done) { + let xhr = new XMLHttpRequest(); + xhr.onerror = () => { + done(null); + } + xhr.open("GET", "https://no-such-domain-httpbin.org"); + xhr.send(); +} + +export function test_responseType(done) { + let xhr = new XMLHttpRequest(); + xhr.responseType = ""; + xhr.responseType = "text"; + + TKUnit.assertThrows( + () => xhr.responseType = "json", + "Didn't raise on unsupported type.", + "Response type of 'json' not supported." + ); + done(null); +} diff --git a/globals/globals.ts b/globals/globals.ts index 28a960fb7..86d704e85 100644 --- a/globals/globals.ts +++ b/globals/globals.ts @@ -1,7 +1,7 @@ import types = require("utils/types"); import timer = require("timer"); import consoleModule = require("console"); -import http = require("http"); +import xhr = require("xhr/xhr"); import dialogs = require("ui/dialogs"); global.setTimeout = timer.setTimeout; @@ -14,7 +14,7 @@ if (types.isUndefined(global.NSObject)) { global.console = new consoleModule.Console(); } -global.XMLHttpRequest = (http).XMLHttpRequest; +global.XMLHttpRequest = xhr.XMLHttpRequest; global.alert = dialogs.alert; export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) { diff --git a/http/http.ts b/http/http.ts index 0e9a22ef2..c6f2f6c40 100644 --- a/http/http.ts +++ b/http/http.ts @@ -1,10 +1,6 @@ import image = require("image-source"); - -import definition = require("http"); import httpRequest = require("http/http-request"); -import types = require("utils/types"); - // merge the exports of the request file with the exports of this file declare var exports; require("utils/module-merge").merge(httpRequest, exports); @@ -27,233 +23,7 @@ export function getImage(arg: any): Promise { return new Promise((resolve, reject) => { httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg) .then(r => { - r.content.toImage().then(source => resolve(source)); - }, e => reject(e)); + r.content.toImage().then(source => resolve(source)); + }, e => reject(e)); }); -} - -export class XMLHttpRequest { - public UNSENT = 0; - public OPENED = 1; - public HEADERS_RECEIVED = 2; - public LOADING = 3; - public DONE = 4; - - public onload: () => void; - public onerror: () => void; - - private _options: definition.HttpRequestOptions; - private _readyState: number; - private _status: number; - private _response: any; - private _responseText: string = ""; - private _headers: any; - private _errorFlag: boolean; - private _responseType: string; - - public onreadystatechange: Function; - - constructor() { - this._readyState = this.UNSENT; - } - - public open(method: string, url: string, async?: boolean, user?: string, password?: string) { - if (types.isString(method) && types.isString(url)) { - this._options = { url: url, method: method }; - this._options.headers = {}; - - if (types.isString(user)) { - this._options.headers["user"] = user; - } - - if (types.isString(password)) { - this._options.headers["password"] = password; - } - - this._setReadyState(this.OPENED); - } - } - - public abort() { - this._errorFlag = true; - - this._response = null; - this._responseText = null; - this._headers = null; - this._status = null; - - if (this._readyState === this.UNSENT || this._readyState === this.OPENED || this._readyState === this.DONE) { - this._readyState = this.UNSENT; - } else { - this._setReadyState(this.DONE); - } - } - - public send(data?: string) { - this._errorFlag = false; - this._response = null; - this._responseText = null; - this._headers = null; - this._status = null; - - if (types.isDefined(this._options)) { - if (types.isString(data)) { - this._options.content = data; - } - - httpRequest.request(this._options).then(r=> { - if (!this._errorFlag) { - this._status = r.statusCode; - this._response = r.content.raw; - - this._headers = r.headers; - this._setReadyState(this.HEADERS_RECEIVED); - - this._setReadyState(this.LOADING); - - this._responseText = r.content.toString(); - this._setReadyState(this.DONE); - } - - }).catch(e => { - this._errorFlag = true; - this._setReadyState(this.DONE); - }); - } - } - - public setRequestHeader(header: string, value: string) { - if (types.isDefined(this._options) && types.isString(header) && types.isString(value)) { - this._options.headers[header] = value; - } - } - - public getAllResponseHeaders(): string { - if (this._readyState < 2 || this._errorFlag) { - return ""; - } - - var result = ""; - - for (var i in this._headers) { - // Cookie headers are excluded - if (i !== "set-cookie" && i !== "set-cookie2") { - result += i + ": " + this._headers[i] + "\r\n"; - } - } - return result.substr(0, result.length - 2); - } - - public getResponseHeader(header: string): string { - if (types.isString(header) && this._readyState > 1 - && this._headers - && this._headers[header] - && !this._errorFlag - ) { - return this._headers[header]; - } - - return null; - } - - public overrideMimeType(mime: string) { - // - } - - get readyState(): number { - return this._readyState; - } - - public get responseType(): string { - return this._responseType; - } - - public set responseType(value: string) { - if (value === "" || value === "text") { - this._responseType = value; - } else { - throw new Error(`Response type of '${value}' not supported.`); - } - } - - private _setReadyState(value: number) { - if (this._readyState !== value) { - this._readyState = value; - - if (types.isFunction(this.onreadystatechange)) { - this.onreadystatechange(); - } - } - - if (this._readyState === this.DONE) { - if (this._errorFlag && types.isFunction(this.onerror)) { - this.onerror(); - } - if (!this._errorFlag && types.isFunction(this.onload)) { - this.onload(); - } - } - } - - get responseText(): string { - return this._responseText; - } - - get response(): any { - return this._response; - } - - get status(): number { - return this._status; - } - - get statusText(): string { - if (this._readyState === this.UNSENT || this._readyState === this.OPENED || this._errorFlag) { - return ""; - } - return this._status + " " + statuses[this._status]; - } -} - -var statuses = { - 100: "Continue", - 101: "Switching Protocols", - 200: "OK", - 201: "Created", - 202: "Accepted", - 203: "Non - Authoritative Information", - 204: "No Content", - 205: "Reset Content", - 206: "Partial Content", - 300: "Multiple Choices", - 301: "Moved Permanently", - 302: "Found", - 303: "See Other", - 304: "Not Modified", - 305: "Use Proxy", - 307: "Temporary Redirect", - 400: "Bad Request", - 401: "Unauthorized", - 402: "Payment Required", - 403: "Forbidden", - 404: "Not Found", - 405: "Method Not Allowed", - 406: "Not Acceptable", - 407: "Proxy Authentication Required", - 408: "Request Timeout", - 409: "Conflict", - 410: "Gone", - 411: "Length Required", - 412: "Precondition Failed", - 413: "Request Entity Too Large", - 414: "Request - URI Too Long", - 415: "Unsupported Media Type", - 416: "Requested Range Not Satisfiable", - 417: "Expectation Failed", - 500: "Internal Server Error", - 501: "Not Implemented", - 502: "Bad Gateway", - 503: "Service Unavailable", - 504: "Gateway Timeout", - 505: "HTTP Version Not Supported" -}; +} \ No newline at end of file diff --git a/xhr/Readme.md b/xhr/Readme.md new file mode 100644 index 000000000..b613b64a2 --- /dev/null +++ b/xhr/Readme.md @@ -0,0 +1 @@ +XMLHttpRequest Level 2: https://xhr.spec.whatwg.org/ \ No newline at end of file diff --git a/xhr/package.json b/xhr/package.json new file mode 100644 index 000000000..cc5d20cf3 --- /dev/null +++ b/xhr/package.json @@ -0,0 +1,2 @@ +{ "name" : "xhr", + "main" : "xhr.js" } \ No newline at end of file diff --git a/xhr/xhr.ts b/xhr/xhr.ts new file mode 100644 index 000000000..7e2c5ef05 --- /dev/null +++ b/xhr/xhr.ts @@ -0,0 +1,228 @@ +import http = require("http"); +import types = require("utils/types"); + +export class XMLHttpRequest { + public UNSENT = 0; + public OPENED = 1; + public HEADERS_RECEIVED = 2; + public LOADING = 3; + public DONE = 4; + + public onload: () => void; + public onerror: () => void; + + private _options: http.HttpRequestOptions; + private _readyState: number; + private _status: number; + private _response: any; + private _responseText: string = ""; + private _headers: any; + private _errorFlag: boolean; + private _responseType: string = ""; + + public onreadystatechange: Function; + + constructor() { + this._readyState = this.UNSENT; + } + + public open(method: string, url: string, async?: boolean, user?: string, password?: string) { + if (types.isString(method) && types.isString(url)) { + this._options = { url: url, method: method }; + this._options.headers = {}; + + if (types.isString(user)) { + this._options.headers["user"] = user; + } + + if (types.isString(password)) { + this._options.headers["password"] = password; + } + + this._setReadyState(this.OPENED); + } + } + + public abort() { + this._errorFlag = true; + + this._response = null; + this._responseText = null; + this._headers = null; + this._status = null; + + if (this._readyState === this.UNSENT || this._readyState === this.OPENED || this._readyState === this.DONE) { + this._readyState = this.UNSENT; + } else { + this._setReadyState(this.DONE); + } + } + + public send(data?: string) { + this._errorFlag = false; + this._response = null; + this._responseText = null; + this._headers = null; + this._status = null; + + if (types.isDefined(this._options)) { + if (types.isString(data)) { + this._options.content = data; + } + + http.request(this._options).then(r=> { + if (!this._errorFlag) { + this._status = r.statusCode; + this._response = r.content.raw; + + this._headers = r.headers; + this._setReadyState(this.HEADERS_RECEIVED); + + this._setReadyState(this.LOADING); + + this._responseText = r.content.toString(); + this._setReadyState(this.DONE); + } + + }).catch(e => { + this._errorFlag = true; + this._setReadyState(this.DONE); + }); + } + } + + public setRequestHeader(header: string, value: string) { + if (types.isDefined(this._options) && types.isString(header) && types.isString(value)) { + this._options.headers[header] = value; + } + } + + public getAllResponseHeaders(): string { + if (this._readyState < 2 || this._errorFlag) { + return ""; + } + + var result = ""; + + for (var i in this._headers) { + // Cookie headers are excluded + if (i !== "set-cookie" && i !== "set-cookie2") { + result += i + ": " + this._headers[i] + "\r\n"; + } + } + return result.substr(0, result.length - 2); + } + + public getResponseHeader(header: string): string { + if (types.isString(header) && this._readyState > 1 + && this._headers + && this._headers[header] + && !this._errorFlag + ) { + return this._headers[header]; + } + + return null; + } + + public overrideMimeType(mime: string) { + // + } + + get readyState(): number { + return this._readyState; + } + + public get responseType(): string { + return this._responseType; + } + + public set responseType(value: string) { + if (value === "" || value === "text") { + this._responseType = value; + } else { + throw new Error(`Response type of '${value}' not supported.`); + } + } + + private _setReadyState(value: number) { + if (this._readyState !== value) { + this._readyState = value; + + if (types.isFunction(this.onreadystatechange)) { + this.onreadystatechange(); + } + } + + if (this._readyState === this.DONE) { + if (this._errorFlag && types.isFunction(this.onerror)) { + this.onerror(); + } + if (!this._errorFlag && types.isFunction(this.onload)) { + this.onload(); + } + } + } + + get responseText(): string { + return this._responseText; + } + + get response(): any { + return this._response; + } + + get status(): number { + return this._status; + } + + get statusText(): string { + if (this._readyState === this.UNSENT || this._readyState === this.OPENED || this._errorFlag) { + return ""; + } + return this._status + " " + statuses[this._status]; + } +} + +var statuses = { + 100: "Continue", + 101: "Switching Protocols", + 200: "OK", + 201: "Created", + 202: "Accepted", + 203: "Non - Authoritative Information", + 204: "No Content", + 205: "Reset Content", + 206: "Partial Content", + 300: "Multiple Choices", + 301: "Moved Permanently", + 302: "Found", + 303: "See Other", + 304: "Not Modified", + 305: "Use Proxy", + 307: "Temporary Redirect", + 400: "Bad Request", + 401: "Unauthorized", + 402: "Payment Required", + 403: "Forbidden", + 404: "Not Found", + 405: "Method Not Allowed", + 406: "Not Acceptable", + 407: "Proxy Authentication Required", + 408: "Request Timeout", + 409: "Conflict", + 410: "Gone", + 411: "Length Required", + 412: "Precondition Failed", + 413: "Request Entity Too Large", + 414: "Request - URI Too Long", + 415: "Unsupported Media Type", + 416: "Requested Range Not Satisfiable", + 417: "Expectation Failed", + 500: "Internal Server Error", + 501: "Not Implemented", + 502: "Bad Gateway", + 503: "Service Unavailable", + 504: "Gateway Timeout", + 505: "HTTP Version Not Supported" +};