From 37c0b16f993e95bb8fd2f42bc2515fd132a22639 Mon Sep 17 00:00:00 2001 From: Kershaw Chang Date: Tue, 7 Aug 2018 06:00:00 +0300 Subject: [PATCH] Bug 1470458 - Test case, r=valentin Test steps: 1. Create a XHR 2. Initializes the XHR with non-ascii username and password 3. Send the request to server 4. Check if the server receives the correct Authorization header --- netwerk/test/unit/test_authentication.js | 56 +++++++++++++++++------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/netwerk/test/unit/test_authentication.js b/netwerk/test/unit/test_authentication.js index 4f1c990adfcc..97192cd6b511 100644 --- a/netwerk/test/unit/test_authentication.js +++ b/netwerk/test/unit/test_authentication.js @@ -4,6 +4,8 @@ ChromeUtils.import("resource://testing-common/httpd.js"); ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); +Cu.importGlobalProperties(["XMLHttpRequest"]); + // Turn off the authentication dialog blocking for this test. var prefs = Cc["@mozilla.org/preferences-service;1"]. getService(Ci.nsIPrefBranch); @@ -283,20 +285,7 @@ var listener = { onStopRequest: function test_onStopR(request, ctx, status) { Assert.equal(status, Cr.NS_ERROR_ABORT); - if (current_test < (tests.length - 1)) { - // First, gotta clear the auth cache - Cc["@mozilla.org/network/http-auth-manager;1"] - .getService(Ci.nsIHttpAuthManager) - .clearAll(); - - current_test++; - tests[current_test](); - } else { - do_test_pending(); - httpserv.stop(do_test_finished); - } - - do_test_finished(); + moveToNextTest(); } }; @@ -318,12 +307,29 @@ var tests = [test_noauth, test_returnfalse1, test_wrongpw1, test_prompt1, test_returnfalse2, test_wrongpw2, test_prompt2, test_ntlm, test_basicrealm, test_nonascii, test_digest_noauth, test_digest, test_digest_bogus_user, test_short_digest, test_large_realm, - test_large_domain]; + test_large_domain, test_nonascii_xhr]; var current_test = 0; var httpserv = null; +function moveToNextTest() { + if (current_test < (tests.length - 1)) { + // First, gotta clear the auth cache + Cc["@mozilla.org/network/http-auth-manager;1"] + .getService(Ci.nsIHttpAuthManager) + .clearAll(); + + current_test++; + tests[current_test](); + } else { + do_test_pending(); + httpserv.stop(do_test_finished); + } + + do_test_finished(); +} + function run_test() { httpserv = new HttpServer(); @@ -460,6 +466,21 @@ function test_nonascii() { do_test_pending(); } +function test_nonascii_xhr() { + var xhr = new XMLHttpRequest(); + xhr.open("GET", URL + "/auth/non_ascii", true, "é", "é"); + xhr.onreadystatechange = function(event) { + if (xhr.readyState == 4) { + Assert.equal(xhr.status, 200); + moveToNextTest(); + xhr.onreadystatechange = null; + } + }; + xhr.send(null); + + do_test_pending(); +} + function test_digest_noauth() { var chan = makeChan(URL + "/auth/digest", URL); @@ -561,7 +582,8 @@ function authNonascii(metadata, response) { response.setStatusLine(metadata.httpVersion, 200, "OK, authorized"); response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false); - body = "success"; + // Use correct XML syntax since this function is also used for testing XHR. + body = "success"; } else { @@ -569,7 +591,7 @@ function authNonascii(metadata, response) { response.setStatusLine(metadata.httpVersion, 401, "Unauthorized"); response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false); - body = "failed"; + body = "failed"; } response.bodyOutputStream.write(body, body.length);