| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 1 | <!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 Graham | 234a672 | 2013-09-17 17:54:38 | [diff] [blame^] | 7 | <script src="/common/utils.js"></script> |
| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 8 | <!-- 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 Graham | 234a672 | 2013-09-17 17:54:38 | [diff] [blame^] | 19 | // Initial request: no information is known to the UA about whether resources/auth4/auth.py requires authentication, |
| Hallvord Reiar M. Steen | 129c074 | 2013-07-03 11:38:17 | [diff] [blame] | 20 | // 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. Steen | 237f53d | 2013-07-02 11:56:16 | [diff] [blame] | 21 | // request with an Authorization header before returning |
| 22 | // (Note: this test will only work as expected if run once per browsing session) |
| James Graham | 234a672 | 2013-09-17 17:54:38 | [diff] [blame^] | 23 | 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. Steen | 237f53d | 2013-07-02 11:56:16 | [diff] [blame] | 26 | // initial request - this will get a 401 response and re-try with HTTP auth |
| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 27 | client.send(null) |
| James Graham | 234a672 | 2013-09-17 17:54:38 | [diff] [blame^] | 28 | assert_equals(client.responseText, open_user + '\nopen-pass') |
| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 29 | assert_equals(client.status, 200) |
| Hallvord Reiar M. Steen | 29bbd33 | 2013-07-02 11:57:53 | [diff] [blame] | 30 | assert_equals(client.getResponseHeader('x-challenge'), 'DID') |
| Hallvord Reiar M. Steen | 237f53d | 2013-07-02 11:56:16 | [diff] [blame] | 31 | // 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 Graham | 234a672 | 2013-09-17 17:54:38 | [diff] [blame^] | 35 | 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. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 39 | client.onreadystatechange = function () { |
| Hallvord Reiar M. Steen | 4e96dac | 2013-07-03 12:42:10 | [diff] [blame] | 40 | if (client.readyState < 4) {return} |
| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 41 | test.step( function () { |
| James Graham | 234a672 | 2013-09-17 17:54:38 | [diff] [blame^] | 42 | assert_equals(client.responseText, user + '\npass') |
| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 43 | assert_equals(client.status, 200) |
| 44 | assert_equals(client.getResponseHeader('x-challenge'), 'DID-NOT') |
| 45 | test.done() |
| 46 | } ) |
| 47 | } |
| James Graham | 234a672 | 2013-09-17 17:54:38 | [diff] [blame^] | 48 | client.send(null) |
| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 49 | }) |
| 50 | </script> |
| Hallvord Reiar M. Steen | 237f53d | 2013-07-02 11:56:16 | [diff] [blame] | 51 | <p>Note: this test will only work as expected once per browsing session. Restart browser to re-test.</p> |
| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 52 | </body> |
| 53 | </html> |