Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | // |
michael@0 | 2 | // HTTP Accept-Language header test |
michael@0 | 3 | // |
michael@0 | 4 | |
michael@0 | 5 | var testpath = "/bug672448"; |
michael@0 | 6 | |
michael@0 | 7 | function run_test() { |
michael@0 | 8 | let intlPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("intl."); |
michael@0 | 9 | |
michael@0 | 10 | // Save old value of preference for later. |
michael@0 | 11 | let oldPref = intlPrefs.getCharPref("accept_languages"); |
michael@0 | 12 | |
michael@0 | 13 | // Test different numbers of languages, to test different fractions. |
michael@0 | 14 | let acceptLangTests = [ |
michael@0 | 15 | "qaa", // 1 |
michael@0 | 16 | "qaa,qab", // 2 |
michael@0 | 17 | "qaa,qab,qac,qad", // 4 |
michael@0 | 18 | "qaa,qab,qac,qad,qae,qaf,qag,qah", // 8 |
michael@0 | 19 | "qaa,qab,qac,qad,qae,qaf,qag,qah,qai,qaj", // 10 |
michael@0 | 20 | "qaa,qab,qac,qad,qae,qaf,qag,qah,qai,qaj,qak", // 11 |
michael@0 | 21 | "qaa,qab,qac,qad,qae,qaf,qag,qah,qai,qaj,qak,qal,qam,qan,qao,qap,qaq,qar,qas,qat,qau", // 21 |
michael@0 | 22 | oldPref, // Restore old value of preference (and test it). |
michael@0 | 23 | ]; |
michael@0 | 24 | |
michael@0 | 25 | let acceptLangTestsNum = acceptLangTests.length; |
michael@0 | 26 | |
michael@0 | 27 | for (let i = 0; i < acceptLangTestsNum; i++) { |
michael@0 | 28 | // Set preference to test value. |
michael@0 | 29 | intlPrefs.setCharPref("accept_languages", acceptLangTests[i]); |
michael@0 | 30 | |
michael@0 | 31 | // Test value. |
michael@0 | 32 | test_accepted_languages(); |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | function test_accepted_languages() { |
michael@0 | 37 | let channel = setupChannel(testpath); |
michael@0 | 38 | |
michael@0 | 39 | let AcceptLanguage = channel.getRequestHeader("Accept-Language"); |
michael@0 | 40 | |
michael@0 | 41 | let acceptedLanguages = AcceptLanguage.split(","); |
michael@0 | 42 | |
michael@0 | 43 | let acceptedLanguagesLength = acceptedLanguages.length; |
michael@0 | 44 | |
michael@0 | 45 | for (let i = 0; i < acceptedLanguagesLength; i++) { |
michael@0 | 46 | let acceptedLanguage, qualityValue; |
michael@0 | 47 | |
michael@0 | 48 | try { |
michael@0 | 49 | // The q-value must conform to the definition in HTTP/1.1 Section 3.9. |
michael@0 | 50 | [_, acceptedLanguage, qualityValue] = acceptedLanguages[i].trim().match(/^([a-z0-9_-]*?)(?:;q=(1(?:\.0{0,3})?|0(?:\.[0-9]{0,3})))?$/i); |
michael@0 | 51 | } catch(e) { |
michael@0 | 52 | do_throw("Invalid language tag or quality value: " + e); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | if (i == 0) { |
michael@0 | 56 | // The first language shouldn't have a quality value. |
michael@0 | 57 | do_check_eq(qualityValue, undefined); |
michael@0 | 58 | } else { |
michael@0 | 59 | let decimalPlaces; |
michael@0 | 60 | |
michael@0 | 61 | // When the number of languages is small, we keep the quality value to only one decimal place. |
michael@0 | 62 | // Otherwise, it can be up to two decimal places. |
michael@0 | 63 | if (acceptedLanguagesLength < 10) { |
michael@0 | 64 | do_check_true(qualityValue.length == 3); |
michael@0 | 65 | |
michael@0 | 66 | decimalPlaces = 1; |
michael@0 | 67 | } else { |
michael@0 | 68 | do_check_true(qualityValue.length >= 3); |
michael@0 | 69 | do_check_true(qualityValue.length <= 4); |
michael@0 | 70 | |
michael@0 | 71 | decimalPlaces = 2; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | // All the other languages should have an evenly-spaced quality value. |
michael@0 | 75 | do_check_eq(parseFloat(qualityValue).toFixed(decimalPlaces), (1.0 - ((1 / acceptedLanguagesLength) * i)).toFixed(decimalPlaces)); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | function setupChannel(path) { |
michael@0 | 81 | let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); |
michael@0 | 82 | let chan = ios.newChannel("http://localhost:4444" + path, "", null); |
michael@0 | 83 | chan.QueryInterface(Ci.nsIHttpChannel); |
michael@0 | 84 | return chan; |
michael@0 | 85 | } |