ShortUrl.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {GlobalOverrider} from "test/unit/utils";
  2. import {shortURL} from "lib/ShortURL.jsm";
  3. const puny = "xn--kpry57d";
  4. const idn = "台灣";
  5. describe("shortURL", () => {
  6. let globals;
  7. let IDNStub;
  8. let getPublicSuffixFromHostStub;
  9. beforeEach(() => {
  10. IDNStub = sinon.stub().callsFake(host => host.replace(puny, idn));
  11. getPublicSuffixFromHostStub = sinon.stub().returns("com");
  12. globals = new GlobalOverrider();
  13. globals.set("IDNService", {convertToDisplayIDN: IDNStub});
  14. globals.set("Services", {eTLD: {getPublicSuffixFromHost: getPublicSuffixFromHostStub}});
  15. });
  16. afterEach(() => {
  17. globals.restore();
  18. });
  19. it("should return a blank string if url is falsey", () => {
  20. assert.equal(shortURL({url: false}), "");
  21. assert.equal(shortURL({url: ""}), "");
  22. assert.equal(shortURL({}), "");
  23. });
  24. it("should return the 'url' if not a valid url", () => {
  25. const checkInvalid = url => assert.equal(shortURL({url}), url);
  26. checkInvalid(true);
  27. checkInvalid("something");
  28. checkInvalid("http:");
  29. checkInvalid("http::double");
  30. checkInvalid("http://badport:65536/");
  31. });
  32. it("should remove the eTLD", () => {
  33. assert.equal(shortURL({url: "http://com.blah.com"}), "com.blah");
  34. });
  35. it("should convert host to idn when calling shortURL", () => {
  36. assert.equal(shortURL({url: `http://${puny}.blah.com`}), `${idn}.blah`);
  37. });
  38. it("should get the hostname from .url", () => {
  39. assert.equal(shortURL({url: "http://bar.com"}), "bar");
  40. });
  41. it("should not strip out www if not first subdomain", () => {
  42. assert.equal(shortURL({url: "http://foo.www.com"}), "foo.www");
  43. });
  44. it("should convert to lowercase", () => {
  45. assert.equal(shortURL({url: "HTTP://FOO.COM"}), "foo");
  46. });
  47. it("should not include the port", () => {
  48. assert.equal(shortURL({url: "http://foo.com:8888"}), "foo");
  49. });
  50. it("should return hostname for localhost", () => {
  51. getPublicSuffixFromHostStub.throws("insufficient domain levels");
  52. assert.equal(shortURL({url: "http://localhost:8000/"}), "localhost");
  53. });
  54. it("should return hostname for ip address", () => {
  55. getPublicSuffixFromHostStub.throws("host is ip address");
  56. assert.equal(shortURL({url: "http://127.0.0.1/foo"}), "127.0.0.1");
  57. });
  58. it("should return etld for www.gov.uk (www-only non-etld)", () => {
  59. getPublicSuffixFromHostStub.returns("gov.uk");
  60. assert.equal(shortURL({url: "https://www.gov.uk/countersigning"}), "gov.uk");
  61. });
  62. it("should return idn etld for www-only non-etld", () => {
  63. getPublicSuffixFromHostStub.returns(puny);
  64. assert.equal(shortURL({url: `https://www.${puny}/foo`}), idn);
  65. });
  66. it("should return not the protocol for file:", () => {
  67. assert.equal(shortURL({url: "file:///foo/bar.txt"}), "/foo/bar.txt");
  68. });
  69. it("should return not the protocol for about:", () => {
  70. assert.equal(shortURL({url: "about:newtab"}), "newtab");
  71. });
  72. it("should fall back to full url as a last resort", () => {
  73. assert.equal(shortURL({url: "about:"}), "about:");
  74. });
  75. });