blob: 414573e84cdfc3354884c6349763ff921012ffa0 [file] [log] [blame]
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:161<!doctype html>
2<html>
3 <head>
4 <title>XMLHttpRequest: send() - "Basic" authenticated request using setRequestHeader() when there is an existing session</title>
5 <script src="/resources/testharness.js"></script>
6 <script src="/resources/testharnessreport.js"></script>
7 <!-- These spec references do not make much sense simply because the spec doesn't say very much about this.. -->
8 <link rel="help" href="http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#the-setrequestheader()-method" data-tested-assertations="following::ol[1]/li[6]" />
9 <link rel="help" href="http://dvcs.w3.org/hg/xhr/raw-file/tip/Overview.html#the-send()-method" data-tested-assertations="following::code[contains(@title,'http-authorization')]/.." />
10 </head>
11 <body>
12 <div id="log"></div>
13 <script>
14 var test = async_test()
15 test.step(function() {
16 var client = new XMLHttpRequest(),
17 urlstart = location.host + location.pathname.replace(/\/[^\/]*$/, '/')
Hallvord Reiar M. Steen237f53d2013-07-02 11:56:1618 // Initial request: no information is known to the UA about whether resources/auth4/auth.php requires authentication,
Hallvord Reiar M. Steen129c0742013-07-03 11:38:1719 // hence it first sends a normal request, gets a 401 response that will not be passed on to the JS, and sends a new
Hallvord Reiar M. Steen237f53d2013-07-02 11:56:1620 // request with an Authorization header before returning
21 // (Note: this test will only work as expected if run once per browsing session)
Hallvord Reiar M. Steen129c0742013-07-03 11:38:1722 client.open("GET", location.protocol+'//'+urlstart + "resources/auth4/auth.php", false, 'open-user', 'open-pass')
Hallvord Reiar M. Steen237f53d2013-07-02 11:56:1623 // initial request - this will get a 401 response and re-try with HTTP auth
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1624 client.send(null)
25 assert_equals(client.responseText, 'open-user\nopen-pass')
26 assert_equals(client.status, 200)
Hallvord Reiar M. Steen29bbd332013-07-02 11:57:5327 assert_equals(client.getResponseHeader('x-challenge'), 'DID')
Hallvord Reiar M. Steen237f53d2013-07-02 11:56:1628 // Another request, this time user,pass is omitted and an Authorization header set explicitly
29 // Here the URL is known to require authentication (from the request above), and the UA has cached open-user:open-pass credentials
30 // However, these session credentials should now be overridden by the setRequestHeader() call so the UA should immediately
31 // send basic Authorization header with credentials user:pass. (This part is perhaps not well specified anywhere)
Hallvord Reiar M. Steen4e96dac2013-07-03 12:42:1032 client.open("GET", location.protocol+'//'+urlstart + "resources/auth4/auth.php", true)
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1633 client.setRequestHeader("x-user", 'user')
34 client.setRequestHeader("x-pass", 'pass')
35 client.setRequestHeader('Authorization', 'Basic dXNlcjpwYXNz')
36 client.onreadystatechange = function () {
Hallvord Reiar M. Steen4e96dac2013-07-03 12:42:1037 if (client.readyState < 4) {return}
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1638 test.step( function () {
39 assert_equals(client.responseText, 'user\npass')
40 assert_equals(client.status, 200)
41 assert_equals(client.getResponseHeader('x-challenge'), 'DID-NOT')
42 test.done()
43 } )
44 }
45 client.send(null)
46 })
47 </script>
Hallvord Reiar M. Steen237f53d2013-07-02 11:56:1648 <p>Note: this test will only work as expected once per browsing session. Restart browser to re-test.</p>
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1649 </body>
50</html>