summaryrefslogtreecommitdiff
path: root/netwerk/test/browser/browser_nsIFormPOSTActionChannel.js
blob: 150c4feca9b53b670b6f79567132015fc4e932ab (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Tests for bug 1241377: A channel with nsIFormPOSTActionChannel interface
 * should be able to accept form POST.
 */

Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Task.jsm");

const SCHEME = "x-bug1241377";

const FORM_BASE = SCHEME + "://dummy/form/";
const NORMAL_FORM_URI = FORM_BASE + "normal.html";
const UPLOAD_FORM_URI = FORM_BASE + "upload.html";
const POST_FORM_URI = FORM_BASE + "post.html";

const ACTION_BASE = SCHEME + "://dummy/action/";
const NORMAL_ACTION_URI = ACTION_BASE + "normal.html";
const UPLOAD_ACTION_URI = ACTION_BASE + "upload.html";
const POST_ACTION_URI = ACTION_BASE + "post.html";

function CustomProtocolHandler() {
}
CustomProtocolHandler.prototype = {
  /** nsIProtocolHandler */
  get scheme() {
    return SCHEME;
  },
  get defaultPort() {
    return -1;
  },
  get protocolFlags() {
    return Ci.nsIProtocolHandler.URI_NORELATIVE |
           Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE;
  },
  newURI: function(aSpec, aOriginCharset, aBaseURI) {
    var uri = Cc["@mozilla.org/network/standard-url;1"].
              createInstance(Ci.nsIURI);
    uri.spec = aSpec;
    return uri;
  },
  newChannel2: function(aURI, aLoadInfo) {
    return new CustomChannel(aURI, aLoadInfo);
  },
  newChannel: function(aURI) {
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },
  allowPort: function(port, scheme) {
    return port != -1;
  },

  /** nsIFactory */
  createInstance: function(aOuter, aIID) {
    if (aOuter) {
      throw Cr.NS_ERROR_NO_AGGREGATION;
    }
    return this.QueryInterface(aIID);
  },
  lockFactory: function() {},

  /** nsISupports */
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIProtocolHandler,
                                         Ci.nsIFactory]),
  classID: Components.ID("{16d594bc-d9d8-47ae-a139-ea714dc0c35c}")
};

function CustomChannel(aURI, aLoadInfo) {
  this.uri = aURI;
  this.loadInfo = aLoadInfo;

  this._uploadStream = null;

  var interfaces = [Ci.nsIRequest, Ci.nsIChannel];
  if (this.uri.spec == POST_ACTION_URI) {
    interfaces.push(Ci.nsIFormPOSTActionChannel);
  } else if (this.uri.spec == UPLOAD_ACTION_URI) {
    interfaces.push(Ci.nsIUploadChannel);
  }
  this.QueryInterface = XPCOMUtils.generateQI(interfaces);
}
CustomChannel.prototype = {
  /** nsIUploadChannel */
  get uploadStream() {
    return this._uploadStream;
  },
  set uploadStream(val) {
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },
  setUploadStream: function(aStream, aContentType, aContentLength) {
    this._uploadStream = aStream;
  },

  /** nsIChannel */
  get originalURI() {
    return this.uri;
  },
  get URI() {
    return this.uri;
  },
  owner: null,
  notificationCallbacks: null,
  get securityInfo() {
    return null;
  },
  get contentType() {
    return "text/html";
  },
  set contentType(val) {
  },
  contentCharset: "UTF-8",
  get contentLength() {
    return -1;
  },
  set contentLength(val) {
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },
  open: function() {
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },
  open2: function() {
    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  },
  asyncOpen: function(aListener, aContext) {
    var data = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>test bug 1241377</title>
</head>
<body>
`;

    if (this.uri.spec.startsWith(FORM_BASE)) {
      data += `
<form id="form" action="${this.uri.spec.replace(FORM_BASE, ACTION_BASE)}"
      method="post" enctype="text/plain" target="frame">
<input type="hidden" name="foo" value="bar">
<input type="submit">
</form>

<iframe id="frame" name="frame" width="200" height="200"></iframe>

<script type="text/javascript">
<!--
document.getElementById('form').submit();
//-->
</script>
`;
    } else if (this.uri.spec.startsWith(ACTION_BASE)) {
      var postData = "";
      if (this._uploadStream) {
        var bstream = Cc["@mozilla.org/binaryinputstream;1"]
            .createInstance(Ci.nsIBinaryInputStream);
        bstream.setInputStream(this._uploadStream);
        postData = bstream.readBytes(bstream.available());
      }
      data += `
<input id="upload_stream" value="${this._uploadStream ? "yes" : "no"}">
<input id="post_data" value="${btoa(postData)}">
`;
    }

    data += `
</body>
</html>
`;

    var stream = Cc["@mozilla.org/io/string-input-stream;1"]
        .createInstance(Ci.nsIStringInputStream);
    stream.setData(data, data.length);

    var runnable = {
      run: () => {
        try {
          aListener.onStartRequest(this, aContext);
        } catch(e) {}
        try {
          aListener.onDataAvailable(this, aContext, stream, 0, stream.available());
        } catch(e) {}
        try {
          aListener.onStopRequest(this, aContext, Cr.NS_OK);
        } catch(e) {}
      }
    };
    Services.tm.currentThread.dispatch(runnable, Ci.nsIEventTarget.DISPATCH_NORMAL);
  },
  asyncOpen2: function(aListener) {
    this.asyncOpen(aListener, null);
  },

  /** nsIRequest */
  get name() {
    return this.uri.spec;
  },
  isPending: function () {
    return false;
  },
  get status() {
    return Cr.NS_OK;
  },
  cancel: function(status) {},
  loadGroup: null,
  loadFlags: Ci.nsIRequest.LOAD_NORMAL |
             Ci.nsIRequest.INHIBIT_CACHING |
             Ci.nsIRequest.LOAD_BYPASS_CACHE,
};

function frameScript() {
  addMessageListener("Test:WaitForIFrame", function() {
    var check = function() {
      if (content) {
        var frame = content.document.getElementById("frame");
        if (frame) {
          var upload_stream = frame.contentDocument.getElementById("upload_stream");
          var post_data = frame.contentDocument.getElementById("post_data");
          if (upload_stream && post_data) {
            sendAsyncMessage("Test:IFrameLoaded", [upload_stream.value, post_data.value]);
            return;
          }
        }
      }

      setTimeout(check, 100);
    };

    check();
  });
}

function loadTestTab(uri) {
  gBrowser.selectedTab = gBrowser.addTab(uri);
  var browser = gBrowser.selectedBrowser;

  let manager = browser.messageManager;
  browser.messageManager.loadFrameScript("data:,(" + frameScript.toString() + ")();", true);

  return new Promise(resolve => {
    function listener({ data: [hasUploadStream, postData] }) {
      manager.removeMessageListener("Test:IFrameLoaded", listener);
      resolve([hasUploadStream, atob(postData)]);
    }

    manager.addMessageListener("Test:IFrameLoaded", listener);
    manager.sendAsyncMessage("Test:WaitForIFrame");
  });
}

add_task(function*() {
  var handler = new CustomProtocolHandler();
  var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  registrar.registerFactory(handler.classID, "",
                            "@mozilla.org/network/protocol;1?name=" + handler.scheme,
                            handler);
  registerCleanupFunction(function() {
    registrar.unregisterFactory(handler.classID, handler);
  });
});

add_task(function*() {
  var [hasUploadStream, postData] = yield loadTestTab(NORMAL_FORM_URI);
  is(hasUploadStream, "no", "normal action should not have uploadStream");

  gBrowser.removeCurrentTab();
});

add_task(function*() {
  var [hasUploadStream, postData] = yield loadTestTab(UPLOAD_FORM_URI);
  is(hasUploadStream, "no", "upload action should not have uploadStream");

  gBrowser.removeCurrentTab();
});

add_task(function*() {
  var [hasUploadStream, postData] = yield loadTestTab(POST_FORM_URI);
  is(hasUploadStream, "yes", "post action should have uploadStream");
  is(postData,
     "Content-Type: text/plain\r\n" +
     "Content-Length: 9\r\n" +
     "\r\n" +
     "foo=bar\r\n", "POST data is received correctly");

  gBrowser.removeCurrentTab();
});