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_rcwn_always_cache_new_content.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_rcwn_always_cache_new_content.js')
-rw-r--r-- | netwerk/test/unit/test_rcwn_always_cache_new_content.js | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/netwerk/test/unit/test_rcwn_always_cache_new_content.js b/netwerk/test/unit/test_rcwn_always_cache_new_content.js new file mode 100644 index 0000000000..8244265088 --- /dev/null +++ b/netwerk/test/unit/test_rcwn_always_cache_new_content.js @@ -0,0 +1,114 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +"use strict"; + +const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js"); + +var httpserver = new HttpServer(); +httpserver.start(-1); +const PORT = httpserver.identity.primaryPort; + +function make_channel(url) { + return NetUtil.newChannel({ + uri: url, + loadUsingSystemPrincipal: true, + }).QueryInterface(Ci.nsIHttpChannel); +} + +let gFirstResponseBody = "first version"; +let gSecondResponseBody = "second version"; +let gRequestCounter = 0; + +function test_handler(metadata, response) { + response.setHeader("Content-Type", "text/plain"); + response.setHeader("Cache-Control", "max-age=3600"); + response.setHeader("ETag", "test-etag1"); + + switch (gRequestCounter) { + case 0: + response.bodyOutputStream.write( + gFirstResponseBody, + gFirstResponseBody.length + ); + break; + case 1: + response.bodyOutputStream.write( + gSecondResponseBody, + gSecondResponseBody.length + ); + break; + default: + do_throw("Unexpected request"); + } + response.setStatusLine(metadata.httpVersion, 200, "OK"); +} + +function checkContent(request, buffer, context, isFromCache) { + let isRacing = request.QueryInterface(Ci.nsICacheInfoChannel).isRacing(); + switch (gRequestCounter) { + case 0: + Assert.equal(buffer, gFirstResponseBody); + Assert.ok(!isFromCache); + Assert.ok(!isRacing); + break; + case 1: + Assert.equal(buffer, gSecondResponseBody); + Assert.ok(!isFromCache); + Assert.ok(isRacing); + break; + case 2: + Assert.equal(buffer, gSecondResponseBody); + Assert.ok(isFromCache); + Assert.ok(!isRacing); + break; + default: + do_throw("Unexpected request"); + } + + gRequestCounter++; + executeSoon(() => { + testGenerator.next(); + }); +} + +function run_test() { + do_get_profile(); + // In this test, we race the requests manually using |TriggerNetwork|, + // therefore we should disable racing in the HttpChannel first. + Services.prefs.setBoolPref("network.http.rcwn.enabled", false); + httpserver.registerPathHandler("/rcwn", test_handler); + testGenerator.next(); + do_test_pending(); +} + +let testGenerator = testSteps(); +function* testSteps() { + // Store first version of the content in the cache. + let channel = make_channel("http://localhost:" + PORT + "/rcwn"); + channel.asyncOpen(new ChannelListener(checkContent, null)); + yield undefined; + equal(gRequestCounter, 1); + + // Simulate the network victory by setting high delay for the cache fetch and + // triggering the network. + channel = make_channel("http://localhost:" + PORT + "/rcwn"); + channel + .QueryInterface(Ci.nsIRaceCacheWithNetwork) + .test_delayCacheEntryOpeningBy(100000); + // Trigger network after 50 ms. + channel.QueryInterface(Ci.nsIRaceCacheWithNetwork).test_triggerNetwork(50); + channel.asyncOpen(new ChannelListener(checkContent, null)); + yield undefined; + equal(gRequestCounter, 2); + + // Simulate navigation back by specifying VALIDATE_NEVER flag. + channel = make_channel("http://localhost:" + PORT + "/rcwn"); + channel.loadFlags = Ci.nsIRequest.VALIDATE_NEVER; + channel.asyncOpen(new ChannelListener(checkContent, null)); + yield undefined; + equal(gRequestCounter, 3); + + httpserver.stop(do_test_finished); +} |