Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Unified Diff: LayoutTests/http/tests/local/formdata/formdata-methods.html

Issue 564963002: New FormData methods: get, getAll, has, set, delete and iterable (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Implement getAll(), fix get() with no matches Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | Source/core/html/DOMFormData.h » ('j') | Source/core/html/DOMFormData.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: LayoutTests/http/tests/local/formdata/formdata-methods.html
diff --git a/LayoutTests/http/tests/local/formdata/formdata-methods.html b/LayoutTests/http/tests/local/formdata/formdata-methods.html
new file mode 100644
index 0000000000000000000000000000000000000000..e6b3985d31db592b8192ac4d32c5078ee4498427
--- /dev/null
+++ b/LayoutTests/http/tests/local/formdata/formdata-methods.html
@@ -0,0 +1,202 @@
+<!DOCTYPE html>
+<title>FormData interface</title>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script>
+
+function serializeFormData(formData)
+{
+ return new Promise(function(resolve, reject) {
+ var xhr = new XMLHttpRequest(), async = true;
+ xhr.open('POST', 'http://127.0.0.1:8000/xmlhttprequest/resources/multipart-post-echo.php', async);
+ xhr.send(formData);
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState !== XMLHttpRequest.DONE)
+ return;
+ if (xhr.status === 200) {
+ resolve(xhr.responseText);
+ } else {
+ reject(Error(xhr.statusText));
+ }
+ };
+ });
+}
+
+function checkFormData(test, formData, expected, message)
+{
+ message = message || 'Serialized FormData should match';
+ return serializeFormData(formData)
+ .then(test.step_func(function(value) {
+ assert_equals(value, expected, message);
+ test.done();
+ }))
+ .catch(test.unreached_func('Failed to fetch form data'));
+}
+
+function blobAsString(blob)
+{
+ return new Promise(function(resolve, reject) {
+ var reader = new FileReader();
+ reader.readAsText(blob);
+ reader.onload = function(e) { resolve(reader.result); };
+ reader.onerror = function(e) { reject(reader.error); };
+ });
+}
+
+function checkBlobData(test, blob, expected, message)
+{
+ message = message || 'Blob daata should match';
tyoshino (SeeGerritForStatus) 2014/09/12 07:52:54 daata -> data
jsbell 2014/09/12 16:30:09 Done.
+ return blobAsString(blob)
+ .then(test.step_func(function(value) {
+ assert_equals(value, expected, message);
+ test.done();
+ }))
+ .catch(test.unreached_func('Failed to read Blob data'));
+}
+
+async_test(function(t) {
+ var fd = new FormData();
+ fd.append('n1', 'value');
+ assert_equals(fd.has('n1'), true, 'FormData.has() should see appended entry');
+ assert_equals(fd.has('n2'), false, 'FormData.has() should not see absent entry');
+ fd.append('n2', 'value');
+ assert_equals(fd.has('n1'), true, 'FormData.has() should still see original entry');
+ assert_equals(fd.has('n2'), true, 'FormData.has() should see newly appended entry');
+ fd.append('n3', new Blob(['content']));
+ assert_equals(fd.has('n3'), true, 'FormData.has() should see newly appended Blob entry');
+
+ checkFormData(t, fd, 'n1=value&n2=value&n3=blob:application/octet-stream:content');
+}, 'FormData.has()');
+
+async_test(function(t) {
+ var fd = new FormData();
+ fd.append('name', 'value');
+ assert_equals(fd.has('name'), true, 'FormData.has() should see appended entry');
+ fd.delete('name');
+ assert_equals(fd.has('name'), false, 'FormData.has() should not see deleted entry');
+
+ fd.append('name', new Blob(['content']));
+ assert_equals(fd.has('name'), true, 'FormData.has() should see newly appended Blob entry');
+ fd.delete('name');
+ assert_equals(fd.has('name'), false, 'FormData.has() should not see deleted Blob entry');
+
+ fd.append('n1', 'v1');
+ fd.append('n2', 'v2');
+ fd.append('n1', 'v3');
+ fd.delete('n1');
+ assert_equals(fd.has('n1'), false, 'FormData.delete() should delete all matching entries');
+
+ checkFormData(t, fd, 'n2=v2');
+}, 'FormData.delete()');
+
+async_test(function(t) {
+ var fd = new FormData();
+ fd.set('n1', 'v1');
+ assert_equals(fd.has('n1'), true, 'FormData.has() should see set() entry');
+ checkFormData(t, fd, 'n1=v1');
+}, 'FormData.set() - DOMString');
+
+async_test(function(t) {
+ var fd = new FormData();
+ fd.set('n1', new Blob(['content']));
+
+ checkFormData(t, fd, 'n1=blob:application/octet-stream:content');
+}, 'FormData.set() - Blob');
+
+async_test(function(t) {
+ var fd = new FormData();
+ fd.set('n1', new Blob(['content']), 'name1');
+
+ checkFormData(t, fd, 'n1=name1:application/octet-stream:content');
+}, 'FormData.set() - Blob and filename');
+
+async_test(function(t) {
+ var fd = new FormData();
+ fd.append('n1', 'v1');
+ fd.append('n1', 'v2');
+ fd.set('n1', 'v3');
+ assert_equals(fd.has('n1'), true, 'FormData.has() should see set() entry');
+ fd.set('n2', 'v4');
+ fd.set('n2', new Blob(['content']), 'name1');
+
+ checkFormData(t, fd, 'n1=v3&n2=name1:application/octet-stream:content',
+ 'FormData.set() should replace all matching entries');
+}, 'FormData.set() replaces all entries');
+
+test(function() {
+ var fd = new FormData();
+ fd.append('n1', 'v1');
+ var result = fd.get('n1');
+ assert_equals(result, 'v1', 'FormData.get() should return matching DOMString entry');
+}, 'FormData.get() - DOMString');
+
+async_test(function(t) {
+ var fd = new FormData();
+ fd.append('n1', new Blob(['content']));
+ var result = fd.get('n1');
+ assert_true(result instanceof File, 'FormData.get() on an appended Blob should return a File');
+ assert_equals(result.name, 'blob', 'Default name on a Blob is "blob"');
+ checkBlobData(t, result, 'content');
+}, 'FormData.get() - Blob');
+
+async_test(function(t) {
+ var fd = new FormData();
+ fd.append('n1', new Blob(['content']), 'name1');
+ var result = fd.get('n1');
+ assert_true(result instanceof File, 'FormData.get() on an appended Blob should return a File');
+ assert_equals(result.name, 'name1', 'Returned File should have specified name');
+ checkBlobData(t, result, 'content');
+}, 'FormData.get() - Blob and filename');
+
+test(function() {
+ var fd = new FormData();
+ fd.append('n1', 'v1');
+ fd.append('n1', 'v2');
+ var result = fd.get('n1');
+ assert_equals(result, 'v1', 'FormData.get() should return first matching entry');
+}, 'FormData.get() - multiple matches');
+
+test(function() {
+ var fd = new FormData();
+ fd.append('n1', 'v1');
+ fd.append('n2', 'v2');
+ var result = fd.get('n3');
+ assert_equals(result, null, 'FormData.get() should return null if no matches');
+}, 'FormData.get() - no matches');
+
+test(function() {
+ var fd = new FormData();
+ fd.append('n1', 'v1');
+ fd.append('n2', 'v2');
+ fd.append('n1', new Blob(['content']));
+ fd.append('n3', 'v3');
+ fd.append('n1', new Blob(['content2']), 'name2');
+ fd.append('n4', 'v4');
+ var results = fd.getAll('n1');
+
+ assert_equals(results.length, 3, 'FormData.getAll() should return all matching entries');
+
+ assert_equals(results[0], 'v1', 'First entry should match');
+
+ assert_true(results[1] instanceof File, 'Second entry should be a File');
+ assert_equals(results[1].name, 'blob', 'Second entry name should be the default');
+
+ assert_true(results[2] instanceof File, 'Third entry should be a File');
+ assert_equals(results[2].name, 'name2', 'Third entry name should be as specified');
+}, 'FormData.getAll()');
+
+test(function() {
+ var fd = new FormData();
+ fd.append('n1', 'v1');
+ fd.append('n2', 'v2');
+ var results = fd.getAll('n3');
+
+ assert_equals(results.length, 0, 'FormData.getAll() should return empty sequence if no matches');
+}, 'FormData.getAll() - no matches');
+
+// FIXME: Add test for get() for appended File without filename
+// FIXME: Add test for get() for appended File with filename
+// FIXME: Add test for getAll() for appended File without filename
+// FIXME: Add test for getAll() for appended File with filename
+
+</script>
« no previous file with comments | « no previous file | Source/core/html/DOMFormData.h » ('j') | Source/core/html/DOMFormData.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698