test_resource_ua.js (2650B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const { Resource } = ChromeUtils.importESModule( 5 "resource://services-sync/resource.sys.mjs" 6 ); 7 const { Service } = ChromeUtils.importESModule( 8 "resource://services-sync/service.sys.mjs" 9 ); 10 11 var httpProtocolHandler = Cc[ 12 "@mozilla.org/network/protocol;1?name=http" 13 ].getService(Ci.nsIHttpProtocolHandler); 14 15 // Tracking info/collections. 16 var collectionsHelper = track_collections_helper(); 17 18 var meta_global; 19 var server; 20 21 var expectedUA; 22 var ua; 23 function uaHandler(f) { 24 return function (request, response) { 25 ua = request.getHeader("User-Agent"); 26 return f(request, response); 27 }; 28 } 29 30 add_task(async function setup() { 31 Log.repository.rootLogger.addAppender(new Log.DumpAppender()); 32 meta_global = new ServerWBO("global"); 33 server = httpd_setup({ 34 "/1.1/johndoe/info/collections": uaHandler(collectionsHelper.handler), 35 "/1.1/johndoe/storage/meta/global": uaHandler(meta_global.handler()), 36 }); 37 38 await configureIdentity({ username: "johndoe" }, server); 39 _("Server URL: " + server.baseURI); 40 41 // Note this string is missing the trailing ".destkop" as the test 42 // adjusts the "client.type" pref where that portion comes from. 43 expectedUA = 44 Services.appinfo.name + 45 "/" + 46 Services.appinfo.version + 47 " (" + 48 httpProtocolHandler.oscpu + 49 ")" + 50 " FxSync/" + 51 WEAVE_VERSION + 52 "." + 53 Services.appinfo.appBuildID; 54 }); 55 56 add_task(async function test_fetchInfo() { 57 _("Testing _fetchInfo."); 58 await Service.login(); 59 await Service._fetchInfo(); 60 _("User-Agent: " + ua); 61 Assert.equal(ua, expectedUA + ".desktop"); 62 ua = ""; 63 }); 64 65 add_task(async function test_desktop_post() { 66 _("Testing direct Resource POST."); 67 let r = new Resource(server.baseURI + "/1.1/johndoe/storage/meta/global"); 68 await r.post("foo=bar"); 69 _("User-Agent: " + ua); 70 Assert.equal(ua, expectedUA + ".desktop"); 71 ua = ""; 72 }); 73 74 add_task(async function test_desktop_get() { 75 _("Testing async."); 76 Svc.PrefBranch.setStringPref("client.type", "desktop"); 77 let r = new Resource(server.baseURI + "/1.1/johndoe/storage/meta/global"); 78 await r.get(); 79 _("User-Agent: " + ua); 80 Assert.equal(ua, expectedUA + ".desktop"); 81 ua = ""; 82 }); 83 84 add_task(async function test_mobile_get() { 85 _("Testing mobile."); 86 Svc.PrefBranch.setStringPref("client.type", "mobile"); 87 let r = new Resource(server.baseURI + "/1.1/johndoe/storage/meta/global"); 88 await r.get(); 89 _("User-Agent: " + ua); 90 Assert.equal(ua, expectedUA + ".mobile"); 91 ua = ""; 92 }); 93 94 add_test(function tear_down() { 95 server.stop(run_next_test); 96 });