summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug894586.js
blob: bc25731d36103d95ad901b163cd8cbe46e3010d6 (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
/*
 * Tests for bug 894586: nsSyncLoadService::PushSyncStreamToListener
 * should not fail for channels of unknown size
 */

"use strict";

var contentSecManager = Cc["@mozilla.org/contentsecuritymanager;1"].getService(
  Ci.nsIContentSecurityManager
);

function ProtocolHandler() {
  this.uri = Cc["@mozilla.org/network/simple-uri-mutator;1"]
    .createInstance(Ci.nsIURIMutator)
    .setSpec(this.scheme + ":dummy")
    .finalize();
}

ProtocolHandler.prototype = {
  /** nsIProtocolHandler */
  get scheme() {
    return "x-bug894586";
  },
  newChannel(aURI, aLoadInfo) {
    this.loadInfo = aLoadInfo;
    return this;
  },
  allowPort(port, scheme) {
    return port != -1;
  },

  /** nsIChannel */
  get originalURI() {
    return this.uri;
  },
  get URI() {
    return this.uri;
  },
  owner: null,
  notificationCallbacks: null,
  get securityInfo() {
    return null;
  },
  get contentType() {
    return "text/css";
  },
  set contentType(val) {},
  contentCharset: "UTF-8",
  get contentLength() {
    return -1;
  },
  set contentLength(val) {
    throw Components.Exception(
      "Setting content length",
      Cr.NS_ERROR_NOT_IMPLEMENTED
    );
  },
  open() {
    // throws an error if security checks fail
    contentSecManager.performSecurityCheck(this, null);

    var file = do_get_file("test_bug894586.js", false);
    Assert.ok(file.exists());
    var url = Services.io.newFileURI(file);
    return NetUtil.newChannel({
      uri: url,
      loadUsingSystemPrincipal: true,
    }).open();
  },
  asyncOpen(aListener, aContext) {
    throw Components.Exception("Not implemented", Cr.NS_ERROR_NOT_IMPLEMENTED);
  },
  contentDisposition: Ci.nsIChannel.DISPOSITION_INLINE,
  get contentDispositionFilename() {
    throw Components.Exception("No file name", Cr.NS_ERROR_NOT_AVAILABLE);
  },
  get contentDispositionHeader() {
    throw Components.Exception("No header", Cr.NS_ERROR_NOT_AVAILABLE);
  },

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

  /** nsISupports */
  QueryInterface: ChromeUtils.generateQI([
    "nsIProtocolHandler",
    "nsIRequest",
    "nsIChannel",
  ]),
};

/**
 * Attempt a sync load; we use the stylesheet service to do this for us,
 * based on the knowledge that it forces a sync load under the hood.
 */
function run_test() {
  var handler = new ProtocolHandler();

  Services.io.registerProtocolHandler(
    handler.scheme,
    handler,
    Ci.nsIProtocolHandler.URI_NORELATIVE |
      Ci.nsIProtocolHandler.URI_NOAUTH |
      Ci.nsIProtocolHandler.URI_IS_UI_RESOURCE |
      Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE |
      Ci.nsIProtocolHandler.URI_NON_PERSISTABLE |
      Ci.nsIProtocolHandler.URI_SYNC_LOAD_IS_OK,
    -1
  );
  try {
    var ss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(
      Ci.nsIStyleSheetService
    );
    ss.loadAndRegisterSheet(handler.uri, Ci.nsIStyleSheetService.AGENT_SHEET);
    Assert.ok(
      ss.sheetRegistered(handler.uri, Ci.nsIStyleSheetService.AGENT_SHEET)
    );
  } finally {
    Services.io.unregisterProtocolHandler(handler.scheme);
  }
}

// vim: set et ts=2 :