url.spec.ts 3.6 KB

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