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