summaryrefslogtreecommitdiff
path: root/services/sync/tests/unit/test_resource_ua.js
blob: 3211c8ef56e4f9f6f391f8cc6a1bce1b4aaffcd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

Cu.import("resource://services-sync/constants.js");
Cu.import("resource://services-sync/resource.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");
Cu.import("resource://testing-common/services/sync/utils.js");

const TEST_GET_URL = "http://localhost:8080/1.1/johndoe/storage/meta/global";

// Tracking info/collections.
let collectionsHelper = track_collections_helper();
let collections = collectionsHelper.collections;

let meta_global;
let server;

let expectedUA;
let ua;
function uaHandler(f) {
  return function(request, response) {
    ua = request.getHeader("User-Agent");
    return f(request, response);
  };
}

function run_test() {
  meta_global = new ServerWBO('global');
  server = httpd_setup({
    "/1.1/johndoe/info/collections": uaHandler(collectionsHelper.handler),
    "/1.1/johndoe/storage/meta/global": uaHandler(meta_global.handler()),
  });

  setBasicCredentials("johndoe", "ilovejane");
  Service.serverURL  = TEST_SERVER_URL;
  Service.clusterURL = TEST_CLUSTER_URL;

  expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
               " FxSync/" + WEAVE_VERSION + "." +
               Services.appinfo.appBuildID;

  run_next_test();
}

add_test(function test_fetchInfo() {
  _("Testing _fetchInfo.");
  Service._fetchInfo();
  _("User-Agent: " + ua);
  do_check_eq(ua, expectedUA + ".desktop");
  ua = "";
  run_next_test();
});

add_test(function test_desktop_post() {
  _("Testing direct Resource POST.");
  let r = new AsyncResource(TEST_GET_URL);
  r.post("foo=bar", function (error, content) {
    _("User-Agent: " + ua);
    do_check_eq(ua, expectedUA + ".desktop");
    ua = "";
    run_next_test();
  });
});

add_test(function test_desktop_get() {
  _("Testing async.");
  Svc.Prefs.set("client.type", "desktop");
  let r = new AsyncResource(TEST_GET_URL);
  r.get(function(error, content) {
    _("User-Agent: " + ua);
    do_check_eq(ua, expectedUA + ".desktop");
    ua = "";
    run_next_test();
  });
});

add_test(function test_mobile_get() {
  _("Testing mobile.");
  Svc.Prefs.set("client.type", "mobile");
  let r = new AsyncResource(TEST_GET_URL);
  r.get(function (error, content) {
    _("User-Agent: " + ua);
    do_check_eq(ua, expectedUA + ".mobile");
    ua = "";
    run_next_test();
  });
});

add_test(function tear_down() {
  server.stop(run_next_test);
});