summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/XMLHttpRequest/FormData-append.html
blob: bf6c66d0f665df8cfb30b97063cca33442900250 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!doctype html>
<meta charset="utf-8">
<title>FormData.append</title>
<link rel="help" href="https://xhr.spec.whatwg.org/#dom-formdata-append">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<form id="form" />
<script>
    function test_formdata(creator, verifier, description) {
        async_test(description).step(function() {
            var fd = creator();
            var xhr = new XMLHttpRequest();
            xhr.onload = this.step_func(function() {
                verifier(xhr.responseText);
                this.done();
            });
            xhr.open("POST", "resources/upload.py");
            xhr.send(fd);
        });
    }

    test_formdata(function() {
        var fd = new FormData();
        fd.append("name", new String("value"));
        return fd;
    }, function(data) {
        assert_equals(data, "name=value,\n");
    }, "Passing a String object to FormData.append should work.");

    test(function() {
        assert_equals(create_formdata(['key', 'value1']).get('key'), "value1");
    }, 'testFormDataAppend1');
    test(function() {
        assert_equals(create_formdata(['key', 'value2'], ['key', 'value1']).get('key'), "value2");
    }, 'testFormDataAppend2');
    test(function() {
        assert_equals(create_formdata(['key', undefined]).get('key'), "undefined");
    }, 'testFormDataAppendUndefined1');
    test(function() {
        assert_equals(create_formdata(['key', undefined], ['key', 'value1']).get('key'), "undefined");
    }, 'testFormDataAppendUndefined2');
    test(function() {
        assert_equals(create_formdata(['key', null]).get('key'), "null");
    }, 'testFormDataAppendNull1');
    test(function() {
        assert_equals(create_formdata(['key', null], ['key', 'value1']).get('key'), "null");
    }, 'testFormDataAppendNull2');
    test(function() {
        var fd = new FormData(document.getElementById("form"));
        fd.append('key', 'value1');
        assert_equals(fd.get('key'), "value1");
    }, 'testFormDataAppendToForm1');
    test(function() {
        var fd = new FormData(document.getElementById("form"));
        fd.append('key', 'value2');
        fd.append('key', 'value1');
        assert_equals(fd.get('key'), "value2");
    }, 'testFormDataAppendToForm2');
    test(function() {
        var fd = new FormData(document.getElementById("form"));
        fd.append('key', undefined);
        assert_equals(fd.get('key'), "undefined");
    }, 'testFormDataAppendToFormUndefined1');
    test(function() {
        var fd = new FormData(document.getElementById("form"));
        fd.append('key', undefined);
        fd.append('key', 'value1');
        assert_equals(fd.get('key'), "undefined");
    }, 'testFormDataAppendToFormUndefined2');
    test(function() {
        var fd = new FormData(document.getElementById("form"));
        fd.append('key', null);
        assert_equals(fd.get('key'), "null");
    }, 'testFormDataAppendToFormNull1');
    test(function() {
        var fd = new FormData(document.getElementById("form"));
        fd.append('key', null);
        fd.append('key', 'value1');
        assert_equals(fd.get('key'), "null");
    }, 'testFormDataAppendToFormNull2');
    test(function() {
        var before = new Date(new Date().getTime() - 2000); // two seconds ago, in case there's clock drift
        var fd = create_formdata(['key', new Blob(), 'blank.txt']).get('key');
        assert_equals(fd.name, "blank.txt");
        assert_equals(fd.type, "");
        assert_equals(fd.size, 0);
        assert_greater_than_equal(fd.lastModified, before);
        assert_less_than_equal(fd.lastModified, new Date());
    }, 'testFormDataAppendEmptyBlob');

    function create_formdata() {
        var fd = new FormData();
        for (var i = 0; i < arguments.length; i++) {
            fd.append.apply(fd, arguments[i]);
        };
        return fd;
    }
</script>