diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /netwerk/test/unit/test_reply_without_content_type.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'netwerk/test/unit/test_reply_without_content_type.js')
-rw-r--r-- | netwerk/test/unit/test_reply_without_content_type.js | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/netwerk/test/unit/test_reply_without_content_type.js b/netwerk/test/unit/test_reply_without_content_type.js new file mode 100644 index 0000000000..806e303dcd --- /dev/null +++ b/netwerk/test/unit/test_reply_without_content_type.js @@ -0,0 +1,149 @@ +// +// tests HTTP replies that lack content-type (where we try to sniff content-type). +// + +// Note: sets Cc and Ci variables +"use strict"; + +const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js"); + +var httpserver = new HttpServer(); +var testpath = "/simple_plainText"; +var httpbody = "<html><body>omg hai</body></html>"; +var testpathGZip = "/simple_gzip"; +//this is compressed httpbody; +var httpbodyGZip = [ + "0x1f", + "0x8b", + "0x8", + "0x0", + "0x0", + "0x0", + "0x0", + "0x0", + "0x0", + "0x3", + "0xb3", + "0xc9", + "0x28", + "0xc9", + "0xcd", + "0xb1", + "0xb3", + "0x49", + "0xca", + "0x4f", + "0xa9", + "0xb4", + "0xcb", + "0xcf", + "0x4d", + "0x57", + "0xc8", + "0x48", + "0xcc", + "0xb4", + "0xd1", + "0x7", + "0xf3", + "0x6c", + "0xf4", + "0xc1", + "0x52", + "0x0", + "0x4", + "0x99", + "0x79", + "0x2b", + "0x21", + "0x0", + "0x0", + "0x0", +]; + +var dbg = 0; +if (dbg) { + print("============== START =========="); +} + +add_test(function test_plainText() { + if (dbg) { + print("============== test_plainText: in"); + } + httpserver.registerPathHandler(testpath, serverHandler_plainText); + httpserver.start(-1); + var channel = setupChannel(testpath); + // ChannelListener defined in head_channels.js + channel.asyncOpen(new ChannelListener(checkRequest, channel)); + do_test_pending(); + if (dbg) { + print("============== test_plainText: out"); + } +}); + +add_test(function test_GZip() { + if (dbg) { + print("============== test_GZip: in"); + } + httpserver.registerPathHandler(testpathGZip, serverHandler_GZip); + httpserver.start(-1); + var channel = setupChannel(testpathGZip); + // ChannelListener defined in head_channels.js + channel.asyncOpen(new ChannelListener(checkRequest, channel, CL_EXPECT_GZIP)); + do_test_pending(); + if (dbg) { + print("============== test_GZip: out"); + } +}); + +function setupChannel(path) { + var chan = NetUtil.newChannel({ + uri: "http://localhost:" + httpserver.identity.primaryPort + path, + loadUsingSystemPrincipal: true, + }); + chan.QueryInterface(Ci.nsIHttpChannel); + chan.requestMethod = "GET"; + return chan; +} + +function serverHandler_plainText(metadata, response) { + if (dbg) { + print("============== serverHandler plainText: in"); + } + // no content type set + // response.setHeader("Content-Type", "text/plain", false); + response.bodyOutputStream.write(httpbody, httpbody.length); + if (dbg) { + print("============== serverHandler plainText: out"); + } +} + +function serverHandler_GZip(metadata, response) { + if (dbg) { + print("============== serverHandler GZip: in"); + } + // no content type set + // response.setHeader("Content-Type", "text/plain", false); + response.setHeader("Content-Encoding", "gzip", false); + var bos = Cc["@mozilla.org/binaryoutputstream;1"].createInstance( + Ci.nsIBinaryOutputStream + ); + bos.setOutputStream(response.bodyOutputStream); + bos.writeByteArray(httpbodyGZip); + if (dbg) { + print("============== serverHandler GZip: out"); + } +} + +function checkRequest(request, data, context) { + if (dbg) { + print("============== checkRequest: in"); + } + Assert.equal(data, httpbody); + Assert.equal(request.QueryInterface(Ci.nsIChannel).contentType, "text/html"); + httpserver.stop(do_test_finished); + run_next_test(); + if (dbg) { + print("============== checkRequest: out"); + } +} |