summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_304_headers.js
blob: edf0e4f38c163cca62ac5aad9fc474d9dc619fdf (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
"use strict";

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);

ChromeUtils.defineLazyGetter(this, "URL", function () {
  return `http://localhost:${httpServer.identity.primaryPort}/test`;
});

let httpServer = null;

function make_channel(url) {
  return NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
}

function contentHandler(metadata, response) {
  response.seizePower();
  let etag = "";
  try {
    etag = metadata.getHeader("If-None-Match");
  } catch (ex) {}

  if (etag == "test-etag1") {
    response.write("HTTP/1.1 304 Not Modified\r\n");

    response.write("Link: <ref>; param1=value1\r\n");
    response.write("Link: <ref2>; param2=value2\r\n");
    response.write("Link: <ref3>; param1=value1\r\n");
    response.write("\r\n");
    response.finish();
    return;
  }

  response.write("HTTP/1.1 200 OK\r\n");

  response.write("ETag: test-etag1\r\n");
  response.write("Link: <ref>; param1=value1\r\n");
  response.write("Link: <ref2>; param2=value2\r\n");
  response.write("Link: <ref3>; param1=value1\r\n");
  response.write("\r\n");
  response.finish();
}

add_task(async function test() {
  httpServer = new HttpServer();
  httpServer.registerPathHandler("/test", contentHandler);
  httpServer.start(-1);
  registerCleanupFunction(async () => {
    await httpServer.stop();
  });

  let chan = make_channel(Services.io.newURI(URL));
  chan.requestMethod = "HEAD";
  await new Promise(resolve => {
    chan.asyncOpen({
      onStopRequest(req, status) {
        equal(status, Cr.NS_OK);
        equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
        equal(
          req.QueryInterface(Ci.nsIHttpChannel).getResponseHeader("Link"),
          "<ref>; param1=value1, <ref2>; param2=value2, <ref3>; param1=value1"
        );
        resolve();
      },
      onStartRequest() {},
      onDataAvailable() {},
    });
  });

  chan = make_channel(Services.io.newURI(URL));
  chan.requestMethod = "HEAD";
  await new Promise(resolve => {
    chan.asyncOpen({
      onStopRequest(req, status) {
        equal(status, Cr.NS_OK);
        equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
        equal(
          req.QueryInterface(Ci.nsIHttpChannel).getResponseHeader("Link"),
          "<ref>; param1=value1, <ref2>; param2=value2, <ref3>; param1=value1"
        );
        resolve();
      },
      onStartRequest() {},
      onDataAvailable() {},
    });
  });
});