summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_redirect_error.js
blob: 2fc6155cd537cb463c7031a33c9869ed5bd53853 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const REDIRECT_FROM = `${TEST_BASE_URL}redirect_error.sjs`;

const REDIRECT_TO = "https://www.bank1.com/"; // Bad-cert host.

function isRedirectedURISpec(aURISpec) {
  return isRedirectedURI(Services.io.newURI(aURISpec));
}

function isRedirectedURI(aURI) {
  // Compare only their before-hash portion.
  return Services.io.newURI(REDIRECT_TO).equalsExceptRef(aURI);
}

/*
   Test.

1. Load redirect_bug623155.sjs#BG in a background tab.

2. The redirected URI is <https://www.bank1.com/#BG>, which displayes a cert
   error page.

3. Switch the tab to foreground.

4. Check the URLbar's value, expecting <https://www.bank1.com/#BG>

5. Load redirect_bug623155.sjs#FG in the foreground tab.

6. The redirected URI is <https://www.bank1.com/#FG>. And this is also
   a cert-error page.

7. Check the URLbar's value, expecting <https://www.bank1.com/#FG>

8. End.

 */

var gNewTab;

function test() {
  waitForExplicitFinish();

  // Load a URI in the background.
  gNewTab = BrowserTestUtils.addTab(gBrowser, REDIRECT_FROM + "#BG");
  gBrowser
    .getBrowserForTab(gNewTab)
    .webProgress.addProgressListener(
      gWebProgressListener,
      Ci.nsIWebProgress.NOTIFY_LOCATION
    );
}

var gWebProgressListener = {
  QueryInterface: ChromeUtils.generateQI([
    "nsIWebProgressListener",
    "nsISupportsWeakReference",
  ]),

  // ---------------------------------------------------------------------------
  // NOTIFY_LOCATION mode should work fine without these methods.
  //
  // onStateChange: function() {},
  // onStatusChange: function() {},
  // onProgressChange: function() {},
  // onSecurityChange: function() {},
  // ----------------------------------------------------------------------------

  onLocationChange(aWebProgress, aRequest, aLocation, aFlags) {
    if (!aRequest) {
      // This is bug 673752, or maybe initial "about:blank".
      return;
    }

    ok(gNewTab, "There is a new tab.");
    ok(
      isRedirectedURI(aLocation),
      "onLocationChange catches only redirected URI."
    );

    if (aLocation.ref == "BG") {
      // This is background tab's request.
      isnot(gNewTab, gBrowser.selectedTab, "This is a background tab.");
    } else if (aLocation.ref == "FG") {
      // This is foreground tab's request.
      is(gNewTab, gBrowser.selectedTab, "This is a foreground tab.");
    } else {
      // We shonuld not reach here.
      ok(false, "This URI hash is not expected:" + aLocation.ref);
    }

    let isSelectedTab = gNewTab.selected;
    setTimeout(delayed, 0, isSelectedTab);
  },
};

function delayed(aIsSelectedTab) {
  // Switch tab and confirm URL bar.
  if (!aIsSelectedTab) {
    gBrowser.selectedTab = gNewTab;
  }

  let currentURI = gBrowser.selectedBrowser.currentURI.spec;
  ok(
    isRedirectedURISpec(currentURI),
    "The content area is redirected. aIsSelectedTab:" + aIsSelectedTab
  );
  is(
    gURLBar.value,
    currentURI,
    "The URL bar shows the content URI. aIsSelectedTab:" + aIsSelectedTab
  );

  if (!aIsSelectedTab) {
    // If this was a background request, go on a foreground request.
    BrowserTestUtils.loadURIString(
      gBrowser.selectedBrowser,
      REDIRECT_FROM + "#FG"
    );
  } else {
    // Othrewise, nothing to do remains.
    finish();
  }
}

/* Cleanup */
registerCleanupFunction(function () {
  if (gNewTab) {
    gBrowser
      .getBrowserForTab(gNewTab)
      .webProgress.removeProgressListener(gWebProgressListener);

    gBrowser.removeTab(gNewTab);
  }
  gNewTab = null;
});