summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_suspend_channel_on_examine.js
blob: 95517324524680099ce3a732757bfc7d31ddca25 (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
// This file tests async handling of a channel suspended in http-on-modify-request.
"use strict";

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

var obs = Services.obs;

var baseUrl;

function responseHandler(metadata, response) {
  var text = "testing";
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Set-Cookie", "chewy", false);
  response.bodyOutputStream.write(text, text.length);
}

function onExamineListener(callback) {
  obs.addObserver(
    {
      observe(subject, topic, data) {
        var obs = Cc["@mozilla.org/observer-service;1"].getService();
        obs = obs.QueryInterface(Ci.nsIObserverService);
        obs.removeObserver(this, "http-on-examine-response");
        callback(subject.QueryInterface(Ci.nsIHttpChannel));
      },
    },
    "http-on-examine-response"
  );
}

function startChannelRequest(baseUrl, flags, callback) {
  var chan = NetUtil.newChannel({
    uri: baseUrl,
    loadUsingSystemPrincipal: true,
  });
  chan.asyncOpen(new ChannelListener(callback, null, flags));
}

// We first make a request that we'll cancel asynchronously.  The response will
// still contain the set-cookie header. Then verify the cookie was not actually
// retained.
add_test(function testAsyncCancel() {
  onExamineListener(chan => {
    // Suspend the channel then yield to make this async.
    chan.suspend();
    Promise.resolve().then(() => {
      chan.cancel(Cr.NS_BINDING_ABORTED);
      chan.resume();
    });
  });
  startChannelRequest(baseUrl, CL_EXPECT_FAILURE, (request, data, context) => {
    Assert.ok(!data, "no response");

    Assert.equal(
      Services.cookies.countCookiesFromHost("localhost"),
      0,
      "no cookies set"
    );

    executeSoon(run_next_test);
  });
});

function run_test() {
  var httpServer = new HttpServer();
  httpServer.registerPathHandler("/", responseHandler);
  httpServer.start(-1);

  baseUrl = `http://localhost:${httpServer.identity.primaryPort}`;

  run_next_test();

  registerCleanupFunction(function() {
    httpServer.stop(() => {});
  });
}