OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <meta charset=utf-8> |
| 3 <title>Fetch: Request and Response text() should decode as UTF-8</title> |
| 4 <script src="/resources/testharness.js"></script> |
| 5 <script src="/resources/testharnessreport.js"></script> |
| 6 <link rel="help" href="https://fetch.spec.whatwg.org/#body-mixin" /> |
| 7 |
| 8 <script src="../resources/utils.js"></script> |
| 9 <script> |
| 10 |
| 11 function testTextDecoding(body, expectedText, urlParameter, title) |
| 12 { |
| 13 var arrayBuffer = stringToArray(body); |
| 14 |
| 15 promise_test(function(test) { |
| 16 var request = new Request("", {method: "POST", body: arrayBuffer}); |
| 17 return request.text().then(function(value) { |
| 18 assert_equals(value, expectedText, "Request.text() should decode dat
a as UTF-8"); |
| 19 }); |
| 20 }, title + " with Request.text()"); |
| 21 |
| 22 promise_test(function(test) { |
| 23 var response = new Response(arrayBuffer); |
| 24 return response.text().then(function(value) { |
| 25 assert_equals(value, expectedText, "Response.text() should decode da
ta as UTF-8"); |
| 26 }); |
| 27 }, title + " with Response.text()"); |
| 28 |
| 29 promise_test(function(test) { |
| 30 return fetch("../resources/status.py?code=200&type=text%2Fplain%3Bcharse
t%3DUTF-8&content=" + urlParameter).then(function(response) { |
| 31 return response.text().then(function(value) { |
| 32 assert_equals(value, expectedText, "Fetched Response.text() shou
ld decode data as UTF-8"); |
| 33 }); |
| 34 }); |
| 35 }, title + " with fetched data (UTF-8 charset)"); |
| 36 |
| 37 promise_test(function(test) { |
| 38 return fetch("../resources/status.py?code=200&type=text%2Fplain%3Bcharse
t%3DUTF-16&content=" + urlParameter).then(function(response) { |
| 39 return response.text().then(function(value) { |
| 40 assert_equals(value, expectedText, "Fetched Response.text() shou
ld decode data as UTF-8"); |
| 41 }); |
| 42 }); |
| 43 }, title + " with fetched data (UTF-16 charset)"); |
| 44 |
| 45 promise_test(function(test) { |
| 46 return new Response(body).arrayBuffer().then(function(buffer) { |
| 47 assert_array_equals(new Uint8Array(buffer), encode_utf8(body), "Resp
onse.arrayBuffer() should contain data encoded as UTF-8"); |
| 48 }); |
| 49 }, title + " (Response object)"); |
| 50 |
| 51 promise_test(function(test) { |
| 52 return new Request("", {method: "POST", body: body}).arrayBuffer().then(
function(buffer) { |
| 53 assert_array_equals(new Uint8Array(buffer), encode_utf8(body), "Requ
est.arrayBuffer() should contain data encoded as UTF-8"); |
| 54 }); |
| 55 }, title + " (Request object)"); |
| 56 |
| 57 } |
| 58 |
| 59 var utf8WithBOM = "\xef\xbb\xbf\xe4\xb8\x89\xe6\x9d\x91\xe3\x81\x8b\xe3\x81\xaa\
xe5\xad\x90"; |
| 60 var utf8WithBOMAsURLParameter = "%EF%BB%BF%E4%B8%89%E6%9D%91%E3%81%8B%E3%81%AA%E
5%AD%90"; |
| 61 var utf8WithoutBOM = "\xe4\xb8\x89\xe6\x9d\x91\xe3\x81\x8b\xe3\x81\xaa\xe5\xad\x
90"; |
| 62 var utf8WithoutBOMAsURLParameter = "%E4%B8%89%E6%9D%91%E3%81%8B%E3%81%AA%E5%AD%9
0"; |
| 63 var utf8Decoded = "三村かな子"; |
| 64 testTextDecoding(utf8WithBOM, utf8Decoded, utf8WithBOMAsURLParameter, "UTF-8 wit
h BOM"); |
| 65 testTextDecoding(utf8WithoutBOM, utf8Decoded, utf8WithoutBOMAsURLParameter, "UTF
-8 without BOM"); |
| 66 |
| 67 var utf16BEWithBOM = "\xfe\xff\x4e\x09\x67\x51\x30\x4b\x30\x6a\x5b\x50"; |
| 68 var utf16BEWithBOMAsURLParameter = "%fe%ff%4e%09%67%51%30%4b%30%6a%5b%50"; |
| 69 var utf16BEWithBOMDecodedAsUTF8 = "��N\tgQ0K0j[P"; |
| 70 testTextDecoding(utf16BEWithBOM, utf16BEWithBOMDecodedAsUTF8, utf16BEWithBOMAsUR
LParameter, "UTF-16BE with BOM decoded as UTF-8"); |
| 71 |
| 72 var utf16LEWithBOM = "\xff\xfe\x09\x4e\x51\x67\x4b\x30\x6a\x30\x50\x5b"; |
| 73 var utf16LEWithBOMAsURLParameter = "%ff%fe%09%4e%51%67%4b%30%6a%30%50%5b"; |
| 74 var utf16LEWithBOMDecodedAsUTF8 = "��\tNQgK0j0P["; |
| 75 testTextDecoding(utf16LEWithBOM, utf16LEWithBOMDecodedAsUTF8, utf16LEWithBOMAsUR
LParameter, "UTF-16LE with BOM decoded as UTF-8"); |
| 76 |
| 77 var utf16WithoutBOM = "\xe6\x00\xf8\x00\xe5\x00\x0a\x00\xc6\x30\xb9\x30\xc8\x30\
x0a\x00"; |
| 78 var utf16WithoutBOMAsURLParameter = "%E6%00%F8%00%E5%00%0A%00%C6%30%B9%30%C8%30%
0A%00"; |
| 79 var utf16WithoutBOMDecoded = "\ufffd\u0000\ufffd\u0000\ufffd\u0000\u000a\u0000\u
fffd\u0030\ufffd\u0030\ufffd\u0030\u000a\u0000"; |
| 80 testTextDecoding(utf16WithoutBOM, utf16WithoutBOMDecoded, utf16WithoutBOMAsURLPa
rameter, "UTF-16 without BOM decoded as UTF-8"); |
| 81 |
| 82 </script> |
OLD | NEW |