1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/unit/test_header_Accept-Language.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +// 1.5 +// HTTP Accept-Language header test 1.6 +// 1.7 + 1.8 +var testpath = "/bug672448"; 1.9 + 1.10 +function run_test() { 1.11 + let intlPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("intl."); 1.12 + 1.13 + // Save old value of preference for later. 1.14 + let oldPref = intlPrefs.getCharPref("accept_languages"); 1.15 + 1.16 + // Test different numbers of languages, to test different fractions. 1.17 + let acceptLangTests = [ 1.18 + "qaa", // 1 1.19 + "qaa,qab", // 2 1.20 + "qaa,qab,qac,qad", // 4 1.21 + "qaa,qab,qac,qad,qae,qaf,qag,qah", // 8 1.22 + "qaa,qab,qac,qad,qae,qaf,qag,qah,qai,qaj", // 10 1.23 + "qaa,qab,qac,qad,qae,qaf,qag,qah,qai,qaj,qak", // 11 1.24 + "qaa,qab,qac,qad,qae,qaf,qag,qah,qai,qaj,qak,qal,qam,qan,qao,qap,qaq,qar,qas,qat,qau", // 21 1.25 + oldPref, // Restore old value of preference (and test it). 1.26 + ]; 1.27 + 1.28 + let acceptLangTestsNum = acceptLangTests.length; 1.29 + 1.30 + for (let i = 0; i < acceptLangTestsNum; i++) { 1.31 + // Set preference to test value. 1.32 + intlPrefs.setCharPref("accept_languages", acceptLangTests[i]); 1.33 + 1.34 + // Test value. 1.35 + test_accepted_languages(); 1.36 + } 1.37 +} 1.38 + 1.39 +function test_accepted_languages() { 1.40 + let channel = setupChannel(testpath); 1.41 + 1.42 + let AcceptLanguage = channel.getRequestHeader("Accept-Language"); 1.43 + 1.44 + let acceptedLanguages = AcceptLanguage.split(","); 1.45 + 1.46 + let acceptedLanguagesLength = acceptedLanguages.length; 1.47 + 1.48 + for (let i = 0; i < acceptedLanguagesLength; i++) { 1.49 + let acceptedLanguage, qualityValue; 1.50 + 1.51 + try { 1.52 + // The q-value must conform to the definition in HTTP/1.1 Section 3.9. 1.53 + [_, acceptedLanguage, qualityValue] = acceptedLanguages[i].trim().match(/^([a-z0-9_-]*?)(?:;q=(1(?:\.0{0,3})?|0(?:\.[0-9]{0,3})))?$/i); 1.54 + } catch(e) { 1.55 + do_throw("Invalid language tag or quality value: " + e); 1.56 + } 1.57 + 1.58 + if (i == 0) { 1.59 + // The first language shouldn't have a quality value. 1.60 + do_check_eq(qualityValue, undefined); 1.61 + } else { 1.62 + let decimalPlaces; 1.63 + 1.64 + // When the number of languages is small, we keep the quality value to only one decimal place. 1.65 + // Otherwise, it can be up to two decimal places. 1.66 + if (acceptedLanguagesLength < 10) { 1.67 + do_check_true(qualityValue.length == 3); 1.68 + 1.69 + decimalPlaces = 1; 1.70 + } else { 1.71 + do_check_true(qualityValue.length >= 3); 1.72 + do_check_true(qualityValue.length <= 4); 1.73 + 1.74 + decimalPlaces = 2; 1.75 + } 1.76 + 1.77 + // All the other languages should have an evenly-spaced quality value. 1.78 + do_check_eq(parseFloat(qualityValue).toFixed(decimalPlaces), (1.0 - ((1 / acceptedLanguagesLength) * i)).toFixed(decimalPlaces)); 1.79 + } 1.80 + } 1.81 +} 1.82 + 1.83 +function setupChannel(path) { 1.84 + let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); 1.85 + let chan = ios.newChannel("http://localhost:4444" + path, "", null); 1.86 + chan.QueryInterface(Ci.nsIHttpChannel); 1.87 + return chan; 1.88 +}