aboutsummaryrefslogtreecommitdiffstats
path: root/test/helpers/url-helpers.test.ts
blob: 9ef06b905c02c4f4fe9c259c8a43ea80c56ebb91 (plain) (blame)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as url_helpers from '../../src/helpers/url-helpers';

describe('url_helpers', () => {
  describe('isValidExternalURL', () => {
    describe('with string', () => {
      it('returns false for empty string', () => {
        const result = url_helpers.isValidExternalURL('');
        expect(result).toBe(false);
      });

      it('returns false for whitespace string', () => {
        const result = url_helpers.isValidExternalURL('  ');
        expect(result).toBe(false);
      });

      it('returns false for random string', () => {
        const result = url_helpers.isValidExternalURL('some random string');
        expect(result).toBe(false);
      });

      it('returns false for invalid url', () => {
        const result = url_helpers.isValidExternalURL('shttps://google');
        expect(result).toBe(false);
      });

      it('returns true for valid http url', () => {
        const result = url_helpers.isValidExternalURL('http://google');
        expect(result).toBe(true);
      });

      it('returns true for valid https url', () => {
        const result = url_helpers.isValidExternalURL('https://google');
        expect(result).toBe(true);
      });
    });

    describe('with URL', () => {
      // Note: not testing the invalid string urls - since the URL ctor itself will raise an error

      it('returns false for invalid url', () => {
        const result = url_helpers.isValidExternalURL(
          new URL('shttps://google'),
        );
        expect(result).toBe(false);
      });

      it('returns true for valid http url', () => {
        const result = url_helpers.isValidExternalURL(new URL('http://google'));
        expect(result).toBe(true);
      });

      it('returns true for valid https url', () => {
        const result = url_helpers.isValidExternalURL(
          new URL('https://google'),
        );
        expect(result).toBe(true);
      });
    });
  });

  describe('fixUrl', () => {
    it('handles with empty string', () => {
      const result = url_helpers.fixUrl('');
      expect(result).toEqual('');
    });

    it('handles with whitespace string', () => {
      const result = url_helpers.fixUrl('   ');
      expect(result).toEqual('   ');
    });

    it('handles with random string', () => {
      const result = url_helpers.fixUrl('some random string');
      expect(result).toEqual('some random string');
    });

    it('handles string starting with http://', () => {
      expect(url_helpers.fixUrl('http://some/random/url')).toEqual(
        'http://some/random/url',
      );
      expect(url_helpers.fixUrl('http://some//random//url')).toEqual(
        'http://some/random/url',
      );

      const gmailEmbeddedUrl =
        'https://www.google.com/url?q=https://github.com/ferdium/ferdium-app/issues/87&source=gmail';
      expect(url_helpers.fixUrl(gmailEmbeddedUrl)).toEqual(gmailEmbeddedUrl); // it should NOT remove the double-slash from the embedded url in the query string
    });

    it('handles string starting with https://', () => {
      expect(url_helpers.fixUrl('https://some/random/url')).toEqual(
        'https://some/random/url',
      );
      expect(url_helpers.fixUrl('https://some//random//url')).toEqual(
        'https://some/random/url',
      );
    });

    it('handles string starting with file://', () => {
      expect(url_helpers.fixUrl('file://some/random/url')).toEqual(
        'file://some/random/url',
      );
      expect(url_helpers.fixUrl('file://some//random//url')).toEqual(
        'file://some/random/url',
      );
    });
  });

  describe('isValidFileUrl', () => {
    it('returns false for empty string', () => {
      const result = url_helpers.isValidFileUrl('');
      expect(result).toBe(false);
    });

    it('returns false for whitespace string', () => {
      const result = url_helpers.isValidFileUrl('  ');
      expect(result).toBe(false);
    });

    it('returns false for random string', () => {
      const result = url_helpers.isValidFileUrl('some random string');
      expect(result).toBe(false);
    });

    it('returns false for invalid url', () => {
      const result = url_helpers.isValidFileUrl('sfile://google');
      expect(result).toBe(false);
    });

    it('returns true for valid file url', () => {
      const fileName =
        process.platform === 'win32' ? 'file:///c:\\' : 'file:///';
      const result = url_helpers.isValidFileUrl(fileName);
      expect(result).toBe(true);
    });
  });
});