test_standardurl_default_port.js (2023B)
1 /* -*- Mode: javascript; indent-tabs-mode: nil; js-indent-level: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* This test exercises the nsIStandardURL "setDefaultPort" API. */ 8 9 "use strict"; 10 11 function run_test() { 12 function stringToURL(str) { 13 return Cc["@mozilla.org/network/standard-url-mutator;1"] 14 .createInstance(Ci.nsIStandardURLMutator) 15 .init(Ci.nsIStandardURL.URLTYPE_AUTHORITY, 80, str, "UTF-8", null) 16 .finalize() 17 .QueryInterface(Ci.nsIStandardURL); 18 } 19 20 // Create a nsStandardURL: 21 var origUrlStr = "http://foo.com/"; 22 var stdUrl = stringToURL(origUrlStr); 23 Assert.equal(-1, stdUrl.port); 24 25 // Changing default port shouldn't adjust the value returned by "port", 26 // or the string representation. 27 let def100Url = stdUrl 28 .mutate() 29 .QueryInterface(Ci.nsIStandardURLMutator) 30 .setDefaultPort(100) 31 .finalize(); 32 Assert.equal(-1, def100Url.port); 33 Assert.equal(def100Url.spec, origUrlStr); 34 35 // Changing port directly should update .port and .spec, though: 36 let port200Url = stdUrl.mutate().setPort("200").finalize(); 37 Assert.equal(200, port200Url.port); 38 Assert.equal(port200Url.spec, "http://foo.com:200/"); 39 40 // ...but then if we change default port to match the custom port, 41 // the custom port should reset to -1 and disappear from .spec: 42 let def200Url = port200Url 43 .mutate() 44 .QueryInterface(Ci.nsIStandardURLMutator) 45 .setDefaultPort(200) 46 .finalize(); 47 Assert.equal(-1, def200Url.port); 48 Assert.equal(def200Url.spec, origUrlStr); 49 50 // And further changes to default port should not make custom port reappear. 51 let def300Url = def200Url 52 .mutate() 53 .QueryInterface(Ci.nsIStandardURLMutator) 54 .setDefaultPort(300) 55 .finalize(); 56 Assert.equal(-1, def300Url.port); 57 Assert.equal(def300Url.spec, origUrlStr); 58 }