/services/sync/tests/unit/test_resource_ua.js

http://github.com/zpao/v8monkey · JavaScript · 92 lines · 79 code · 12 blank · 1 comment · 0 complexity · ee628b4b970cef39b3ccd8decbb0635c MD5 · raw file

  1. Cu.import("resource://services-sync/constants.js");
  2. Cu.import("resource://services-sync/resource.js");
  3. Cu.import("resource://services-sync/service.js");
  4. function test_resource_user_agent() {
  5. let meta_global = new ServerWBO('global');
  6. // Tracking info/collections.
  7. let collectionsHelper = track_collections_helper();
  8. let collections = collectionsHelper.collections;
  9. let ua;
  10. function uaHandler(f) {
  11. return function(request, response) {
  12. ua = request.getHeader("User-Agent");
  13. return f(request, response);
  14. };
  15. }
  16. do_test_pending();
  17. let server = httpd_setup({
  18. "/1.1/johndoe/info/collections": uaHandler(collectionsHelper.handler),
  19. "/1.1/johndoe/storage/meta/global": uaHandler(meta_global.handler()),
  20. });
  21. Weave.Service.serverURL = "http://localhost:8080/";
  22. Weave.Service.clusterURL = "http://localhost:8080/";
  23. Weave.Service.username = "johndoe";
  24. Weave.Service.password = "ilovejane";
  25. let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
  26. " FxSync/" + WEAVE_VERSION + "." +
  27. Services.appinfo.appBuildID;
  28. function test_fetchInfo(next) {
  29. _("Testing _fetchInfo.");
  30. Weave.Service._fetchInfo();
  31. _("User-Agent: " + ua);
  32. do_check_eq(ua, expectedUA + ".desktop");
  33. ua = "";
  34. next();
  35. }
  36. function test_desktop_post(next) {
  37. _("Testing direct Resource POST.");
  38. let r = new AsyncResource("http://localhost:8080/1.1/johndoe/storage/meta/global");
  39. r.post("foo=bar", function (error, content) {
  40. _("User-Agent: " + ua);
  41. do_check_eq(ua, expectedUA + ".desktop");
  42. ua = "";
  43. next();
  44. });
  45. }
  46. function test_desktop_get(next) {
  47. _("Testing async.");
  48. Svc.Prefs.set("client.type", "desktop");
  49. let r = new AsyncResource("http://localhost:8080/1.1/johndoe/storage/meta/global");
  50. r.get(function(error, content) {
  51. _("User-Agent: " + ua);
  52. do_check_eq(ua, expectedUA + ".desktop");
  53. ua = "";
  54. next();
  55. });
  56. }
  57. function test_mobile_get(next) {
  58. _("Testing mobile.");
  59. Svc.Prefs.set("client.type", "mobile");
  60. let r = new AsyncResource("http://localhost:8080/1.1/johndoe/storage/meta/global");
  61. r.get(function (error, content) {
  62. _("User-Agent: " + ua);
  63. do_check_eq(ua, expectedUA + ".mobile");
  64. ua = "";
  65. next();
  66. });
  67. }
  68. Async.chain(
  69. test_fetchInfo,
  70. test_desktop_post,
  71. test_desktop_get,
  72. test_mobile_get,
  73. function (next) {
  74. server.stop(next);
  75. },
  76. do_test_finished)();
  77. }
  78. function run_test() {
  79. test_resource_user_agent();
  80. }