gecko/dom/html/test/test_formData.html
2015-02-21 11:54:44 -08:00

182 lines
6.4 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=690659
-->
<head>
<title>Test for Bug 690659</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=690659">Mozilla Bug 690659</a>
<script type="text/javascript">
SimpleTest.waitForExplicitFinish();
function testHas() {
var f = new FormData();
f.append("foo", "bar");
f.append("another", "value");
ok(f.has("foo"), "has() on existing name should be true.");
ok(f.has("another"), "has() on existing name should be true.");
ok(!f.has("nonexistent"), "has() on non-existent name should be false.");
}
function testGet() {
var f = new FormData();
f.append("foo", "bar");
f.append("foo", "bar2");
f.append("blob", new Blob(["hey"], { type: 'text/plain' }));
f.append("file", new File(["hey"], 'testname', {type: 'text/plain'}));
is(f.get("foo"), "bar", "get() on existing name should return first value");
ok(f.get("blob") instanceof Blob, "get() on existing name should return first value");
is(f.get("blob").type, 'text/plain', "get() on existing name should return first value");
ok(f.get("file") instanceof File, "get() on existing name should return first value");
is(f.get("file").name, 'testname', "get() on existing name should return first value");
is(f.get("nonexistent"), null, "get() on non-existent name should return null.");
}
function testGetAll() {
var f = new FormData();
f.append("other", "value");
f.append("foo", "bar");
f.append("foo", "bar2");
f.append("foo", new Blob(["hey"], { type: 'text/plain' }));
var arr = f.getAll("foo");
is(arr.length, 3, "getAll() should retrieve all matching entries.");
is(arr[0], "bar", "values should match and be in order");
is(arr[1], "bar2", "values should match and be in order");
ok(arr[2] instanceof Blob, "values should match and be in order");
is(f.get("nonexistent"), null, "get() on non-existent name should return null.");
}
function testDelete() {
var f = new FormData();
f.append("other", "value");
f.append("foo", "bar");
f.append("foo", "bar2");
f.append("foo", new Blob(["hey"], { type: 'text/plain' }));
ok(f.has("foo"), "has() on existing name should be true.");
f.delete("foo");
ok(!f.has("foo"), "has() on deleted name should be false.");
is(f.getAll("foo").length, 0, "all entries should be deleted.");
is(f.getAll("other").length, 1, "other names should still be there.");
f.delete("other");
is(f.getAll("other").length, 0, "all entries should be deleted.");
}
function testSet() {
var f = new FormData();
f.set("other", "value");
ok(f.has("other"), "set() on new name should be similar to append()");
is(f.getAll("other").length, 1, "set() on new name should be similar to append()");
f.append("other", "value2");
is(f.getAll("other").length, 2, "append() should not replace existing entries.");
f.append("foo", "bar");
f.append("other", "value3");
f.append("other", "value3");
f.append("other", "value3");
is(f.getAll("other").length, 5, "append() should not replace existing entries.");
f.set("other", "value4");
is(f.getAll("other").length, 1, "set() should replace existing entries.");
is(f.getAll("other")[0], "value4", "set() should replace existing entries.");
}
function testIterate() {
todo(false, "Implement this in Bug 1085284.");
}
function testFilename() {
var f = new FormData();
// Spec says if a Blob (which is not a File) is added, the name parameter is effectively ignored, always set to "blob".
f.append("blob", new Blob(["hi"]), "blob1.txt");
is(f.get("blob").name, "blob", "Blob's filename should be blob irrespective of what was passed.");
// Spec says if a File is added, the name parameter is respected if passed.
f.append("file1", new File(["hi"], "file1.txt"));
is(f.get("file1").name, "file1.txt", "File's filename should be original's name if no filename is explicitly passed.");
f.append("file2", new File(["hi"], "file2.txt"), "fakename.txt");
is(f.get("file2").name, "fakename.txt", "File's filename should be explicitly passed name.");
f.append("file3", new File(["hi"], ""));
is(f.get("file3").name, "", "File's filename is returned even if empty.");
}
function testSend() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "form_submit_server.sjs");
xhr.onload = function () {
var response = xhr.response;
for (var entry of response) {
is(entry.body, 'hey');
is(entry.headers['Content-Type'], 'text/plain');
}
is(response[0].headers['Content-Disposition'],
'form-data; name="empty"; filename="blob"');
is(response[1].headers['Content-Disposition'],
'form-data; name="explicit"; filename="blob"');
is(response[2].headers['Content-Disposition'],
'form-data; name="explicit-empty"; filename="blob"');
is(response[3].headers['Content-Disposition'],
'form-data; name="file-name"; filename="testname"');
is(response[4].headers['Content-Disposition'],
'form-data; name="empty-file-name"; filename="blob"');
is(response[5].headers['Content-Disposition'],
'form-data; name="file-name-overwrite"; filename="overwrite"');
SimpleTest.finish();
}
var file, blob = new Blob(['hey'], {type: 'text/plain'});
var fd = new FormData();
fd.append("empty", blob);
fd.append("explicit", blob, "explicit-file-name-will-be-ignored");
fd.append("explicit-empty", blob, "");
file = new File([blob], 'testname', {type: 'text/plain'});
fd.append("file-name", file);
// XXXnsm Empty filename will get converted to "blob" when sending to
// server, which I guess is acceptable since no spec seems to define
// anything. Blink does the same.
file = new File([blob], '', {type: 'text/plain'});
fd.append("empty-file-name", file);
file = new File([blob], 'testname', {type: 'text/plain'});
fd.append("file-name-overwrite", file, "overwrite");
xhr.responseType = 'json';
xhr.send(fd);
}
function runTest() {
testHas();
testGet();
testGetAll();
testDelete();
testSet();
testIterate();
testFilename();
// Finally, send an XHR and verify the response matches.
testSend();
}
runTest()
</script>
</body>
</html>