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);
}