summaryrefslogtreecommitdiffstats
path: root/dom/xhr/tests/test_XHR_anon.html
blob: 6747b6a918b96363df66cf8ad63578671a58b287 (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
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for XMLHttpRequest with system privileges</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="setup();">
<p id="display">
<iframe id="loader"></iframe>
</p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="application/javascript">

// An XHR with the anon flag set will not send cookie and auth information.
const TEST_URL = "http://example.com/tests/dom/xhr/tests/file_XHR_anon.sjs";
document.cookie = "foo=bar";

let am = {
  authMgr: null,

  init() {
    this.authMgr = SpecialPowers.Cc["@mozilla.org/network/http-auth-manager;1"]
                                .getService(SpecialPowers.Ci.nsIHttpAuthManager)
  },

  addIdentity() {
    this.authMgr.setAuthIdentity("http", "example.com", -1, "basic", "testrealm",
                                 "", "example.com", "user1", "password1");
  },

  tearDown() {
    this.authMgr.clearAll();
  },
}

var tests = [ test1, test2, test2a, test3, test3, test3, test4, test4, test4, test5, test5, test5 ];

function runTests() {
  if (!tests.length) {
    am.tearDown();

    // Resetting the cookie.
    document.cookie = "foo=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
    SimpleTest.finish();
    return;
  }

  var test = tests.shift();
  test();
}

function test1() {
  am.addIdentity();

  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  is(xhr.mozAnon, true, "test1: .mozAnon == true");
  xhr.open("GET", TEST_URL);
  xhr.onload = function onload() {
    is(xhr.status, 200, "test1: " + xhr.responseText);
    am.tearDown();
    runTests();
  };
  xhr.onerror = function onerror() {
    ok(false, "Got an error event!");
    am.tearDown();
    runTests();
  }
  xhr.send();
}

function test2() {
  am.addIdentity();

  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  is(xhr.mozAnon, true, "test2: .mozAnon == true");
  xhr.open("GET", TEST_URL + "?expectAuth=true", true,
           "user2name", "pass2word");
  xhr.onload = function onload() {
    is(xhr.status, 200, "test2: " + xhr.responseText);
    let response = JSON.parse(xhr.responseText);
    is(response.authorization, "Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
    am.tearDown();
    runTests();
  };
  xhr.onerror = function onerror() {
    ok(false, "Got an error event!");
    am.tearDown();
    runTests();
  }
  xhr.send();
}

function test2a() {
  am.addIdentity();

  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  is(xhr.mozAnon, true, "test2: .mozAnon == true");
  xhr.open("GET", TEST_URL + "?expectAuth=true", true,
           "user1", "pass2word");
  xhr.onload = function onload() {
    is(xhr.status, 200, "test2: " + xhr.responseText);
    let response = JSON.parse(xhr.responseText);
    is(response.authorization, "Basic dXNlcjE6cGFzczJ3b3Jk");
    am.tearDown();
    runTests();
  };
  xhr.onerror = function onerror() {
    ok(false, "Got an error event!");
    am.tearDown();
    runTests();
  }
  xhr.send();
}

function test3() {
  am.addIdentity();

  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  is(xhr.mozAnon, true, "test3: .mozAnon == true");
  xhr.open("GET", TEST_URL + "?expectAuth=true", true);
  xhr.onload = function onload() {
    is(xhr.status, 401, "test3: " + xhr.responseText);
    am.tearDown();
    runTests();
  };
  xhr.onerror = function onerror() {
    ok(false, "Got an error event!");
    am.tearDown();
    runTests();
  }
  xhr.send();
}

function test4() {
  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  is(xhr.mozAnon, true, "test4: .mozAnon == true");
  xhr.open("GET", TEST_URL + "?expectAuth=true", true);
  xhr.onload = function onload() {
    is(xhr.status, 401, "test4: " + xhr.responseText);
    runTests();
  };
  xhr.onerror = function onerror() {
    ok(false, "Got an error event!");
    runTests();
  }
  xhr.send();
}

function test5() {
  let xhr = new XMLHttpRequest({mozAnon: true, mozSystem: true});
  is(xhr.mozAnon, true, "test5: .mozAnon == true");
  xhr.open("GET", TEST_URL + "?expectAuth=true", true,
           "user2name", "pass2word");
  xhr.onload = function onload() {
    is(xhr.status, 200, "test5: " + xhr.responseText);
    let response = JSON.parse(xhr.responseText);
    is(response.authorization, "Basic dXNlcjJuYW1lOnBhc3Myd29yZA==");
    runTests();
  };
  xhr.onerror = function onerror() {
    ok(false, "Got an error event!");
    runTests();
  }
  xhr.send();
}

function setup() {
  am.init();
  SimpleTest.waitForExplicitFinish();
  SpecialPowers.pushPermissions([{'type': 'systemXHR', 'allow': true, 'context': document}], runTests);
}
</script>
</pre>
</body>
</html>