summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/xhr/formdata/iteration.any.js
blob: 1633fd9c4daa792f7050723220b7fcaa38b20858 (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
// META: title=FormData: changes to entry list during iteration

// These are tests for next()'s behavior as specified in
// https://webidl.spec.whatwg.org/#es-iterator-prototype-object

"use strict";

function createFormData(input) {
    const formData = new FormData();

    for (const [name, value] of input) {
        formData.append(name, value);
    }

    return formData;
}

test(() => {
    const formData = createFormData([["foo", "0"],
                                     ["baz", "1"],
                                     ["BAR", "2"]]);
    const actualKeys = [];
    const actualValues = [];
    for (const [name, value] of formData) {
        actualKeys.push(name);
        actualValues.push(value);
        formData.delete("baz");
    }
    assert_array_equals(actualKeys, ["foo", "BAR"]);
    assert_array_equals(actualValues, ["0", "2"]);
}, "Iteration skips elements removed while iterating");

test(() => {
    const formData = createFormData([["foo", "0"],
                                     ["baz", "1"],
                                     ["BAR", "2"],
                                     ["quux", "3"]]);
    const actualKeys = [];
    const actualValues = [];
    for (const [name, value] of formData) {
        actualKeys.push(name);
        actualValues.push(value);
        if (name === "baz")
            formData.delete("foo");
    }
    assert_array_equals(actualKeys, ["foo", "baz", "quux"]);
    assert_array_equals(actualValues, ["0", "1", "3"]);
}, "Removing elements already iterated over causes an element to be skipped during iteration");

test(() => {
    const formData = createFormData([["foo", "0"],
                                     ["baz", "1"],
                                     ["BAR", "2"],
                                     ["quux", "3"]]);
    const actualKeys = [];
    const actualValues = [];
    for (const [name, value] of formData) {
        actualKeys.push(name);
        actualValues.push(value);
        if (name === "baz")
            formData.append("X-yZ", "4");
    }
    assert_array_equals(actualKeys, ["foo", "baz", "BAR", "quux", "X-yZ"]);
    assert_array_equals(actualValues, ["0", "1", "2", "3", "4"]);
}, "Appending a value pair during iteration causes it to be reached during iteration");