summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_early_hint_listener.js
blob: a0db7190fe5de523173b2730a44f3a5d44a59874 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

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

var httpserver = new HttpServer();
var earlyhintspath = "/earlyhints";
var multipleearlyhintspath = "/multipleearlyhintspath";
var otherearlyhintspath = "/otherearlyhintspath";
var noearlyhintspath = "/noearlyhints";
var httpbody = "0123456789";
var hint1 = "</style.css>; rel=preload; as=style";
var hint2 = "</img.png>; rel=preload; as=image";

function earlyHintsResponse(metadata, response) {
  response.setInformationalResponseStatusLine(
    metadata.httpVersion,
    103,
    "EarlyHints"
  );
  response.setInformationalResponseHeader("Link", hint1, false);

  response.setHeader("Content-Type", "text/plain", false);
  response.bodyOutputStream.write(httpbody, httpbody.length);
}

function multipleEarlyHintsResponse(metadata, response) {
  response.setInformationalResponseStatusLine(
    metadata.httpVersion,
    103,
    "EarlyHints"
  );
  response.setInformationalResponseHeader("Link", hint1, false);
  response.setInformationalHeaderNoCheck("Link", hint2);

  response.setHeader("Content-Type", "text/plain", false);
  response.bodyOutputStream.write(httpbody, httpbody.length);
}

function otherHeadersEarlyHintsResponse(metadata, response) {
  response.setInformationalResponseStatusLine(
    metadata.httpVersion,
    103,
    "EarlyHints"
  );
  response.setInformationalResponseHeader("Link", hint1, false);
  response.setInformationalResponseHeader("Something", "something", false);

  response.setHeader("Content-Type", "text/plain", false);
  response.bodyOutputStream.write(httpbody, httpbody.length);
}

function noEarlyHintsResponse(metadata, response) {
  response.setHeader("Content-Type", "text/plain", false);
  response.bodyOutputStream.write(httpbody, httpbody.length);
}

let EarlyHintsListener = function () {};

EarlyHintsListener.prototype = {
  _expected_hints: "",
  earlyHintsReceived: false,

  earlyHint: function testEarlyHint(header) {
    Assert.equal(header, this._expected_hints);
    this.earlyHintsReceived = true;
  },
};

function chanPromise(uri, listener) {
  return new Promise(resolve => {
    var principal = Services.scriptSecurityManager.createContentPrincipal(
      NetUtil.newURI(uri),
      {}
    );
    var chan = NetUtil.newChannel({
      uri,
      loadingPrincipal: principal,
      securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
      contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
    });

    chan
      .QueryInterface(Ci.nsIHttpChannelInternal)
      .setEarlyHintObserver(listener);
    chan.asyncOpen(new ChannelListener(resolve));
  });
}

add_task(async function setup() {
  httpserver.registerPathHandler(earlyhintspath, earlyHintsResponse);
  httpserver.registerPathHandler(
    multipleearlyhintspath,
    multipleEarlyHintsResponse
  );
  httpserver.registerPathHandler(
    otherearlyhintspath,
    otherHeadersEarlyHintsResponse
  );
  httpserver.registerPathHandler(noearlyhintspath, noEarlyHintsResponse);
  httpserver.start(-1);
});

add_task(async function early_hints() {
  let earlyHints = new EarlyHintsListener();
  earlyHints._expected_hints = hint1;

  await chanPromise(
    "http://localhost:" + httpserver.identity.primaryPort + earlyhintspath,
    earlyHints
  );
  Assert.ok(earlyHints.earlyHintsReceived);
});

add_task(async function no_early_hints() {
  let earlyHints = new EarlyHintsListener("");

  await chanPromise(
    "http://localhost:" + httpserver.identity.primaryPort + noearlyhintspath,
    earlyHints
  );
  Assert.ok(!earlyHints.earlyHintsReceived);
});

add_task(async function early_hints_multiple() {
  let earlyHints = new EarlyHintsListener();
  earlyHints._expected_hints = hint1 + ", " + hint2;

  await chanPromise(
    "http://localhost:" +
      httpserver.identity.primaryPort +
      multipleearlyhintspath,
    earlyHints
  );
  Assert.ok(earlyHints.earlyHintsReceived);
});

add_task(async function early_hints_other() {
  let earlyHints = new EarlyHintsListener();
  earlyHints._expected_hints = hint1;

  await chanPromise(
    "http://localhost:" + httpserver.identity.primaryPort + otherearlyhintspath,
    earlyHints
  );
  Assert.ok(earlyHints.earlyHintsReceived);
});

add_task(async function early_hints_only_secure_context() {
  Services.prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
  let earlyHints2 = new EarlyHintsListener();
  earlyHints2._expected_hints = "";

  await chanPromise(
    "http://localhost:" + httpserver.identity.primaryPort + earlyhintspath,
    earlyHints2
  );
  Assert.ok(!earlyHints2.earlyHintsReceived);
});

add_task(async function clean_up() {
  Services.prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
  await new Promise(resolve => {
    httpserver.stop(resolve);
  });
});