summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/fetch/api/response/response-body-read-task-handling.html
blob: 64b0755666168d7cac9808fe1593a01889a19bff (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
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
</head>
<body>
  <script>
function performMicrotaskCheckpoint() {
    document.createNodeIterator(document, -1, {
        acceptNode() {
            return NodeFilter.FILTER_ACCEPT;
        }
    }).nextNode();
}

promise_test(function() {
    return fetch("../resources/data.json").then(function(response) {
        // Add a getter for "then" that will incidentally be invoked
        // during promise resolution.
        Object.prototype.__defineGetter__('then', () => {
            // Clean up behind ourselves.
            delete Object.prototype.then;

            // This promise should (like all promises) be resolved
            // asynchronously.
            var executed = false;
            Promise.resolve().then(_ => { executed = true; });

            // This shouldn't run microtasks!  They should only run
            // after the fetch is resolved.
            performMicrotaskCheckpoint();

            // The fulfill handler above shouldn't have run yet.  If it has run,
            // throw to reject this promise and fail the test.
            assert_false(executed, "shouldn't have run microtasks yet");

            // Otherwise act as if there's no "then" property so the promise
            // fulfills and the test passes.
            return undefined;
        });

        // Create a read request, incidentally resolving a promise with an
        // object value, thereby invoking the getter installed above.
        return response.body.getReader().read();
    });
}, "reading from a body stream should occur in a microtask scope");

promise_test(function() {
    return fetch("../resources/data.json").then(function(response) {
        // Add a getter for "then" that will incidentally be invoked
        // during promise resolution.
        Object.prototype.__defineGetter__('then', () => {
            // Clean up behind ourselves.
            delete Object.prototype.then;

            // This promise should (like all promises) be resolved
            // asynchronously.
            var executed = false;
            Promise.resolve().then(_ => { executed = true; });

            // This shouldn't run microtasks!  They should only run
            // after the fetch is resolved.
            performMicrotaskCheckpoint();

            // The fulfill handler above shouldn't have run yet.  If it has run,
            // throw to reject this promise and fail the test.
            assert_false(executed, "shouldn't have run microtasks yet");

            // Otherwise act as if there's no "then" property so the promise
            // fulfills and the test passes.
            return undefined;
        });

        // Create a read request, incidentally resolving a promise with an
        // object value, thereby invoking the getter installed above.
        return response.body.pipeTo(new WritableStream({
            write(chunk) {}
        }))
    });
}, "piping from a body stream to a JS-written WritableStream should occur in a microtask scope");
  </script>
</body>
</html>