blob: 00ce845b3f2923add21fd1ae40c08ad5f2923be8 [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>
James Graham234a6722013-09-17 17:54:387 <script src="/common/utils.js"></script>
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:168 <!-- These spec references do not make much sense simply because the spec doesn't say very much about this.. -->
9 <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]" />
10 <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')]/.." />
11 </head>
12 <body>
13 <div id="log"></div>
14 <script>
15 var test = async_test()
16 test.step(function() {
17 var client = new XMLHttpRequest(),
18 urlstart = location.host + location.pathname.replace(/\/[^\/]*$/, '/')
James Graham234a6722013-09-17 17:54:3819 // Initial request: no information is known to the UA about whether resources/auth4/auth.py requires authentication,
Hallvord Reiar M. Steen129c0742013-07-03 11:38:1720 // 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:1621 // request with an Authorization header before returning
22 // (Note: this test will only work as expected if run once per browsing session)
James Graham234a6722013-09-17 17:54:3823 var open_user = token()
24 client.open("GET", location.protocol+'//'+urlstart + "resources/auth4/auth.py", false, open_user, 'open-pass')
25 client.setRequestHeader('X-User', open_user)
Hallvord Reiar M. Steen237f53d2013-07-02 11:56:1626 // initial request - this will get a 401 response and re-try with HTTP auth
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1627 client.send(null)
James Graham234a6722013-09-17 17:54:3828 assert_equals(client.responseText, open_user + '\nopen-pass')
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1629 assert_equals(client.status, 200)
Hallvord Reiar M. Steen29bbd332013-07-02 11:57:5330 assert_equals(client.getResponseHeader('x-challenge'), 'DID')
Hallvord Reiar M. Steen237f53d2013-07-02 11:56:1631 // Another request, this time user,pass is omitted and an Authorization header set explicitly
32 // Here the URL is known to require authentication (from the request above), and the UA has cached open-user:open-pass credentials
33 // However, these session credentials should now be overridden by the setRequestHeader() call so the UA should immediately
34 // send basic Authorization header with credentials user:pass. (This part is perhaps not well specified anywhere)
James Graham234a6722013-09-17 17:54:3835 var user = token();
36 client.open("GET", location.protocol+'//'+urlstart + "resources/auth4/auth.py", true)
37 client.setRequestHeader("x-user", user)
38 client.setRequestHeader('Authorization', 'Basic ' + btoa(user + ":pass"))
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1639 client.onreadystatechange = function () {
Hallvord Reiar M. Steen4e96dac2013-07-03 12:42:1040 if (client.readyState < 4) {return}
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1641 test.step( function () {
James Graham234a6722013-09-17 17:54:3842 assert_equals(client.responseText, user + '\npass')
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1643 assert_equals(client.status, 200)
44 assert_equals(client.getResponseHeader('x-challenge'), 'DID-NOT')
45 test.done()
46 } )
47 }
James Graham234a6722013-09-17 17:54:3848 client.send(null)
Hallvord Reiar M. Steen4dbaf682013-05-28 09:31:1649 })
50 </script>
Hallvord Reiar M. Steen237f53d2013-07-02 11:56:1651 <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:1652 </body>
53</html>