Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_inhibit_caching.js
blob330e0f632f25620e1c64e2fa497bb7439c470bca
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
5 );
7 var first = true;
8 function contentHandler(metadata, response) {
9 response.setHeader("Content-Type", "text/plain");
10 var body = "first";
11 if (!first) {
12 body = "second";
14 first = false;
15 response.bodyOutputStream.write(body, body.length);
18 ChromeUtils.defineLazyGetter(this, "uri", function () {
19 return "http://localhost:" + httpserver.identity.primaryPort;
20 });
22 var httpserver = null;
24 function run_test() {
25 // setup test
26 httpserver = new HttpServer();
27 httpserver.registerPathHandler("/test", contentHandler);
28 httpserver.start(-1);
30 add_test(test_first_response);
31 add_test(test_inhibit_caching);
33 run_next_test();
36 // Makes a regular request
37 function test_first_response() {
38 var chan = NetUtil.newChannel({
39 uri: uri + "/test",
40 loadUsingSystemPrincipal: true,
41 });
42 chan.asyncOpen(new ChannelListener(check_first_response, null));
45 // Checks that we got the appropriate response
46 function check_first_response(request, buffer) {
47 request.QueryInterface(Ci.nsIHttpChannel);
48 Assert.equal(request.responseStatus, 200);
49 Assert.equal(buffer, "first");
50 // Open the cache entry to check its contents
51 asyncOpenCacheEntry(
52 uri + "/test",
53 "disk",
54 Ci.nsICacheStorage.OPEN_READONLY,
55 null,
56 cache_entry_callback
60 // Checks that the cache entry has the correct contents
61 function cache_entry_callback(status, entry) {
62 equal(status, Cr.NS_OK);
63 var inputStream = entry.openInputStream(0);
64 pumpReadStream(inputStream, function (read) {
65 inputStream.close();
66 equal(read, "first");
67 run_next_test();
68 });
71 // Makes a request with the INHIBIT_CACHING load flag
72 function test_inhibit_caching() {
73 var chan = NetUtil.newChannel({
74 uri: uri + "/test",
75 loadUsingSystemPrincipal: true,
76 });
77 chan.QueryInterface(Ci.nsIRequest).loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
78 chan.asyncOpen(new ChannelListener(check_second_response, null));
81 // Checks that we got a different response from the first request
82 function check_second_response(request, buffer) {
83 request.QueryInterface(Ci.nsIHttpChannel);
84 Assert.equal(request.responseStatus, 200);
85 Assert.equal(buffer, "second");
86 // Checks that the cache entry still contains the content from the first request
87 asyncOpenCacheEntry(
88 uri + "/test",
89 "disk",
90 Ci.nsICacheStorage.OPEN_READONLY,
91 null,
92 cache_entry_callback