Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_304_responses.js
blobdfb740c0ae02ef1a2e93b63003c3995304b3a732
1 "use strict";
2 // https://bugzilla.mozilla.org/show_bug.cgi?id=761228
4 const { HttpServer } = ChromeUtils.importESModule(
5 "resource://testing-common/httpd.sys.mjs"
6 );
8 ChromeUtils.defineLazyGetter(this, "URL", function () {
9 return "http://localhost:" + httpServer.identity.primaryPort;
10 });
12 var httpServer = null;
13 const testFileName = "test_customConditionalRequest_304";
14 const basePath = "/" + testFileName + "/";
16 ChromeUtils.defineLazyGetter(this, "baseURI", function () {
17 return URL + basePath;
18 });
20 const unexpected304 = "unexpected304";
21 const existingCached304 = "existingCached304";
23 function make_channel(url) {
24 return NetUtil.newChannel({
25 uri: url,
26 loadUsingSystemPrincipal: true,
27 }).QueryInterface(Ci.nsIHttpChannel);
30 function alwaysReturn304Handler(metadata, response) {
31 response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
32 response.setHeader("Returned-From-Handler", "1");
35 function run_test() {
36 evict_cache_entries();
38 httpServer = new HttpServer();
39 httpServer.registerPathHandler(
40 basePath + unexpected304,
41 alwaysReturn304Handler
43 httpServer.registerPathHandler(
44 basePath + existingCached304,
45 alwaysReturn304Handler
47 httpServer.start(-1);
48 run_next_test();
51 function consume304(request) {
52 request.QueryInterface(Ci.nsIHttpChannel);
53 Assert.equal(request.responseStatus, 304);
54 Assert.equal(request.getResponseHeader("Returned-From-Handler"), "1");
55 run_next_test();
58 // Test that we return a 304 response to the caller when we are not expecting
59 // a 304 response (i.e. when the server shouldn't have sent us one).
60 add_test(function test_unexpected_304() {
61 var chan = make_channel(baseURI + unexpected304);
62 chan.asyncOpen(new ChannelListener(consume304, null));
63 });
65 // Test that we can cope with a 304 response that was (erroneously) stored in
66 // the cache.
67 add_test(function test_304_stored_in_cache() {
68 asyncOpenCacheEntry(
69 baseURI + existingCached304,
70 "disk",
71 Ci.nsICacheStorage.OPEN_NORMALLY,
72 null,
73 function (entryStatus, cacheEntry) {
74 cacheEntry.setMetaDataElement("request-method", "GET");
75 cacheEntry.setMetaDataElement(
76 "response-head",
77 // eslint-disable-next-line no-useless-concat
78 "HTTP/1.1 304 Not Modified\r\n" + "\r\n"
80 cacheEntry.metaDataReady();
82 var chan = make_channel(baseURI + existingCached304);
84 // make it a custom conditional request
85 chan.QueryInterface(Ci.nsIHttpChannel);
86 chan.setRequestHeader("If-None-Match", '"foo"', false);
88 chan.asyncOpen(new ChannelListener(consume304, null));
91 });