From e2f464e1d2e64563c41c9697570f6def25b42b5a Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Mon, 20 Jul 2015 14:42:29 +0300 Subject: [PATCH] FormData support added --- apps/tests/fetch-tests.ts | 14 +++++++---- apps/tests/http-tests.ts | 26 +++++++++++++++++++ apps/tests/xhr-tests.ts | 29 +++++++++++++++++++-- fetch/fetch.d.ts | 2 +- fetch/fetch.js | 4 +-- globals/globals.ts | 1 + http/http-request.android.ts | 4 +-- http/http-request.ios.ts | 4 +-- http/http.d.ts | 2 +- xhr/xhr.ts | 49 ++++++++++++++++++++++++++++++++---- 10 files changed, 115 insertions(+), 20 deletions(-) diff --git a/apps/tests/fetch-tests.ts b/apps/tests/fetch-tests.ts index 2553995db..6d71a4bf5 100644 --- a/apps/tests/fetch-tests.ts +++ b/apps/tests/fetch-tests.ts @@ -123,6 +123,7 @@ export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) = // ``` // }; +*/ export var test_fetch_formData = function (done: (err: Error, res?: string) => void) { var result; @@ -145,7 +146,7 @@ export var test_fetch_formData = function (done: (err: Error, res?: string) => v // ``` // }; -*/ + export var test_fetch_fail_invalid_url = function (done) { var completed: boolean; var isReady = function () { return completed; } @@ -231,16 +232,19 @@ export var test_fetch_headers_sent = function (done) { }; export var test_fetch_post_form_data = function (done) { + var data = new FormData(); + data.append("MyVariableOne", "ValueOne"); + data.append("MyVariableTwo", "ValueTwo"); + fetchModule.fetch("https://httpbin.org/post", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, - body: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo" + body: data }).then(r => { - // return r.formData(); Uncomment this when FormData is available! - return r.json(); + return r.formData(); }).then(function (r) { try { - TKUnit.assert(r.form["MyVariableOne"] === "ValueOne" && r.form["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Actual result is: " + r.form); + TKUnit.assert(r instanceof FormData, "Content not sent/received properly! Actual result is: " + r); done(null); } catch (err) { diff --git a/apps/tests/http-tests.ts b/apps/tests/http-tests.ts index 714558308..a3c7b49a7 100644 --- a/apps/tests/http-tests.ts +++ b/apps/tests/http-tests.ts @@ -368,6 +368,32 @@ export var test_request_contentSentAndReceivedProperly = function (done) { }); }; +export var test_request_FormDataContentSentAndReceivedProperly = function (done) { + var result; + + var data = new FormData(); + data.append("MyVariableOne", "ValueOne"); + data.append("MyVariableTwo", "ValueTwo"); + + http.request({ + url: "https://httpbin.org/post", + method: "POST", + headers: { "Content-Type": "application/x-www-form-urlencoded" }, + content: data + }).then(function (response) { + result = response.content.toJSON(); + try { + TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!"); + done(null); + } + catch (err) { + done(err); + } + }, function (e) { + done(e); + }); +}; + export var test_request_NonStringHeadersSentAndReceivedProperly = function (done) { var result; diff --git a/apps/tests/xhr-tests.ts b/apps/tests/xhr-tests.ts index 3836be4a3..f10f8075a 100644 --- a/apps/tests/xhr-tests.ts +++ b/apps/tests/xhr-tests.ts @@ -113,6 +113,30 @@ export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done) xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" })); }; +export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function (done) { + xhr = new XMLHttpRequest(); + xhr.open("POST", "https://httpbin.org/post"); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.onreadystatechange = function () { + if (xhr.readyState > 3) { + var result = JSON.parse(xhr.responseText); + try { + TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Result is: " + xhr.responseText); + done(null); + } + catch (err) { + done(err); + } + } + }; + + var data = new FormData(); + data.append("MyVariableOne", "ValueOne"); + data.append("MyVariableTwo", "ValueTwo"); + + xhr.send(data); +}; + export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) { var flag = false; @@ -173,11 +197,12 @@ export function test_responseType(done) { let xhr = new XMLHttpRequest(); xhr.responseType = ""; xhr.responseType = "text"; + xhr.responseType = "json"; TKUnit.assertThrows( - () => xhr.responseType = "json", + () => xhr.responseType = "arraybuffer", "Didn't raise on unsupported type.", - "Response type of 'json' not supported." + "Response type of 'arraybuffer' not supported." ); done(null); } diff --git a/fetch/fetch.d.ts b/fetch/fetch.d.ts index 67745571e..d67dd5ecd 100644 --- a/fetch/fetch.d.ts +++ b/fetch/fetch.d.ts @@ -54,8 +54,8 @@ declare module "fetch" { /* arrayBuffer(): Promise; blob(): Promise; - formData(): Promise; */ + formData(): Promise; json(): Promise; text(): Promise; } diff --git a/fetch/fetch.js b/fetch/fetch.js index 3b0ec8ce1..188f17d78 100644 --- a/fetch/fetch.js +++ b/fetch/fetch.js @@ -110,7 +110,7 @@ } var support = { - blob: 'FileReader' in self && 'Blob' in self && (function () { + blob: 'FileReader' in global && 'Blob' in global && (function () { try { new Blob(); return true @@ -118,7 +118,7 @@ return false } })(), - formData: 'FormData' in self + formData: 'FormData' in global } function Body() { diff --git a/globals/globals.ts b/globals/globals.ts index 86d704e85..66e682b20 100644 --- a/globals/globals.ts +++ b/globals/globals.ts @@ -15,6 +15,7 @@ if (types.isUndefined(global.NSObject)) { } global.XMLHttpRequest = xhr.XMLHttpRequest; +global.FormData = xhr.FormData; global.alert = dialogs.alert; export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) { diff --git a/http/http-request.android.ts b/http/http-request.android.ts index 732255c6c..192cb422f 100644 --- a/http/http-request.android.ts +++ b/http/http-request.android.ts @@ -73,8 +73,8 @@ function buildJavaOptions(options: http.HttpRequestOptions) { if (types.isString(options.method)) { javaOptions.method = options.method; } - if (options.content) { - javaOptions.content = options.content; + if (types.isString(options.content) || options.content instanceof FormData) { + javaOptions.content = options.content.toString(); } if (types.isNumber(options.timeout)) { javaOptions.timeout = options.timeout; diff --git a/http/http-request.ios.ts b/http/http-request.ios.ts index 73af2e074..38d2e49f6 100644 --- a/http/http-request.ios.ts +++ b/http/http-request.ios.ts @@ -32,8 +32,8 @@ export function request(options: http.HttpRequestOptions): Promisedata).toString(); } http.request(this._options).then(r=> { @@ -80,7 +88,12 @@ export class XMLHttpRequest { this._setReadyState(this.LOADING); - this._responseText = r.content.toString(); + if (this.responseType === XMLHttpRequestResponseType.empty || + this.responseType === XMLHttpRequestResponseType.text || + this.responseType === XMLHttpRequestResponseType.json) { + this._responseText = r.content.toString; + } + this._setReadyState(this.DONE); } @@ -138,7 +151,7 @@ export class XMLHttpRequest { } public set responseType(value: string) { - if (value === "" || value === "text") { + if (value === XMLHttpRequestResponseType.empty || value in XMLHttpRequestResponseType) { this._responseType = value; } else { throw new Error(`Response type of '${value}' not supported.`); @@ -165,7 +178,11 @@ export class XMLHttpRequest { } get responseText(): string { - return this._responseText; + if (types.isFunction(this._responseText)) { + return this._responseText(); + } + + return ""; } get response(): any { @@ -226,3 +243,25 @@ var statuses = { 504: "Gateway Timeout", 505: "HTTP Version Not Supported" }; + +export class FormData { + private _data: Map; + + constructor() { + this._data = new Map(); + } + + append(name: string, value: any) { + this._data.set(name, value); + } + + toString(): string { + var arr = new Array(); + + this._data.forEach(function (value, name, map) { + arr.push(`${encodeURIComponent(name) }=${encodeURIComponent(value) }`); + }); + + return arr.join("&"); + } +} \ No newline at end of file