PageRenderTime 21ms CodeModel.GetById 15ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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