2 // https://bugzilla.mozilla.org/show_bug.cgi?id=761228
4 const { HttpServer
} = ChromeUtils
.importESModule(
5 "resource://testing-common/httpd.sys.mjs"
8 ChromeUtils
.defineLazyGetter(this, "URL", function () {
9 return "http://localhost:" + httpServer
.identity
.primaryPort
;
12 var httpServer
= null;
13 const testFileName
= "test_customConditionalRequest_304";
14 const basePath
= "/" + testFileName
+ "/";
16 ChromeUtils
.defineLazyGetter(this, "baseURI", function () {
17 return URL
+ basePath
;
20 const unexpected304
= "unexpected304";
21 const existingCached304
= "existingCached304";
23 function make_channel(url
) {
24 return NetUtil
.newChannel({
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");
36 evict_cache_entries();
38 httpServer
= new HttpServer();
39 httpServer
.registerPathHandler(
40 basePath
+ unexpected304
,
41 alwaysReturn304Handler
43 httpServer
.registerPathHandler(
44 basePath
+ existingCached304
,
45 alwaysReturn304Handler
51 function consume304(request
) {
52 request
.QueryInterface(Ci
.nsIHttpChannel
);
53 Assert
.equal(request
.responseStatus
, 304);
54 Assert
.equal(request
.getResponseHeader("Returned-From-Handler"), "1");
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));
65 // Test that we can cope with a 304 response that was (erroneously) stored in
67 add_test(function test_304_stored_in_cache() {
69 baseURI
+ existingCached304
,
71 Ci
.nsICacheStorage
.OPEN_NORMALLY
,
73 function (entryStatus
, cacheEntry
) {
74 cacheEntry
.setMetaDataElement("request-method", "GET");
75 cacheEntry
.setMetaDataElement(
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));