Bug 955953 - FxAccountsClient should set Accept-Language header on all server calls. r=rnewman
authorSam Penrose <spenrose@mozilla.com>
Sun, 16 Feb 2014 08:39:05 -0800
changeset 170200 e7c91056d5d5c8b077364be9380c472f92e20dcf
parent 170199 0106724dcbfd0f75610dc6d56ac3da56c80ba49f
child 170201 511de941ae39c19df848246e7238a2f199bc777c
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersrnewman
bugs955953
milestone30.0a1
Bug 955953 - FxAccountsClient should set Accept-Language header on all server calls. r=rnewman
services/common/rest.js
services/common/tests/unit/test_restrequest.js
--- a/services/common/rest.js
+++ b/services/common/rest.js
@@ -777,13 +777,23 @@ HAWKAuthenticatedRESTRequest.prototype =
       };
       let header = CryptoUtils.computeHAWK(this.uri, method, options);
       this.setHeader("Authorization", header.field);
       this._log.trace("hawk auth header: " + header.field);
     }
 
     this.setHeader("Content-Type", contentType);
 
+    try {
+      let acceptLanguage = Services.prefs.getComplexValue(
+          "intl.accept_languages", Ci.nsIPrefLocalizedString).data;
+      if (acceptLanguage) {
+        this.setHeader("Accept-Language", acceptLanguage);
+      }
+    } catch (err) {
+      this._log.error("Error reading intl.accept_languages pref: " + CommonUtils.exceptionStr(err));
+    }
+
     return RESTRequest.prototype.dispatch.call(
       this, method, data, onComplete, onProgress
     );
   }
 };
--- a/services/common/tests/unit/test_restrequest.js
+++ b/services/common/tests/unit/test_restrequest.js
@@ -848,16 +848,21 @@ add_test(function test_hawk_authenticate
   // computation with the hawk timestamp easier, since hawk throws away the
   // millisecond values.
   let then = 34329600000;
 
   let clockSkew = 120000;
   let timeOffset = -1 * clockSkew;
   let localTime = then + clockSkew;
 
+  // Set the accept-languages pref to the Nepalese dialect of Zulu.
+  let acceptLanguage = Cc['@mozilla.org/supports-string;1'].createInstance(Ci.nsISupportsString);
+  acceptLanguage.data = 'zu-NP'; // omit trailing ';', which our HTTP libs snip
+  Services.prefs.setComplexValue('intl.accept_languages', Ci.nsISupportsString, acceptLanguage);
+
   let credentials = {
     id: "eyJleHBpcmVzIjogMTM2NTAxMDg5OC4x",
     key: "qTZf4ZFpAMpMoeSsX3zVRjiqmNs=",
     algorithm: "sha256"
   };
 
   let server = httpd_setup({
     "/elysium": function(request, response) {
@@ -865,16 +870,24 @@ add_test(function test_hawk_authenticate
 
       // check that the header timestamp is our arbitrary system date, not
       // today's date.  Note that hawk header timestamps are in seconds, not
       // milliseconds.
       let authorization = request.getHeader("Authorization");
       let tsMS = parseInt(/ts="(\d+)"/.exec(authorization)[1], 10) * 1000;
       do_check_eq(tsMS, then);
 
+      // This testing can be a little wonky. In an environment where
+      //   pref("intl.accept_languages") === 'en-US, en'
+      // the header is sent as:
+      //   'en-US,en;q=0.5'
+      // hence our fake value for acceptLanguage.
+      let lang = request.getHeader('Accept-Language');
+      do_check_eq(lang, acceptLanguage);
+
       let message = "yay";
       response.setStatusLine(request.httpVersion, 200, "OK");
       response.bodyOutputStream.write(message, message.length);
     }
   });
 
   function onProgress() {
     onProgressCalled = true;
@@ -890,9 +903,14 @@ add_test(function test_hawk_authenticate
 
   let url = server.baseURI + "/elysium";
   let extra = {
     now: localTime,
     localtimeOffsetMsec: timeOffset
   };
   let request = new HAWKAuthenticatedRESTRequest(url, credentials, extra);
   request.post(postData, onComplete, onProgress);
+
+  Services.prefs.resetUserPrefs();
+  let pref = Services.prefs.getComplexValue('intl.accept_languages',
+                                            Ci.nsIPrefLocalizedString);
+  do_check_neq(acceptLanguage.data, pref.data);
 });