Skip to content

Commit e2f464e

Browse files
author
Vladimir Enchev
committed
FormData support added
1 parent 54c0de8 commit e2f464e

File tree

10 files changed

+115
-20
lines changed

10 files changed

+115
-20
lines changed

apps/tests/fetch-tests.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) =
123123
// ```
124124
// </snippet>
125125
};
126+
*/
126127

127128
export var test_fetch_formData = function (done: (err: Error, res?: string) => void) {
128129
var result;
@@ -145,7 +146,7 @@ export var test_fetch_formData = function (done: (err: Error, res?: string) => v
145146
// ```
146147
// </snippet>
147148
};
148-
*/
149+
149150
export var test_fetch_fail_invalid_url = function (done) {
150151
var completed: boolean;
151152
var isReady = function () { return completed; }
@@ -231,16 +232,19 @@ export var test_fetch_headers_sent = function (done) {
231232
};
232233

233234
export var test_fetch_post_form_data = function (done) {
235+
var data = new FormData();
236+
data.append("MyVariableOne", "ValueOne");
237+
data.append("MyVariableTwo", "ValueTwo");
238+
234239
fetchModule.fetch("https://httpbin.org/post", {
235240
method: "POST",
236241
headers: { "Content-Type": "application/x-www-form-urlencoded" },
237-
body: "MyVariableOne=ValueOne&MyVariableTwo=ValueTwo"
242+
body: data
238243
}).then(r => {
239-
// return r.formData(); Uncomment this when FormData is available!
240-
return r.json();
244+
return r.formData();
241245
}).then(function (r) {
242246
try {
243-
TKUnit.assert(r.form["MyVariableOne"] === "ValueOne" && r.form["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Actual result is: " + r.form);
247+
TKUnit.assert(r instanceof FormData, "Content not sent/received properly! Actual result is: " + r);
244248
done(null);
245249
}
246250
catch (err) {

apps/tests/http-tests.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,32 @@ export var test_request_contentSentAndReceivedProperly = function (done) {
368368
});
369369
};
370370

371+
export var test_request_FormDataContentSentAndReceivedProperly = function (done) {
372+
var result;
373+
374+
var data = new FormData();
375+
data.append("MyVariableOne", "ValueOne");
376+
data.append("MyVariableTwo", "ValueTwo");
377+
378+
http.request({
379+
url: "https://httpbin.org/post",
380+
method: "POST",
381+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
382+
content: data
383+
}).then(function (response) {
384+
result = response.content.toJSON();
385+
try {
386+
TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly!");
387+
done(null);
388+
}
389+
catch (err) {
390+
done(err);
391+
}
392+
}, function (e) {
393+
done(e);
394+
});
395+
};
396+
371397
export var test_request_NonStringHeadersSentAndReceivedProperly = function (done) {
372398
var result;
373399

apps/tests/xhr-tests.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ export var test_XMLHttpRequest_contentSentAndReceivedProperly = function (done)
113113
xhr.send(JSON.stringify({ MyVariableOne: "ValueOne", MyVariableTwo: "ValueTwo" }));
114114
};
115115

116+
export var test_XMLHttpRequest_FormDataContentSentAndReceivedProperly = function (done) {
117+
xhr = new XMLHttpRequest();
118+
xhr.open("POST", "https://httpbin.org/post");
119+
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
120+
xhr.onreadystatechange = function () {
121+
if (xhr.readyState > 3) {
122+
var result = JSON.parse(xhr.responseText);
123+
try {
124+
TKUnit.assert(result["form"]["MyVariableOne"] === "ValueOne" && result["form"]["MyVariableTwo"] === "ValueTwo", "Content not sent/received properly! Result is: " + xhr.responseText);
125+
done(null);
126+
}
127+
catch (err) {
128+
done(err);
129+
}
130+
}
131+
};
132+
133+
var data = new FormData();
134+
data.append("MyVariableOne", "ValueOne");
135+
data.append("MyVariableTwo", "ValueTwo");
136+
137+
xhr.send(<any>data);
138+
};
139+
116140
export var test_XMLHttpRequest_abortShouldCancelonreadystatechange = function (done) {
117141
var flag = false;
118142

@@ -173,11 +197,12 @@ export function test_responseType(done) {
173197
let xhr = new XMLHttpRequest();
174198
xhr.responseType = "";
175199
xhr.responseType = "text";
200+
xhr.responseType = "json";
176201

177202
TKUnit.assertThrows(
178-
() => xhr.responseType = "json",
203+
() => xhr.responseType = "arraybuffer",
179204
"Didn't raise on unsupported type.",
180-
"Response type of 'json' not supported."
205+
"Response type of 'arraybuffer' not supported."
181206
);
182207
done(null);
183208
}

fetch/fetch.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ declare module "fetch" {
5454
/*
5555
arrayBuffer(): Promise<ArrayBuffer>;
5656
blob(): Promise<Blob>;
57-
formData(): Promise<FormData>;
5857
*/
58+
formData(): Promise<FormData>;
5959
json(): Promise<any>;
6060
text(): Promise<string>;
6161
}

fetch/fetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@
110110
}
111111

112112
var support = {
113-
blob: 'FileReader' in self && 'Blob' in self && (function () {
113+
blob: 'FileReader' in global && 'Blob' in global && (function () {
114114
try {
115115
new Blob();
116116
return true
117117
} catch (e) {
118118
return false
119119
}
120120
})(),
121-
formData: 'FormData' in self
121+
formData: 'FormData' in global
122122
}
123123

124124
function Body() {

globals/globals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if (types.isUndefined(global.NSObject)) {
1515
}
1616

1717
global.XMLHttpRequest = xhr.XMLHttpRequest;
18+
global.FormData = xhr.FormData;
1819
global.alert = dialogs.alert;
1920

2021
export function Deprecated(target: Object, key?: string | symbol, descriptor?: any) {

http/http-request.android.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
7373
if (types.isString(options.method)) {
7474
javaOptions.method = options.method;
7575
}
76-
if (options.content) {
77-
javaOptions.content = options.content;
76+
if (types.isString(options.content) || options.content instanceof FormData) {
77+
javaOptions.content = options.content.toString();
7878
}
7979
if (types.isNumber(options.timeout)) {
8080
javaOptions.timeout = options.timeout;

http/http-request.ios.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
3232
}
3333
}
3434

35-
if (types.isString(options.content)) {
36-
urlRequest.HTTPBody = NSString.alloc().initWithString(options.content).dataUsingEncoding(4);
35+
if (types.isString(options.content) || options.content instanceof FormData) {
36+
urlRequest.HTTPBody = NSString.alloc().initWithString(options.content.toString()).dataUsingEncoding(4);
3737
}
3838

3939
if (types.isNumber(options.timeout)) {

http/http.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ declare module "http" {
6969
/**
7070
* Gets or sets the request body.
7171
*/
72-
content?: string;
72+
content?: string | FormData;
7373

7474
/**
7575
* Gets or sets the request timeout in milliseconds.

xhr/xhr.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import http = require("http");
22
import types = require("utils/types");
33

4+
module XMLHttpRequestResponseType {
5+
export var empty = "";
6+
export var text = "text";
7+
export var json = "json";
8+
}
9+
410
export class XMLHttpRequest {
511
public UNSENT = 0;
612
public OPENED = 1;
@@ -15,7 +21,7 @@ export class XMLHttpRequest {
1521
private _readyState: number;
1622
private _status: number;
1723
private _response: any;
18-
private _responseText: string = "";
24+
private _responseText: Function;
1925
private _headers: any;
2026
private _errorFlag: boolean;
2127
private _responseType: string = "";
@@ -58,7 +64,7 @@ export class XMLHttpRequest {
5864
}
5965
}
6066

61-
public send(data?: string) {
67+
public send(data?: any) {
6268
this._errorFlag = false;
6369
this._response = null;
6470
this._responseText = null;
@@ -68,6 +74,8 @@ export class XMLHttpRequest {
6874
if (types.isDefined(this._options)) {
6975
if (types.isString(data)) {
7076
this._options.content = data;
77+
} else if (data instanceof FormData) {
78+
this._options.content = (<FormData>data).toString();
7179
}
7280

7381
http.request(this._options).then(r=> {
@@ -80,7 +88,12 @@ export class XMLHttpRequest {
8088

8189
this._setReadyState(this.LOADING);
8290

83-
this._responseText = r.content.toString();
91+
if (this.responseType === XMLHttpRequestResponseType.empty ||
92+
this.responseType === XMLHttpRequestResponseType.text ||
93+
this.responseType === XMLHttpRequestResponseType.json) {
94+
this._responseText = r.content.toString;
95+
}
96+
8497
this._setReadyState(this.DONE);
8598
}
8699

@@ -138,7 +151,7 @@ export class XMLHttpRequest {
138151
}
139152

140153
public set responseType(value: string) {
141-
if (value === "" || value === "text") {
154+
if (value === XMLHttpRequestResponseType.empty || value in XMLHttpRequestResponseType) {
142155
this._responseType = value;
143156
} else {
144157
throw new Error(`Response type of '${value}' not supported.`);
@@ -165,7 +178,11 @@ export class XMLHttpRequest {
165178
}
166179

167180
get responseText(): string {
168-
return this._responseText;
181+
if (types.isFunction(this._responseText)) {
182+
return this._responseText();
183+
}
184+
185+
return "";
169186
}
170187

171188
get response(): any {
@@ -226,3 +243,25 @@ var statuses = {
226243
504: "Gateway Timeout",
227244
505: "HTTP Version Not Supported"
228245
};
246+
247+
export class FormData {
248+
private _data: Map<string, any>;
249+
250+
constructor() {
251+
this._data = new Map<string, any>();
252+
}
253+
254+
append(name: string, value: any) {
255+
this._data.set(name, value);
256+
}
257+
258+
toString(): string {
259+
var arr = new Array<string>();
260+
261+
this._data.forEach(function (value, name, map) {
262+
arr.push(`${encodeURIComponent(name) }=${encodeURIComponent(value) }`);
263+
});
264+
265+
return arr.join("&");
266+
}
267+
}

0 commit comments

Comments
 (0)