browser_NetUtil.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. Components.utils.import("resource://gre/modules/NetUtil.jsm");
  6. function test() {
  7. waitForExplicitFinish();
  8. // We overload this test to include verifying that httpd.js is
  9. // importable as a testing-only JS module.
  10. Components.utils.import("resource://testing-common/httpd.js", {});
  11. nextTest();
  12. }
  13. function nextTest() {
  14. if (tests.length)
  15. executeSoon(tests.shift());
  16. else
  17. executeSoon(finish);
  18. }
  19. var tests = [
  20. test_asyncFetchBadCert,
  21. ];
  22. function test_asyncFetchBadCert() {
  23. // Try a load from an untrusted cert, with errors supressed
  24. NetUtil.asyncFetch({
  25. uri: "https://untrusted.example.com",
  26. loadUsingSystemPrincipal: true
  27. }, function (aInputStream, aStatusCode, aRequest) {
  28. ok(!Components.isSuccessCode(aStatusCode), "request failed");
  29. ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
  30. // Now try again with a channel whose notificationCallbacks doesn't suprress errors
  31. let channel = NetUtil.newChannel({
  32. uri: "https://untrusted.example.com",
  33. loadUsingSystemPrincipal: true});
  34. channel.notificationCallbacks = {
  35. QueryInterface: XPCOMUtils.generateQI([Ci.nsIProgressEventSink,
  36. Ci.nsIInterfaceRequestor]),
  37. getInterface: function (aIID) { return this.QueryInterface(aIID); },
  38. onProgress: function () {},
  39. onStatus: function () {}
  40. };
  41. NetUtil.asyncFetch(channel, function (aInputStream, aStatusCode, aRequest) {
  42. ok(!Components.isSuccessCode(aStatusCode), "request failed");
  43. ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
  44. // Now try a valid request
  45. NetUtil.asyncFetch({
  46. uri: "https://example.com",
  47. loadUsingSystemPrincipal: true
  48. }, function (aInputStream, aStatusCode, aRequest) {
  49. info("aStatusCode for valid request: " + aStatusCode);
  50. ok(Components.isSuccessCode(aStatusCode), "request succeeded");
  51. ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
  52. ok(aRequest.requestSucceeded, "HTTP request succeeded");
  53. nextTest();
  54. });
  55. });
  56. });
  57. }
  58. function WindowListener(aURL, aCallback) {
  59. this.callback = aCallback;
  60. this.url = aURL;
  61. }
  62. WindowListener.prototype = {
  63. onOpenWindow: function(aXULWindow) {
  64. var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
  65. .getInterface(Ci.nsIDOMWindow);
  66. var self = this;
  67. domwindow.addEventListener("load", function() {
  68. domwindow.removeEventListener("load", arguments.callee, false);
  69. if (domwindow.document.location.href != self.url)
  70. return;
  71. // Allow other window load listeners to execute before passing to callback
  72. executeSoon(function() {
  73. self.callback(domwindow);
  74. });
  75. }, false);
  76. },
  77. onCloseWindow: function(aXULWindow) {},
  78. onWindowTitleChange: function(aXULWindow, aNewTitle) {}
  79. }