netwerk/test/unit/test_304_responses.js

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 "use strict";
michael@0 2 // https://bugzilla.mozilla.org/show_bug.cgi?id=761228
michael@0 3
michael@0 4 Cu.import("resource://testing-common/httpd.js");
michael@0 5
michael@0 6 XPCOMUtils.defineLazyGetter(this, "URL", function() {
michael@0 7 return "http://localhost:" + httpServer.identity.primaryPort;
michael@0 8 });
michael@0 9
michael@0 10 var httpServer = null;
michael@0 11 const testFileName = "test_customConditionalRequest_304";
michael@0 12 const basePath = "/" + testFileName + "/";
michael@0 13
michael@0 14 XPCOMUtils.defineLazyGetter(this, "baseURI", function() {
michael@0 15 return URL + basePath;
michael@0 16 });
michael@0 17
michael@0 18 const unexpected304 = "unexpected304";
michael@0 19 const existingCached304 = "existingCached304";
michael@0 20
michael@0 21 function make_uri(url) {
michael@0 22 var ios = Cc["@mozilla.org/network/io-service;1"].
michael@0 23 getService(Ci.nsIIOService);
michael@0 24 return ios.newURI(url, null, null);
michael@0 25 }
michael@0 26
michael@0 27 function make_channel(url) {
michael@0 28 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 29 var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
michael@0 30 return chan;
michael@0 31 }
michael@0 32
michael@0 33 function clearCache() {
michael@0 34 var service = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"]
michael@0 35 .getService(Ci.nsICacheStorageService);
michael@0 36 service.clear();
michael@0 37 }
michael@0 38
michael@0 39 function alwaysReturn304Handler(metadata, response) {
michael@0 40 response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
michael@0 41 response.setHeader("Returned-From-Handler", "1");
michael@0 42 }
michael@0 43
michael@0 44 function run_test() {
michael@0 45 evict_cache_entries();
michael@0 46
michael@0 47 httpServer = new HttpServer();
michael@0 48 httpServer.registerPathHandler(basePath + unexpected304,
michael@0 49 alwaysReturn304Handler);
michael@0 50 httpServer.registerPathHandler(basePath + existingCached304,
michael@0 51 alwaysReturn304Handler);
michael@0 52 httpServer.start(-1);
michael@0 53 run_next_test();
michael@0 54 }
michael@0 55
michael@0 56 function finish_test(request, buffer) {
michael@0 57 httpServer.stop(do_test_finished);
michael@0 58 }
michael@0 59
michael@0 60 function consume304(request, buffer) {
michael@0 61 request.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 62 do_check_eq(request.responseStatus, 304);
michael@0 63 do_check_eq(request.getResponseHeader("Returned-From-Handler"), "1");
michael@0 64 run_next_test();
michael@0 65 }
michael@0 66
michael@0 67 // Test that we return a 304 response to the caller when we are not expecting
michael@0 68 // a 304 response (i.e. when the server shouldn't have sent us one).
michael@0 69 add_test(function test_unexpected_304() {
michael@0 70 var chan = make_channel(baseURI + unexpected304);
michael@0 71 chan.asyncOpen(new ChannelListener(consume304, null), null);
michael@0 72 });
michael@0 73
michael@0 74 // Test that we can cope with a 304 response that was (erroneously) stored in
michael@0 75 // the cache.
michael@0 76 add_test(function test_304_stored_in_cache() {
michael@0 77 asyncOpenCacheEntry(
michael@0 78 baseURI + existingCached304, "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
michael@0 79 function (entryStatus, cacheEntry) {
michael@0 80 cacheEntry.setMetaDataElement("request-method", "GET");
michael@0 81 cacheEntry.setMetaDataElement("response-head",
michael@0 82 "HTTP/1.1 304 Not Modified\r\n" +
michael@0 83 "\r\n");
michael@0 84 cacheEntry.metaDataReady();
michael@0 85 cacheEntry.close();
michael@0 86
michael@0 87 var chan = make_channel(baseURI + existingCached304);
michael@0 88
michael@0 89 // make it a custom conditional request
michael@0 90 chan.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 91 chan.setRequestHeader("If-None-Match", '"foo"', false);
michael@0 92
michael@0 93 chan.asyncOpen(new ChannelListener(consume304, null), null);
michael@0 94 });
michael@0 95 });

mercurial