summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_original_sent_received_head.js
blob: 651e8d1458c04f038d35a847b988676dc8df7d0d (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
//  HTTP headers test
//  Response headers can be changed after they have been received, e.g. empty
//  headers are deleted, some duplicate header are merged (if no error is
//  thrown), etc.
//
//  The "original header" is introduced to hold the header array in the order
//  and the form as they have been received from the network.
//  Here, the "original headers" are tested.
//
//  Original headers will be stored in the cache as well. This test checks
//  that too.

// Note: sets Cc and Ci variables

"use strict";

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

XPCOMUtils.defineLazyGetter(this, "URL", function () {
  return "http://localhost:" + httpserver.identity.primaryPort;
});

var httpserver = new HttpServer();
var testpath = "/simple";
var httpbody = "0123456789";

var dbg = 1;

function run_test() {
  if (dbg) {
    print("============== START ==========");
  }

  httpserver.registerPathHandler(testpath, serverHandler);
  httpserver.start(-1);
  run_next_test();
}

add_test(function test_headerChange() {
  if (dbg) {
    print("============== test_headerChange setup: in");
  }

  var channel1 = setupChannel(testpath);
  channel1.loadFlags = Ci.nsIRequest.LOAD_BYPASS_CACHE;

  // ChannelListener defined in head_channels.js
  channel1.asyncOpen(new ChannelListener(checkResponse, null));

  if (dbg) {
    print("============== test_headerChange setup: out");
  }
});

add_test(function test_fromCache() {
  if (dbg) {
    print("============== test_fromCache setup: in");
  }

  var channel2 = setupChannel(testpath);
  channel2.loadFlags = Ci.nsIRequest.LOAD_FROM_CACHE;

  // ChannelListener defined in head_channels.js
  channel2.asyncOpen(new ChannelListener(checkResponse, null));

  if (dbg) {
    print("============== test_fromCache setup: out");
  }
});

add_test(function finish() {
  if (dbg) {
    print("============== STOP ==========");
  }
  httpserver.stop(do_test_finished);
});

function setupChannel(path) {
  var chan = NetUtil.newChannel({
    uri: URL + path,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
  chan.requestMethod = "GET";
  return chan;
}

function serverHandler(metadata, response) {
  if (dbg) {
    print("============== serverHandler: in");
  }

  let etag;
  try {
    etag = metadata.getHeader("If-None-Match");
  } catch (ex) {
    etag = "";
  }
  if (etag == "testtag") {
    if (dbg) {
      print("============== 304 answerr: in");
    }
    response.setStatusLine("1.1", 304, "Not Modified");
  } else {
    response.setHeader("Content-Type", "text/plain", false);
    response.setStatusLine("1.1", 200, "OK");

    // Set a empty header. A empty link header will not appear in header list,
    // but in the "original headers", it will be still exactly as received.
    response.setHeaderNoCheck("Link", "", true);
    response.setHeaderNoCheck("Link", "value1");
    response.setHeaderNoCheck("Link", "value2");
    response.setHeaderNoCheck("Location", "loc");
    response.setHeader("Cache-Control", "max-age=10000", false);
    response.setHeader("ETag", "testtag", false);
    response.bodyOutputStream.write(httpbody, httpbody.length);
  }
  if (dbg) {
    print("============== serverHandler: out");
  }
}

function checkResponse(request, data, context) {
  if (dbg) {
    print("============== checkResponse: in");
  }

  request.QueryInterface(Ci.nsIHttpChannel);
  Assert.equal(request.responseStatus, 200);
  Assert.equal(request.responseStatusText, "OK");
  Assert.ok(request.requestSucceeded);

  // Response header have only one link header.
  var linkHeaderFound = 0;
  var locationHeaderFound = 0;
  request.visitResponseHeaders({
    visitHeader: function visit(aName, aValue) {
      if (aName == "link") {
        linkHeaderFound++;
        Assert.equal(aValue, "value1, value2");
      }
      if (aName == "location") {
        locationHeaderFound++;
        Assert.equal(aValue, "loc");
      }
    },
  });
  Assert.equal(linkHeaderFound, 1);
  Assert.equal(locationHeaderFound, 1);

  // The "original header" still contains 3 link headers.
  var linkOrgHeaderFound = 0;
  var locationOrgHeaderFound = 0;
  request.visitOriginalResponseHeaders({
    visitHeader: function visitOrg(aName, aValue) {
      if (aName == "link") {
        if (linkOrgHeaderFound == 0) {
          Assert.equal(aValue, "");
        } else if (linkOrgHeaderFound == 1) {
          Assert.equal(aValue, "value1");
        } else {
          Assert.equal(aValue, "value2");
        }
        linkOrgHeaderFound++;
      }
      if (aName == "location") {
        locationOrgHeaderFound++;
        Assert.equal(aValue, "loc");
      }
    },
  });
  Assert.equal(linkOrgHeaderFound, 3);
  Assert.equal(locationOrgHeaderFound, 1);

  if (dbg) {
    print("============== Remove headers");
  }
  // Remove header.
  request.setResponseHeader("Link", "", false);
  request.setResponseHeader("Location", "", false);

  var linkHeaderFound2 = false;
  var locationHeaderFound2 = 0;
  request.visitResponseHeaders({
    visitHeader: function visit(aName, aValue) {
      if (aName == "Link") {
        linkHeaderFound2 = true;
      }
      if (aName == "Location") {
        locationHeaderFound2 = true;
      }
    },
  });
  Assert.ok(!linkHeaderFound2, "There should be no link header");
  Assert.ok(!locationHeaderFound2, "There should be no location headers.");

  // The "original header" still contains the empty header.
  var linkOrgHeaderFound2 = 0;
  var locationOrgHeaderFound2 = 0;
  request.visitOriginalResponseHeaders({
    visitHeader: function visitOrg(aName, aValue) {
      if (aName == "link") {
        if (linkOrgHeaderFound2 == 0) {
          Assert.equal(aValue, "");
        } else if (linkOrgHeaderFound2 == 1) {
          Assert.equal(aValue, "value1");
        } else {
          Assert.equal(aValue, "value2");
        }
        linkOrgHeaderFound2++;
      }
      if (aName == "location") {
        locationOrgHeaderFound2++;
        Assert.equal(aValue, "loc");
      }
    },
  });
  Assert.ok(linkOrgHeaderFound2 == 3, "Original link header still here.");
  Assert.ok(
    locationOrgHeaderFound2 == 1,
    "Original location header still here."
  );

  if (dbg) {
    print("============== Test GetResponseHeader");
  }
  var linkOrgHeaderFound3 = 0;
  request.getOriginalResponseHeader("link", {
    visitHeader: function visitOrg(aName, aValue) {
      if (linkOrgHeaderFound3 == 0) {
        Assert.equal(aValue, "");
      } else if (linkOrgHeaderFound3 == 1) {
        Assert.equal(aValue, "value1");
      } else {
        Assert.equal(aValue, "value2");
      }
      linkOrgHeaderFound3++;
    },
  });
  Assert.ok(linkOrgHeaderFound2 == 3, "Original link header still here.");

  if (dbg) {
    print("============== checkResponse: out");
  }

  run_next_test();
}