Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_redirect-caching_canceled.js
blob847e054d0732937e8226568dcedd75c62f0fb1cc
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
5 );
7 ChromeUtils.defineLazyGetter(this, "URL", function () {
8 return "http://localhost:" + httpServer.identity.primaryPort;
9 });
11 var httpServer = null;
12 // Need to randomize, because apparently no one clears our cache
13 var randomPath = "/redirect/" + Math.random();
15 ChromeUtils.defineLazyGetter(this, "randomURI", function () {
16 return URL + randomPath;
17 });
19 function make_channel(url) {
20 return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
23 const responseBody = "response body";
25 function redirectHandler(metadata, response) {
26 response.setStatusLine(metadata.httpVersion, 301, "Moved");
27 response.setHeader("Location", URL + "/content", false);
28 response.setHeader("Cache-control", "max-age=1000", false);
31 function contentHandler(metadata, response) {
32 response.setHeader("Content-Type", "text/plain");
33 response.bodyOutputStream.write(responseBody, responseBody.length);
36 function firstTimeThrough(request, buffer) {
37 Assert.equal(buffer, responseBody);
38 var chan = make_channel(randomURI);
39 chan.asyncOpen(new ChannelListener(secondTimeThrough, null));
42 function secondTimeThrough(request, buffer) {
43 Assert.equal(buffer, responseBody);
44 var chan = make_channel(randomURI);
45 chan.loadFlags |= Ci.nsIRequest.LOAD_FROM_CACHE;
46 chan.notificationCallbacks = new ChannelEventSink(ES_ABORT_REDIRECT);
47 chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE));
50 function finish_test(request, buffer) {
51 Assert.equal(buffer, "");
52 httpServer.stop(do_test_finished);
55 function run_test() {
56 httpServer = new HttpServer();
57 httpServer.registerPathHandler(randomPath, redirectHandler);
58 httpServer.registerPathHandler("/content", contentHandler);
59 httpServer.start(-1);
61 var chan = make_channel(randomURI);
62 chan.asyncOpen(new ChannelListener(firstTimeThrough, null));
63 do_test_pending();