summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_suspend_channel_on_modified.js
blob: a4f7c221e8f02c32a0e4a1fa1a7181d15649f78f (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
// This file tests async handling of a channel suspended in http-on-modify-request.

var CC = Components.Constructor;

Cu.import("resource://testing-common/httpd.js");
Cu.import("resource://gre/modules/NetUtil.jsm");

var obs = Cc["@mozilla.org/observer-service;1"]
            .getService(Ci.nsIObserverService);

var ios = Cc["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);

// baseUrl is always the initial connection attempt and is handled by
// failResponseHandler since every test expects that request will either be
// redirected or cancelled.
var baseUrl;

function failResponseHandler(metadata, response)
{
  var text = "failure response";
  response.setHeader("Content-Type", "text/plain", false);
  response.bodyOutputStream.write(text, text.length);
  do_check_true(false, "Received request when we shouldn't.");
}

function successResponseHandler(metadata, response)
{
  var text = "success response";
  response.setHeader("Content-Type", "text/plain", false);
  response.bodyOutputStream.write(text, text.length);
  do_check_true(true, "Received expected request.");
}

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

function startChannelRequest(baseUrl, flags, expectedResponse=null) {
  var chan = NetUtil.newChannel({
    uri: baseUrl,
    loadUsingSystemPrincipal: true
  });
  chan.asyncOpen2(new ChannelListener((request, data, context) => {
    if (expectedResponse) {
      do_check_eq(data, expectedResponse);
    } else {
      do_check_true(!!!data, "no response");
    }
    do_execute_soon(run_next_test)
  }, null, flags));
}


add_test(function testSimpleRedirect() {
  onModifyListener(chan => {
    chan.redirectTo(ios.newURI(`${baseUrl}/success`));
  });
  startChannelRequest(baseUrl, undefined, "success response");
});

add_test(function testSimpleCancel() {
  onModifyListener(chan => {
    chan.cancel(Cr.NS_BINDING_ABORTED);
  });
  startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
});

add_test(function testSimpleCancelRedirect() {
  onModifyListener(chan => {
    chan.redirectTo(ios.newURI(`${baseUrl}/fail`));
    chan.cancel(Cr.NS_BINDING_ABORTED);
  });
  startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
});

// Test a request that will get redirected asynchronously.  baseUrl should
// not be requested, we should receive the request for the redirectedUrl.
add_test(function testAsyncRedirect() {
  onModifyListener(chan => {
    // Suspend the channel then yield to make this async.
    chan.suspend();
    Promise.resolve().then(() => {
      chan.redirectTo(ios.newURI(`${baseUrl}/success`));
      chan.resume();
    });
  });
  startChannelRequest(baseUrl, undefined, "success response");
});

add_test(function testSyncRedirect() {
  onModifyListener(chan => {
    chan.suspend();
    chan.redirectTo(ios.newURI(`${baseUrl}/success`));
    Promise.resolve().then(() => {
      chan.resume();
    });
  });
  startChannelRequest(baseUrl, undefined, "success response");
});

add_test(function testAsyncCancel() {
  onModifyListener(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);
});

add_test(function testSyncCancel() {
  onModifyListener(chan => {
    chan.suspend();
    chan.cancel(Cr.NS_BINDING_ABORTED);
    Promise.resolve().then(() => {
      chan.resume();
    });
  });
  startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
});

// Test request that will get redirected and cancelled asynchronously,
// ensure no connection is made.
add_test(function testAsyncCancelRedirect() {
  onModifyListener(chan => {
    // Suspend the channel then yield to make this async.
    chan.suspend();
    Promise.resolve().then(() => {
      chan.cancel(Cr.NS_BINDING_ABORTED);
      chan.redirectTo(ios.newURI(`${baseUrl}/fail`));
      chan.resume();
    });
  });
  startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
});

// Test a request that will get cancelled synchronously, ensure async redirect
// is not made.
add_test(function testSyncCancelRedirect() {
  onModifyListener(chan => {
    chan.suspend();
    chan.cancel(Cr.NS_BINDING_ABORTED);
    Promise.resolve().then(() => {
      chan.redirectTo(ios.newURI(`${baseUrl}/fail`));
      chan.resume();
    });
  });
  startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
});

function run_test() {
  var httpServer = new HttpServer();
  httpServer.registerPathHandler("/", failResponseHandler);
  httpServer.registerPathHandler("/fail", failResponseHandler);
  httpServer.registerPathHandler("/success", successResponseHandler);
  httpServer.start(-1);

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

  run_next_test();

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