Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_multipart_set_cookie.js
blobe7ea71535556ecfb2ed0473795a8eb43c2a89216
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
5 );
7 var httpserver = null;
9 ChromeUtils.defineLazyGetter(this, "uri", function () {
10 return "http://localhost:" + httpserver.identity.primaryPort + "/multipart";
11 });
13 function make_channel(url) {
14 return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
17 var multipartBody =
18 "--boundary\r\nSet-Cookie: foo=bar\r\n\r\nSome text\r\n--boundary--";
20 function contentHandler(metadata, response) {
21 response.setHeader(
22 "Content-Type",
23 'multipart/x-mixed-replace; boundary="boundary"'
25 response.bodyOutputStream.write(multipartBody, multipartBody.length);
28 let first = true;
30 function responseHandler(request, buffer) {
31 let channel = request.QueryInterface(Ci.nsIChannel);
32 Assert.equal(buffer, "Some text");
33 Assert.equal(channel.contentType, "text/plain");
35 // two runs: pref on and off
36 if (first) {
37 // foo=bar should not be visible here
38 Assert.equal(
39 Services.cookies.getCookieStringFromHttp(channel.URI, channel),
42 first = false;
43 Services.prefs.setBoolPref(
44 "network.cookie.prevent_set_cookie_from_multipart",
45 false
47 createConverterAndRequest();
48 } else {
49 // validate that the pref is working
50 Assert.equal(
51 Services.cookies.getCookieStringFromHttp(channel.URI, channel),
52 "foo=bar"
54 httpserver.stop(do_test_finished);
58 var multipartListener = {
59 _buffer: "",
61 QueryInterface: ChromeUtils.generateQI([
62 "nsIStreamListener",
63 "nsIRequestObserver",
64 ]),
66 onStartRequest() {
67 this._buffer = "";
70 onDataAvailable(request, stream, offset, count) {
71 try {
72 this._buffer = this._buffer.concat(read_stream(stream, count));
73 dump("BUFFEEE: " + this._buffer + "\n\n");
74 } catch (ex) {
75 do_throw("Error in onDataAvailable: " + ex);
79 onStopRequest(request) {
80 try {
81 responseHandler(request, this._buffer);
82 } catch (ex) {
83 do_throw("Error in closure function: " + ex);
88 function createConverterAndRequest() {
89 var streamConv = Cc["@mozilla.org/streamConverters;1"].getService(
90 Ci.nsIStreamConverterService
92 var conv = streamConv.asyncConvertData(
93 "multipart/x-mixed-replace",
94 "*/*",
95 multipartListener,
96 null
99 var chan = make_channel(uri);
100 chan.asyncOpen(conv);
103 function run_test() {
104 Services.prefs.setBoolPref(
105 "network.cookieJarSettings.unblocked_for_testing",
106 true
109 httpserver = new HttpServer();
110 httpserver.registerPathHandler("/multipart", contentHandler);
111 httpserver.start(-1);
113 createConverterAndRequest();
115 do_test_pending();