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
|
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Response clone</title>
<meta name="help" href="https://fetch.spec.whatwg.org/#response">
<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 src="../resources/utils.js"></script>
<script>
var defaultValues = { "type" : "default",
"url" : "",
"ok" : true,
"status" : 200,
"statusText" : "OK"
};
var response = new Response();
var clonedResponse = response.clone();
test(function() {
for (var attributeName in defaultValues) {
var expectedValue = defaultValues[attributeName];
assert_equals(clonedResponse[attributeName], expectedValue,
"Expect default response." + attributeName + " is " + expectedValue);
}
}, "Check Response's clone with default values, without body");
var body = "This is response body";
var headersInit = { "name" : "value" };
var responseInit = { "status" : 200,
"statusText" : "GOOD",
"headers" : headersInit
};
var response = new Response(body, responseInit);
var clonedResponse = response.clone();
test(function() {
assert_equals(clonedResponse.status, responseInit["status"],
"Expect response.status is " + responseInit["status"]);
assert_equals(clonedResponse.statusText, responseInit["statusText"],
"Expect response.statusText is " + responseInit["statusText"]);
assert_equals(clonedResponse.headers.get("name"), "value",
"Expect response.headers has name:value header");
}, "Check Response's clone has the expected attribute values");
promise_test(function(test) {
return validateStreamFromString(response.body.getReader(), body);
}, "Check orginal response's body after cloning");
promise_test(function(test) {
return validateStreamFromString(clonedResponse.body.getReader(), body);
}, "Check cloned response's body");
promise_test(function(test) {
var disturbedResponse = new Response("data");
return disturbedResponse.text().then(function() {
assert_true(disturbedResponse.bodyUsed, "response is disturbed");
assert_throws(new TypeError() , function() { disturbedResponse.clone(); },
"Expect TypeError exception");
});
}, "Cannot clone a disturbed response");
promise_test(function(t) {
var clone;
var result;
var response;
return fetch('../resources/trickle.py?count=2&delay=100').then(function(res) {
clone = res.clone();
response = res;
return clone.arrayBuffer();
}).then(function(r) {
assert_equals(r.byteLength, 26);
result = r;
return response.arrayBuffer();
}).then(function(r) {
assert_array_equals(r, result, "cloned responses should provide the same data");
});
}, 'Cloned responses should provide the same data');
promise_test(function(t) {
var clone;
return fetch('../resources/trickle.py?count=2&delay=100').then(function(res) {
clone = res.clone();
res.body.cancel();
assert_true(res.bodyUsed);
assert_false(clone.bodyUsed);
return clone.arrayBuffer();
}).then(function(r) {
assert_equals(r.byteLength, 26);
assert_true(clone.bodyUsed);
});
}, 'Cancelling stream should not affect cloned one');
</script>
</body>
</html>
|