michael@0: "use strict"; michael@0: // https://bugzilla.mozilla.org/show_bug.cgi?id=761228 michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "URL", function() { michael@0: return "http://localhost:" + httpServer.identity.primaryPort; michael@0: }); michael@0: michael@0: var httpServer = null; michael@0: const testFileName = "test_customConditionalRequest_304"; michael@0: const basePath = "/" + testFileName + "/"; michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "baseURI", function() { michael@0: return URL + basePath; michael@0: }); michael@0: michael@0: const unexpected304 = "unexpected304"; michael@0: const existingCached304 = "existingCached304"; michael@0: michael@0: function make_uri(url) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: return ios.newURI(url, null, null); michael@0: } michael@0: michael@0: function make_channel(url) { michael@0: var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); michael@0: var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel); michael@0: return chan; michael@0: } michael@0: michael@0: function clearCache() { michael@0: var service = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"] michael@0: .getService(Ci.nsICacheStorageService); michael@0: service.clear(); michael@0: } michael@0: michael@0: function alwaysReturn304Handler(metadata, response) { michael@0: response.setStatusLine(metadata.httpVersion, 304, "Not Modified"); michael@0: response.setHeader("Returned-From-Handler", "1"); michael@0: } michael@0: michael@0: function run_test() { michael@0: evict_cache_entries(); michael@0: michael@0: httpServer = new HttpServer(); michael@0: httpServer.registerPathHandler(basePath + unexpected304, michael@0: alwaysReturn304Handler); michael@0: httpServer.registerPathHandler(basePath + existingCached304, michael@0: alwaysReturn304Handler); michael@0: httpServer.start(-1); michael@0: run_next_test(); michael@0: } michael@0: michael@0: function finish_test(request, buffer) { michael@0: httpServer.stop(do_test_finished); michael@0: } michael@0: michael@0: function consume304(request, buffer) { michael@0: request.QueryInterface(Components.interfaces.nsIHttpChannel); michael@0: do_check_eq(request.responseStatus, 304); michael@0: do_check_eq(request.getResponseHeader("Returned-From-Handler"), "1"); michael@0: run_next_test(); michael@0: } michael@0: michael@0: // Test that we return a 304 response to the caller when we are not expecting michael@0: // a 304 response (i.e. when the server shouldn't have sent us one). michael@0: add_test(function test_unexpected_304() { michael@0: var chan = make_channel(baseURI + unexpected304); michael@0: chan.asyncOpen(new ChannelListener(consume304, null), null); michael@0: }); michael@0: michael@0: // Test that we can cope with a 304 response that was (erroneously) stored in michael@0: // the cache. michael@0: add_test(function test_304_stored_in_cache() { michael@0: asyncOpenCacheEntry( michael@0: baseURI + existingCached304, "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null, michael@0: function (entryStatus, cacheEntry) { michael@0: cacheEntry.setMetaDataElement("request-method", "GET"); michael@0: cacheEntry.setMetaDataElement("response-head", michael@0: "HTTP/1.1 304 Not Modified\r\n" + michael@0: "\r\n"); michael@0: cacheEntry.metaDataReady(); michael@0: cacheEntry.close(); michael@0: michael@0: var chan = make_channel(baseURI + existingCached304); michael@0: michael@0: // make it a custom conditional request michael@0: chan.QueryInterface(Components.interfaces.nsIHttpChannel); michael@0: chan.setRequestHeader("If-None-Match", '"foo"', false); michael@0: michael@0: chan.asyncOpen(new ChannelListener(consume304, null), null); michael@0: }); michael@0: });