summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_mismatch_last-modified.js
blob: 28f9f9642e3aa0db19cd82c8491ed8610d2b75b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"use strict";

const BinaryInputStream = Components.Constructor(
  "@mozilla.org/binaryinputstream;1",
  "nsIBinaryInputStream",
  "setInputStream"
);

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
var httpserver = new HttpServer();

// Test the handling of a cache revalidation with mismatching last-modified
// headers. If we get such a revalidation the cache entry should be purged.
// see bug 717350

// In this test the wrong data is from 11-16-1994 with a value of 'A',
// and the right data is from 11-15-1994 with a value of 'B'.

// the same URL is requested 3 times. the first time the wrong data comes
// back, the second time that wrong data is revalidated with a 304 but
// a L-M header of the right data (this triggers a cache purge), and
// the third time the right data is returned.

var listener_3 = {
  // this listener is used to process the the request made after
  // the cache invalidation. it expects to see the 'right data'

  QueryInterface: ChromeUtils.generateQI([
    "nsIStreamListener",
    "nsIRequestObserver",
  ]),

  onStartRequest: function test_onStartR(request) {},

  onDataAvailable: function test_ODA(request, inputStream, offset, count) {
    var data = new BinaryInputStream(inputStream).readByteArray(count);

    Assert.equal(data[0], "B".charCodeAt(0));
  },

  onStopRequest: function test_onStopR(request, status) {
    httpserver.stop(do_test_finished);
  },
};

XPCOMUtils.defineLazyGetter(this, "listener_2", function () {
  return {
    // this listener is used to process the revalidation of the
    // corrupted cache entry. its revalidation prompts it to be cleaned

    QueryInterface: ChromeUtils.generateQI([
      "nsIStreamListener",
      "nsIRequestObserver",
    ]),

    onStartRequest: function test_onStartR(request) {},

    onDataAvailable: function test_ODA(request, inputStream, offset, count) {
      var data = new BinaryInputStream(inputStream).readByteArray(count);

      // This is 'A' from a cache revalidation, but that reval will clean the cache
      // because of mismatched last-modified response headers

      Assert.equal(data[0], "A".charCodeAt(0));
    },

    onStopRequest: function test_onStopR(request, status) {
      request.QueryInterface(Ci.nsIHttpChannel);
      var chan = NetUtil.newChannel({
        uri: "http://localhost:" + httpserver.identity.primaryPort + "/test1",
        loadUsingSystemPrincipal: true,
      });
      chan.asyncOpen(listener_3);
    },
  };
});

XPCOMUtils.defineLazyGetter(this, "listener_1", function () {
  return {
    // this listener processes the initial request from a empty cache.
    // the server responds with the wrong data ('A')

    QueryInterface: ChromeUtils.generateQI([
      "nsIStreamListener",
      "nsIRequestObserver",
    ]),

    onStartRequest: function test_onStartR(request) {},

    onDataAvailable: function test_ODA(request, inputStream, offset, count) {
      var data = new BinaryInputStream(inputStream).readByteArray(count);
      Assert.equal(data[0], "A".charCodeAt(0));
    },

    onStopRequest: function test_onStopR(request, status) {
      request.QueryInterface(Ci.nsIHttpChannel);
      var chan = NetUtil.newChannel({
        uri: "http://localhost:" + httpserver.identity.primaryPort + "/test1",
        loadUsingSystemPrincipal: true,
      });
      chan.asyncOpen(listener_2);
    },
  };
});

function run_test() {
  do_get_profile();

  evict_cache_entries();

  httpserver.registerPathHandler("/test1", handler);
  httpserver.start(-1);

  var port = httpserver.identity.primaryPort;
  var chan = NetUtil.newChannel({
    uri: "http://localhost:" + port + "/test1",
    loadUsingSystemPrincipal: true,
  });
  chan.asyncOpen(listener_1);

  do_test_pending();
}

var iter = 0;
function handler(metadata, response) {
  iter++;
  if (metadata.hasHeader("If-Modified-Since")) {
    response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
    response.setHeader("Last-Modified", "Tue, 15 Nov 1994 12:45:26 GMT", false);
  } else {
    response.setStatusLine(metadata.httpVersion, 200, "OK");
    response.setHeader("Cache-Control", "max-age=0", false);
    if (iter == 1) {
      // simulated wrong response
      response.setHeader(
        "Last-Modified",
        "Wed, 16 Nov 1994 00:00:00 GMT",
        false
      );
      response.bodyOutputStream.write("A", 1);
    }
    if (iter == 3) {
      // 'correct' response
      response.setHeader(
        "Last-Modified",
        "Tue, 15 Nov 1994 12:45:26 GMT",
        false
      );
      response.bodyOutputStream.write("B", 1);
    }
  }
}