| 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> |
| 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. Steen | 237f53d | 2013-07-02 11:56:16 | [diff] [blame] | 18 | // Initial request: no information is known to the UA about whether resources/auth4/auth.php requires authentication, |
| Hallvord Reiar M. Steen | 129c074 | 2013-07-03 11:38:17 | [diff] [blame] | 19 | // 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] | 20 | // 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. Steen | 129c074 | 2013-07-03 11:38:17 | [diff] [blame] | 22 | client.open("GET", location.protocol+'//'+urlstart + "resources/auth4/auth.php", false, 'open-user', 'open-pass') |
| Hallvord Reiar M. Steen | 237f53d | 2013-07-02 11:56:16 | [diff] [blame] | 23 | // 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] | 24 | client.send(null) |
| 25 | assert_equals(client.responseText, 'open-user\nopen-pass') |
| 26 | assert_equals(client.status, 200) |
| Hallvord Reiar M. Steen | 29bbd33 | 2013-07-02 11:57:53 | [diff] [blame] | 27 | assert_equals(client.getResponseHeader('x-challenge'), 'DID') |
| Hallvord Reiar M. Steen | 237f53d | 2013-07-02 11:56:16 | [diff] [blame] | 28 | // 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. Steen | 4e96dac | 2013-07-03 12:42:10 | [diff] [blame] | 32 | client.open("GET", location.protocol+'//'+urlstart + "resources/auth4/auth.php", true) |
| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 33 | client.setRequestHeader("x-user", 'user') |
| 34 | client.setRequestHeader("x-pass", 'pass') |
| 35 | client.setRequestHeader('Authorization', 'Basic dXNlcjpwYXNz') |
| 36 | client.onreadystatechange = function () { |
| Hallvord Reiar M. Steen | 4e96dac | 2013-07-03 12:42:10 | [diff] [blame] | 37 | if (client.readyState < 4) {return} |
| Hallvord Reiar M. Steen | 4dbaf68 | 2013-05-28 09:31:16 | [diff] [blame] | 38 | 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. Steen | 237f53d | 2013-07-02 11:56:16 | [diff] [blame] | 48 | <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] | 49 | </body> |
| 50 | </html> |