Andrew Top | 61a8495 | 2019-04-30 15:07:33 -0700 | [diff] [blame] | 1 | <!doctype html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>XMLHttpRequest: send() - "Basic" authenticated requests with competing user name/password options</title> |
| 5 | <script src="/resources/testharness.js"></script> |
| 6 | <script src="/resources/testharnessreport.js"></script> |
| 7 | <script src="/common/utils.js"></script> |
| 8 | <link rel="help" href="https://xhr.spec.whatwg.org/#the-open()-method" data-tested-assertations="following::ol[1]/li[9]/ol[1]/li[1] following::ol[1]/li[9]/ol[1]/li[2]" /> |
| 9 | <link rel="help" href="https://xhr.spec.whatwg.org/#the-send()-method" data-tested-assertations="following::code[contains(@title,'http-authorization')]/.." /> </head> |
| 10 | <body> |
| 11 | <div id="log"></div> |
| 12 | <script> |
| 13 | function request(user1, pass1, user2, pass2, name) { |
| 14 | // user1, pass1 will if given become userinfo part of URL |
| 15 | // user2, pass2 will if given be passed to open() call |
| 16 | test(function() { |
| 17 | var client = new XMLHttpRequest(), |
| 18 | urlstart = "", userwin, passwin |
| 19 | // if user2 is set, winning user name and password is 2 |
| 20 | if(user2) |
| 21 | userwin = user2, passwin = pass2 |
| 22 | // if user1 is set, and user2 is not set, user1 and pass1 win |
| 23 | if(user1 && ! user2) |
| 24 | userwin = user1, passwin = pass1 |
| 25 | // if neither user name is set, pass 2 wins (there will be no userinfo in URL) |
| 26 | if (!(user1 || user2)) |
| 27 | passwin = pass2 |
| 28 | if(user1) { // should add userinfo to URL (there is no way to create userinfo part of URL with only password in) |
| 29 | urlstart = "http://" + user1 |
| 30 | if(pass1) |
| 31 | urlstart += ":" + pass1 |
| 32 | urlstart += "@" + location.host + location.pathname.replace(/\/[^\/]*$/, '/') |
| 33 | } |
| 34 | client.open("GET", urlstart + "resources/authentication.py", false, user2, pass2) |
| 35 | client.setRequestHeader("x-user", userwin) |
| 36 | client.send(null) |
| 37 | assert_true(client.responseText == ((userwin||'') + "\n" + (passwin||'')), 'responseText should contain the right user and password') |
| 38 | |
| 39 | // We want to send multiple requests to the same realm here, so we try to make the UA forget its (cached) credentials between each test.. |
| 40 | // forcing a 401 response to (hopefully) "log out" |
| 41 | // NOTE: This is commented out because it causes authentication prompts while running the test |
| 42 | //client.open('GET', "resources/authentication.py?logout=1", false) |
| 43 | //client.send() |
| 44 | }, document.title+' '+name) |
| 45 | } |
| 46 | request(null, null, token(), token(), 'user/pass in open() call') |
| 47 | request(null, null, token(), token(), 'another user/pass in open() call - must override cached credentials from previous test') |
| 48 | request("userinfo-user", "userinfo-pass", token(), token(), 'user/pass both in URL userinfo AND open() call - expexted that open() wins') |
| 49 | request(token(), token(), null, null, 'user/pass *only* in URL userinfo') |
| 50 | request(token(), null, null, token(), 'user name in URL userinfo, password in open() call: user name wins and password is thrown away') |
| 51 | request("1", token(), token(), null, 'user name and password in URL userinfo, only user name in open() call: user name in open() wins') |
| 52 | </script> |
| 53 | </body> |
| 54 | </html> |