summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/ShortUrl.test.js
blob: e0f6688db8e929bfccb124bcc83296a5896c19af (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
94
95
96
97
98
99
100
101
102
103
104
import { GlobalOverrider } from "test/unit/utils";
import { shortURL } from "lib/ShortURL.jsm";

const puny = "xn--kpry57d";
const idn = "台灣";

describe("shortURL", () => {
  let globals;
  let IDNStub;
  let getPublicSuffixFromHostStub;

  beforeEach(() => {
    IDNStub = sinon.stub().callsFake(host => host.replace(puny, idn));
    getPublicSuffixFromHostStub = sinon.stub().returns("com");

    globals = new GlobalOverrider();
    globals.set("IDNService", { convertToDisplayIDN: IDNStub });
    globals.set("Services", {
      eTLD: { getPublicSuffixFromHost: getPublicSuffixFromHostStub },
    });
  });

  afterEach(() => {
    globals.restore();
  });

  it("should return a blank string if url is falsey", () => {
    assert.equal(shortURL({ url: false }), "");
    assert.equal(shortURL({ url: "" }), "");
    assert.equal(shortURL({}), "");
  });

  it("should return the 'url' if not a valid url", () => {
    const checkInvalid = url => assert.equal(shortURL({ url }), url);
    checkInvalid(true);
    checkInvalid("something");
    checkInvalid("http:");
    checkInvalid("http::double");
    checkInvalid("http://badport:65536/");
  });

  it("should remove the eTLD", () => {
    assert.equal(shortURL({ url: "http://com.blah.com" }), "com.blah");
  });

  it("should convert host to idn when calling shortURL", () => {
    assert.equal(shortURL({ url: `http://${puny}.blah.com` }), `${idn}.blah`);
  });

  it("should get the hostname from .url", () => {
    assert.equal(shortURL({ url: "http://bar.com" }), "bar");
  });

  it("should not strip out www if not first subdomain", () => {
    assert.equal(shortURL({ url: "http://foo.www.com" }), "foo.www");
  });

  it("should convert to lowercase", () => {
    assert.equal(shortURL({ url: "HTTP://FOO.COM" }), "foo");
  });

  it("should not include the port", () => {
    assert.equal(shortURL({ url: "http://foo.com:8888" }), "foo");
  });

  it("should return hostname for localhost", () => {
    getPublicSuffixFromHostStub.throws("insufficient domain levels");

    assert.equal(shortURL({ url: "http://localhost:8000/" }), "localhost");
  });

  it("should return hostname for ip address", () => {
    getPublicSuffixFromHostStub.throws("host is ip address");

    assert.equal(shortURL({ url: "http://127.0.0.1/foo" }), "127.0.0.1");
  });

  it("should return etld for www.gov.uk (www-only non-etld)", () => {
    getPublicSuffixFromHostStub.returns("gov.uk");

    assert.equal(
      shortURL({ url: "https://www.gov.uk/countersigning" }),
      "gov.uk"
    );
  });

  it("should return idn etld for www-only non-etld", () => {
    getPublicSuffixFromHostStub.returns(puny);

    assert.equal(shortURL({ url: `https://www.${puny}/foo` }), idn);
  });

  it("should return not the protocol for file:", () => {
    assert.equal(shortURL({ url: "file:///foo/bar.txt" }), "/foo/bar.txt");
  });

  it("should return not the protocol for about:", () => {
    assert.equal(shortURL({ url: "about:newtab" }), "newtab");
  });

  it("should fall back to full url as a last resort", () => {
    assert.equal(shortURL({ url: "about:" }), "about:");
  });
});