File: response-consume-empty.html

package info (click to toggle)
firefox-esr 78.15.0esr-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,301,156 kB
  • sloc: cpp: 5,665,905; javascript: 4,798,386; ansic: 2,878,233; python: 977,004; asm: 270,347; xml: 181,456; java: 111,756; sh: 72,926; makefile: 21,819; perl: 13,380; cs: 4,725; yacc: 4,565; objc: 3,026; pascal: 1,787; lex: 1,720; ada: 1,681; exp: 505; php: 436; lisp: 260; awk: 152; ruby: 103; csh: 80; sed: 53; sql: 45
file content (112 lines) | stat: -rw-r--r-- 4,720 bytes parent folder | download | duplicates (4)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Response consume empty bodies</title>
    <meta name="help" href="https://fetch.spec.whatwg.org/#response">
    <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin">
    <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
  </head>
  <body>
    <script>
    function checkBodyText(test, response) {
      return response.text().then(function(bodyAsText) {
        assert_equals(bodyAsText, "", "Resolved value should be empty");
        assert_false(response.bodyUsed);
      });
    }

    function checkBodyBlob(test, response) {
      return response.blob().then(function(bodyAsBlob) {
        var promise = new Promise(function(resolve, reject) {
          var reader = new FileReader();
          reader.onload = function(evt) {
            resolve(reader.result)
          };
          reader.onerror = function() {
            reject("Blob's reader failed");
          };
          reader.readAsText(bodyAsBlob);
        });
        return promise.then(function(body) {
          assert_equals(body, "", "Resolved value should be empty");
          assert_false(response.bodyUsed);
        });
      });
    }

    function checkBodyArrayBuffer(test, response) {
      return response.arrayBuffer().then(function(bodyAsArrayBuffer) {
        assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be empty");
        assert_false(response.bodyUsed);
      });
    }

    function checkBodyJSON(test, response) {
      return response.json().then(
        function(bodyAsJSON) {
          assert_unreached("JSON parsing should fail");
        },
        function() {
          assert_false(response.bodyUsed);
        });
    }

    function checkBodyFormData(test, response) {
      return response.formData().then(function(bodyAsFormData) {
        assert_true(bodyAsFormData instanceof FormData, "Should receive a FormData");
        assert_false(response.bodyUsed);
     });
    }

    function checkBodyFormDataError(test, response) {
      return promise_rejects_js(test, TypeError, response.formData()).then(function() {
        assert_false(response.bodyUsed);
      });
    }

    function checkResponseWithNoBody(bodyType, checkFunction, headers = []) {
      promise_test(function(test) {
        var response = new Response(undefined, { "headers": headers });
        assert_false(response.bodyUsed);
        return checkFunction(test, response);
      }, "Consume response's body as " + bodyType);
    }

    checkResponseWithNoBody("text", checkBodyText);
    checkResponseWithNoBody("blob", checkBodyBlob);
    checkResponseWithNoBody("arrayBuffer", checkBodyArrayBuffer);
    checkResponseWithNoBody("json (error case)", checkBodyJSON);
    checkResponseWithNoBody("formData with correct multipart type (error case)", checkBodyFormDataError, [["Content-Type", 'multipart/form-data; boundary="boundary"']]);
    checkResponseWithNoBody("formData with correct urlencoded type", checkBodyFormData, [["Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"]]);
    checkResponseWithNoBody("formData without correct type (error case)", checkBodyFormDataError);

    function checkResponseWithEmptyBody(bodyType, body, asText) {
      promise_test(function(test) {
        var response = new Response(body);
        assert_false(response.bodyUsed, "bodyUsed is false at init");
        if (asText) {
          return response.text().then(function(bodyAsString) {
            assert_equals(bodyAsString.length, 0, "Resolved value should be empty");
            assert_true(response.bodyUsed, "bodyUsed is true after being consumed");
          });
        }
        return response.arrayBuffer().then(function(bodyAsArrayBuffer) {
          assert_equals(bodyAsArrayBuffer.byteLength, 0, "Resolved value should be empty");
          assert_true(response.bodyUsed, "bodyUsed is true after being consumed");
        });
      }, "Consume empty " + bodyType + " response body as " + (asText ? "text" : "arrayBuffer"));
    }

    checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), false);
    checkResponseWithEmptyBody("text", "", false);
    checkResponseWithEmptyBody("blob", new Blob([], { "type" : "text/plain" }), true);
    checkResponseWithEmptyBody("text", "", true);
    checkResponseWithEmptyBody("URLSearchParams", new URLSearchParams(""), true);
    checkResponseWithEmptyBody("FormData", new FormData(), true);
    checkResponseWithEmptyBody("ArrayBuffer", new ArrayBuffer(), true);
    </script>
  </body>
</html>