url.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { URLExt } from '@jupyterlab/coreutils';
  4. describe('@jupyterlab/coreutils', () => {
  5. describe('URLExt', () => {
  6. describe('.parse()', () => {
  7. it('should parse a url into a URLExt object', () => {
  8. const obj = URLExt.parse('http://www.example.com');
  9. expect(obj.href).toBe('http://www.example.com/');
  10. expect(obj.protocol).toBe('http:');
  11. expect(obj.host).toBe('www.example.com');
  12. expect(obj.hostname).toBe('www.example.com');
  13. expect(obj.pathname).toBe('/');
  14. });
  15. it('should handle query and hash', () => {
  16. const url = "http://example.com/path?that's#all, folks";
  17. const obj = URLExt.parse(url);
  18. // Chrome has a different href
  19. expect([
  20. 'http://example.com/path?that%27s#all,%20folks',
  21. 'http://example.com/path?that%27s#all, folks'
  22. ]).toContain(obj.href);
  23. expect(obj.protocol).toBe('http:');
  24. expect(obj.host).toBe('example.com');
  25. expect(obj.hostname).toBe('example.com');
  26. expect(obj.search).toBe('?that%27s');
  27. expect(obj.pathname).toBe('/path');
  28. // Chrome has a different hash
  29. expect(['#all,%20folks', '#all, folks']).toContain(obj.hash);
  30. });
  31. });
  32. describe('.join()', () => {
  33. it('should join a sequence of url components', () => {
  34. expect(URLExt.join('/foo/', 'bar/')).toBe('/foo/bar/');
  35. expect(URLExt.join('//example.com', 'bar/')).toBe('//example.com/bar/');
  36. expect(URLExt.join('//example.com', 'foo:bar/')).toBe(
  37. '//example.com/foo:bar/'
  38. );
  39. expect(URLExt.join('http://www.example.com/', '/bar')).toBe(
  40. 'http://www.example.com/bar'
  41. );
  42. expect(URLExt.join('http://user:pass@www.example.com/', '/bar')).toBe(
  43. 'http://user:pass@www.example.com/bar'
  44. );
  45. expect(URLExt.join('//example.com', 'foo:bar:', 'baz')).toBe(
  46. '//example.com/foo:bar:/baz'
  47. );
  48. expect(URLExt.join('http://example.com', 'foo:bar:', 'baz')).toBe(
  49. 'http://example.com/foo:bar:/baz'
  50. );
  51. expect(
  52. URLExt.join('http://example.com', 'foo', '..', '..', 'bar/')
  53. ).toBe('http://example.com/bar/');
  54. });
  55. });
  56. describe('.encodeParts()', () => {
  57. it('should encode and join a sequence of url components', () => {
  58. expect(URLExt.encodeParts('>/>')).toBe('%3E/%3E');
  59. });
  60. });
  61. describe('.normalize()', () => {
  62. it('should handle leading slash', () => {
  63. expect(URLExt.normalize('/')).toBe(location.origin + '/');
  64. });
  65. it('should handle leading double slash', () => {
  66. expect(URLExt.normalize('//foo')).toBe(location.protocol + '//foo/');
  67. });
  68. it('should handle http', () => {
  69. expect(URLExt.normalize('http://foo')).toBe('http://foo/');
  70. });
  71. it('should handle other', () => {
  72. expect(URLExt.normalize('ftp://foo')).toBe('ftp://foo/');
  73. });
  74. });
  75. describe('objectToQueryString()', () => {
  76. it('should return a serialized object string suitable for a query', () => {
  77. const obj = {
  78. name: 'foo',
  79. id: 'baz'
  80. };
  81. expect(URLExt.objectToQueryString(obj)).toBe('?name=foo&id=baz');
  82. });
  83. });
  84. describe('.isLocal()', () => {
  85. it('should test whether the url is a local url', () => {
  86. expect(URLExt.isLocal('https://foo/bar.txt')).toBe(false);
  87. expect(URLExt.isLocal('http://foo/bar.txt')).toBe(false);
  88. expect(URLExt.isLocal('//foo/bar.txt')).toBe(false);
  89. expect(URLExt.isLocal('file://foo/bar.txt')).toBe(false);
  90. expect(URLExt.isLocal('data:text/plain,123ABC')).toBe(false);
  91. expect(URLExt.isLocal('/foo/bar.txt')).toBe(false);
  92. expect(URLExt.isLocal('httpserver/index.html')).toBe(true);
  93. expect(URLExt.isLocal('../foo/bar.txt')).toBe(true);
  94. expect(URLExt.isLocal('./foo/bar.txt')).toBe(true);
  95. expect(URLExt.isLocal('foo/bar.txt')).toBe(true);
  96. expect(URLExt.isLocal('bar.txt')).toBe(true);
  97. });
  98. });
  99. });
  100. });