mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
FormData support added
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user