FormData support added

This commit is contained in:
Vladimir Enchev
2015-07-20 14:42:29 +03:00
parent 54c0de8df6
commit e2f464e1d2
10 changed files with 115 additions and 20 deletions

View File

@ -123,6 +123,7 @@ export var test_fetch_arrayBuffer = function (done: (err: Error, res?: string) =
// ```
// </snippet>
};
*/
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
// ```
// </snippet>
};
*/
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) {

View File

@ -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;

View File

@ -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(<any>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);
}

2
fetch/fetch.d.ts vendored
View File

@ -54,8 +54,8 @@ declare module "fetch" {
/*
arrayBuffer(): Promise<ArrayBuffer>;
blob(): Promise<Blob>;
formData(): Promise<FormData>;
*/
formData(): Promise<FormData>;
json(): Promise<any>;
text(): Promise<string>;
}

View File

@ -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() {

View File

@ -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) {

View File

@ -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;

View File

@ -32,8 +32,8 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
}
}
if (types.isString(options.content)) {
urlRequest.HTTPBody = NSString.alloc().initWithString(options.content).dataUsingEncoding(4);
if (types.isString(options.content) || options.content instanceof FormData) {
urlRequest.HTTPBody = NSString.alloc().initWithString(options.content.toString()).dataUsingEncoding(4);
}
if (types.isNumber(options.timeout)) {

2
http/http.d.ts vendored
View File

@ -69,7 +69,7 @@ declare module "http" {
/**
* Gets or sets the request body.
*/
content?: string;
content?: string | FormData;
/**
* Gets or sets the request timeout in milliseconds.

View File

@ -1,6 +1,12 @@
import http = require("http");
import types = require("utils/types");
module XMLHttpRequestResponseType {
export var empty = "";
export var text = "text";
export var json = "json";
}
export class XMLHttpRequest {
public UNSENT = 0;
public OPENED = 1;
@ -15,7 +21,7 @@ export class XMLHttpRequest {
private _readyState: number;
private _status: number;
private _response: any;
private _responseText: string = "";
private _responseText: Function;
private _headers: any;
private _errorFlag: boolean;
private _responseType: string = "";
@ -58,7 +64,7 @@ export class XMLHttpRequest {
}
}
public send(data?: string) {
public send(data?: any) {
this._errorFlag = false;
this._response = null;
this._responseText = null;
@ -68,6 +74,8 @@ export class XMLHttpRequest {
if (types.isDefined(this._options)) {
if (types.isString(data)) {
this._options.content = data;
} else if (data instanceof FormData) {
this._options.content = (<FormData>data).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<string, any>;
constructor() {
this._data = new Map<string, any>();
}
append(name: string, value: any) {
this._data.set(name, value);
}
toString(): string {
var arr = new Array<string>();
this._data.forEach(function (value, name, map) {
arr.push(`${encodeURIComponent(name) }=${encodeURIComponent(value) }`);
});
return arr.join("&");
}
}