diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /devtools/shared/tests/xpcshell/test_fetch-bom.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/shared/tests/xpcshell/test_fetch-bom.js')
-rw-r--r-- | devtools/shared/tests/xpcshell/test_fetch-bom.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/devtools/shared/tests/xpcshell/test_fetch-bom.js b/devtools/shared/tests/xpcshell/test_fetch-bom.js new file mode 100644 index 0000000000..3275c9fcd0 --- /dev/null +++ b/devtools/shared/tests/xpcshell/test_fetch-bom.js @@ -0,0 +1,80 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +// Tests for DevToolsUtils.fetch BOM detection. + +const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js"); +const BinaryOutputStream = Components.Constructor( + "@mozilla.org/binaryoutputstream;1", + "nsIBinaryOutputStream", + "setOutputStream" +); + +function write8(bos) { + bos.write8(0xef); + bos.write8(0xbb); + bos.write8(0xbf); + bos.write8(0x68); + bos.write8(0xc4); + bos.write8(0xb1); +} + +function write16be(bos) { + bos.write8(0xfe); + bos.write8(0xff); + bos.write8(0x00); + bos.write8(0x68); + bos.write8(0x01); + bos.write8(0x31); +} + +function write16le(bos) { + bos.write8(0xff); + bos.write8(0xfe); + bos.write8(0x68); + bos.write8(0x00); + bos.write8(0x31); + bos.write8(0x01); +} + +function getHandler(writer) { + return function (request, response) { + response.setStatusLine(request.httpVersion, 200, "OK"); + + const bos = new BinaryOutputStream(response.bodyOutputStream); + writer(bos); + }; +} + +const server = new HttpServer(); +server.registerDirectory("/", do_get_cwd()); +server.registerPathHandler("/u8", getHandler(write8)); +server.registerPathHandler("/u16be", getHandler(write16be)); +server.registerPathHandler("/u16le", getHandler(write16le)); +server.start(-1); + +const port = server.identity.primaryPort; +const serverURL = "http://localhost:" + port; + +do_get_profile(); + +registerCleanupFunction(() => { + return new Promise(resolve => server.stop(resolve)); +}); + +add_task(async function () { + await test_one(serverURL + "/u8", "UTF-8"); + await test_one(serverURL + "/u16be", "UTF-16BE"); + await test_one(serverURL + "/u16le", "UTF-16LE"); +}); + +async function test_one(url, encoding) { + // Be sure to set the encoding to something that will yield an + // invalid result if BOM sniffing is not done. + await DevToolsUtils.fetch(url, { charset: "ISO-8859-1" }).then( + ({ content }) => { + Assert.equal(content, "hı", "The content looks correct for " + encoding); + } + ); +} |