summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/response/response-cancel-stream.any.js
blob: baa46de4039b176c30e0e8a49ee75acbd20e6450 (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
// META: global=window,worker
// META: title=Response consume blob and http bodies
// META: script=../resources/utils.js

promise_test(function(test) {
    return new Response(new Blob([], { "type" : "text/plain" })).body.cancel();
}, "Cancelling a starting blob Response stream");

promise_test(function(test) {
    var response = new Response(new Blob(["This is data"], { "type" : "text/plain" }));
    var reader = response.body.getReader();
    reader.read();
    return reader.cancel();
}, "Cancelling a loading blob Response stream");

promise_test(function(test) {
    var response = new Response(new Blob(["T"], { "type" : "text/plain" }));
    var reader = response.body.getReader();

    var closedPromise = reader.closed.then(function() {
        return reader.cancel();
    });
    reader.read().then(function readMore({done, value}) {
      if (!done) return reader.read().then(readMore);
    });
    return closedPromise;
}, "Cancelling a closed blob Response stream");

promise_test(function(test) {
    return fetch(RESOURCES_DIR + "trickle.py?ms=30&count=100").then(function(response) {
        return response.body.cancel();
    });
}, "Cancelling a starting Response stream");

promise_test(function() {
    return fetch(RESOURCES_DIR + "trickle.py?ms=30&count=100").then(function(response) {
        var reader = response.body.getReader();
        return reader.read().then(function() {
            return reader.cancel();
        });
    });
}, "Cancelling a loading Response stream");

promise_test(function() {
    async function readAll(reader) {
      while (true) {
        const {value, done} = await reader.read();
        if (done)
          return;
      }
    }

    return fetch(RESOURCES_DIR + "top.txt").then(function(response) {
        var reader = response.body.getReader();
        return readAll(reader).then(() => reader.cancel());
    });
}, "Cancelling a closed Response stream");