url.spec.ts 3.3 KB

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