Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 2 | var httpserver = new HttpServer(); |
michael@0 | 3 | |
michael@0 | 4 | var ios; |
michael@0 | 5 | |
michael@0 | 6 | // Test the handling of a cache revalidation with mismatching last-modified |
michael@0 | 7 | // headers. If we get such a revalidation the cache entry should be purged. |
michael@0 | 8 | // see bug 717350 |
michael@0 | 9 | |
michael@0 | 10 | // In this test the wrong data is from 11-16-1994 with a value of 'A', |
michael@0 | 11 | // and the right data is from 11-15-1994 with a value of 'B'. |
michael@0 | 12 | |
michael@0 | 13 | // the same URL is requested 3 times. the first time the wrong data comes |
michael@0 | 14 | // back, the second time that wrong data is revalidated with a 304 but |
michael@0 | 15 | // a L-M header of the right data (this triggers a cache purge), and |
michael@0 | 16 | // the third time the right data is returned. |
michael@0 | 17 | |
michael@0 | 18 | var listener_3 = { |
michael@0 | 19 | // this listener is used to process the the request made after |
michael@0 | 20 | // the cache invalidation. it expects to see the 'right data' |
michael@0 | 21 | |
michael@0 | 22 | QueryInterface: function(iid) { |
michael@0 | 23 | if (iid.equals(Components.interfaces.nsIStreamListener) || |
michael@0 | 24 | iid.equals(Components.interfaces.nsIRequestObserver) || |
michael@0 | 25 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 26 | return this; |
michael@0 | 27 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 28 | }, |
michael@0 | 29 | |
michael@0 | 30 | onStartRequest: function test_onStartR(request, ctx) {}, |
michael@0 | 31 | |
michael@0 | 32 | onDataAvailable: function test_ODA(request, cx, inputStream, |
michael@0 | 33 | offset, count) { |
michael@0 | 34 | var data = new BinaryInputStream(inputStream).readByteArray(count); |
michael@0 | 35 | |
michael@0 | 36 | do_check_eq(data[0], "B".charCodeAt(0)); |
michael@0 | 37 | }, |
michael@0 | 38 | |
michael@0 | 39 | onStopRequest: function test_onStopR(request, ctx, status) { |
michael@0 | 40 | httpserver.stop(do_test_finished); |
michael@0 | 41 | } |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | XPCOMUtils.defineLazyGetter(this, "listener_2", function() { |
michael@0 | 45 | return { |
michael@0 | 46 | // this listener is used to process the revalidation of the |
michael@0 | 47 | // corrupted cache entry. its revalidation prompts it to be cleaned |
michael@0 | 48 | |
michael@0 | 49 | QueryInterface: function(iid) { |
michael@0 | 50 | if (iid.equals(Components.interfaces.nsIStreamListener) || |
michael@0 | 51 | iid.equals(Components.interfaces.nsIRequestObserver) || |
michael@0 | 52 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 53 | return this; |
michael@0 | 54 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 55 | }, |
michael@0 | 56 | |
michael@0 | 57 | onStartRequest: function test_onStartR(request, ctx) {}, |
michael@0 | 58 | |
michael@0 | 59 | onDataAvailable: function test_ODA(request, cx, inputStream, |
michael@0 | 60 | offset, count) { |
michael@0 | 61 | var data = new BinaryInputStream(inputStream).readByteArray(count); |
michael@0 | 62 | |
michael@0 | 63 | // This is 'A' from a cache revalidation, but that reval will clean the cache |
michael@0 | 64 | // because of mismatched last-modified response headers |
michael@0 | 65 | |
michael@0 | 66 | do_check_eq(data[0], "A".charCodeAt(0)); |
michael@0 | 67 | }, |
michael@0 | 68 | |
michael@0 | 69 | onStopRequest: function test_onStopR(request, ctx, status) { |
michael@0 | 70 | var channel = request.QueryInterface(Ci.nsIHttpChannel); |
michael@0 | 71 | |
michael@0 | 72 | var chan = ios.newChannel("http://localhost:" + |
michael@0 | 73 | httpserver.identity.primaryPort + |
michael@0 | 74 | "/test1", "", null); |
michael@0 | 75 | chan.asyncOpen(listener_3, null); |
michael@0 | 76 | } |
michael@0 | 77 | }; |
michael@0 | 78 | }); |
michael@0 | 79 | |
michael@0 | 80 | XPCOMUtils.defineLazyGetter(this, "listener_1", function() { |
michael@0 | 81 | return { |
michael@0 | 82 | // this listener processes the initial request from a empty cache. |
michael@0 | 83 | // the server responds with the wrong data ('A') |
michael@0 | 84 | |
michael@0 | 85 | QueryInterface: function(iid) { |
michael@0 | 86 | if (iid.equals(Components.interfaces.nsIStreamListener) || |
michael@0 | 87 | iid.equals(Components.interfaces.nsIRequestObserver) || |
michael@0 | 88 | iid.equals(Components.interfaces.nsISupports)) |
michael@0 | 89 | return this; |
michael@0 | 90 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 91 | }, |
michael@0 | 92 | |
michael@0 | 93 | onStartRequest: function test_onStartR(request, ctx) {}, |
michael@0 | 94 | |
michael@0 | 95 | onDataAvailable: function test_ODA(request, cx, inputStream, |
michael@0 | 96 | offset, count) { |
michael@0 | 97 | var data = new BinaryInputStream(inputStream).readByteArray(count); |
michael@0 | 98 | do_check_eq(data[0], "A".charCodeAt(0)); |
michael@0 | 99 | }, |
michael@0 | 100 | |
michael@0 | 101 | onStopRequest: function test_onStopR(request, ctx, status) { |
michael@0 | 102 | var channel = request.QueryInterface(Ci.nsIHttpChannel); |
michael@0 | 103 | |
michael@0 | 104 | var chan = ios.newChannel("http://localhost:" + |
michael@0 | 105 | httpserver.identity.primaryPort + |
michael@0 | 106 | "/test1", "", null); |
michael@0 | 107 | chan.asyncOpen(listener_2, null); |
michael@0 | 108 | } |
michael@0 | 109 | }; |
michael@0 | 110 | }); |
michael@0 | 111 | |
michael@0 | 112 | function run_test() { |
michael@0 | 113 | do_get_profile(); |
michael@0 | 114 | ios = Cc["@mozilla.org/network/io-service;1"] |
michael@0 | 115 | .getService(Ci.nsIIOService); |
michael@0 | 116 | |
michael@0 | 117 | evict_cache_entries(); |
michael@0 | 118 | |
michael@0 | 119 | httpserver.registerPathHandler("/test1", handler); |
michael@0 | 120 | httpserver.start(-1); |
michael@0 | 121 | |
michael@0 | 122 | var port = httpserver.identity.primaryPort; |
michael@0 | 123 | |
michael@0 | 124 | var chan = ios.newChannel("http://localhost:" + port + "/test1", "", null); |
michael@0 | 125 | chan.asyncOpen(listener_1, null); |
michael@0 | 126 | |
michael@0 | 127 | do_test_pending(); |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | var iter=0; |
michael@0 | 131 | function handler(metadata, response) { |
michael@0 | 132 | iter++; |
michael@0 | 133 | if (metadata.hasHeader("If-Modified-Since")) { |
michael@0 | 134 | response.setStatusLine(metadata.httpVersion, 304, "Not Modified"); |
michael@0 | 135 | response.setHeader("Last-Modified", "Tue, 15 Nov 1994 12:45:26 GMT", false); |
michael@0 | 136 | } |
michael@0 | 137 | else { |
michael@0 | 138 | response.setStatusLine(metadata.httpVersion, 200, "OK"); |
michael@0 | 139 | response.setHeader("Cache-Control", "max-age=0", false) |
michael@0 | 140 | if (iter == 1) { |
michael@0 | 141 | // simulated wrong response |
michael@0 | 142 | response.setHeader("Last-Modified", "Wed, 16 Nov 1994 00:00:00 GMT", false); |
michael@0 | 143 | response.bodyOutputStream.write("A", 1); |
michael@0 | 144 | } |
michael@0 | 145 | if (iter == 3) { |
michael@0 | 146 | // 'correct' response |
michael@0 | 147 | response.setHeader("Last-Modified", "Tue, 15 Nov 1994 12:45:26 GMT", false); |
michael@0 | 148 | response.bodyOutputStream.write("B", 1); |
michael@0 | 149 | } |
michael@0 | 150 | } |
michael@0 | 151 | } |