You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.8KB

  1. var urljoin = require('../lib/url-join');
  2. describe('url join', function () {
  3. it('should work for simple case', function () {
  4. urljoin('http://www.google.com/', 'foo/bar', '?test=123')
  5. .should.eql('http://www.google.com/foo/bar?test=123');
  6. });
  7. it('should work for simple case with new syntax', function () {
  8. urljoin(['http://www.google.com/', 'foo/bar', '?test=123'])
  9. .should.eql('http://www.google.com/foo/bar?test=123');
  10. });
  11. it('should work for hashbang urls', function () {
  12. urljoin(['http://www.google.com', '#!', 'foo/bar', '?test=123'])
  13. .should.eql('http://www.google.com/#!/foo/bar?test=123');
  14. });
  15. it('should be able to join protocol', function () {
  16. urljoin('http:', 'www.google.com/', 'foo/bar', '?test=123')
  17. .should.eql('http://www.google.com/foo/bar?test=123');
  18. });
  19. it('should be able to join protocol with slashes', function () {
  20. urljoin('http://', 'www.google.com/', 'foo/bar', '?test=123')
  21. .should.eql('http://www.google.com/foo/bar?test=123');
  22. });
  23. it('should remove extra slashes', function () {
  24. urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123')
  25. .should.eql('http://www.google.com/foo/bar?test=123');
  26. });
  27. it('should not remove extra slashes in an encoded URL', function () {
  28. urljoin('http:', 'www.google.com///', 'foo/bar', '?url=http%3A//Ftest.com')
  29. .should.eql('http://www.google.com/foo/bar?url=http%3A//Ftest.com');
  30. urljoin('http://a.com/23d04b3/', '/b/c.html')
  31. .should.eql('http://a.com/23d04b3/b/c.html')
  32. .should.not.eql('http://a.com/23d04b3//b/c.html');
  33. });
  34. it('should support anchors in urls', function () {
  35. urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '#faaaaa')
  36. .should.eql('http://www.google.com/foo/bar?test=123#faaaaa');
  37. });
  38. it('should support protocol-relative urls', function () {
  39. urljoin('//www.google.com', 'foo/bar', '?test=123')
  40. .should.eql('//www.google.com/foo/bar?test=123')
  41. });
  42. it('should support file protocol urls', function () {
  43. urljoin('file:/', 'android_asset', 'foo/bar')
  44. .should.eql('file://android_asset/foo/bar')
  45. urljoin('file:', '/android_asset', 'foo/bar')
  46. .should.eql('file://android_asset/foo/bar')
  47. });
  48. it('should support absolute file protocol urls', function () {
  49. urljoin('file:', '///android_asset', 'foo/bar')
  50. .should.eql('file:///android_asset/foo/bar')
  51. urljoin('file:///', 'android_asset', 'foo/bar')
  52. .should.eql('file:///android_asset/foo/bar')
  53. urljoin('file:///', '//android_asset', 'foo/bar')
  54. .should.eql('file:///android_asset/foo/bar')
  55. urljoin('file:///android_asset', 'foo/bar')
  56. .should.eql('file:///android_asset/foo/bar')
  57. });
  58. it.skip('should merge multiple query params properly', function () {
  59. urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?key=456')
  60. .should.eql('http://www.google.com/foo/bar?test=123&key=456');
  61. urljoin('http:', 'www.google.com///', 'foo/bar', '?test=123', '?boom=value', '&key=456')
  62. .should.eql('http://www.google.com/foo/bar?test=123&boom=value&key=456');
  63. urljoin('http://example.org/x', '?a=1', '?b=2', '?c=3', '?d=4')
  64. .should.eql('http://example.org/x?a=1&b=2&c=3&d=4');
  65. });
  66. //There is a problem with the capital A and the regex in the replace function
  67. // /([^:\s\%\3\A])\/+/g
  68. // I think the intention of the regex is to avoid replacing two slashes in the query string:
  69. // "?url=http%3A//"
  70. it.skip('should merge slashes in paths correctly', function () {
  71. urljoin('http://example.org', 'a//', 'b//', 'A//', 'B//')
  72. .should.eql('http://example.org/a/b/A/B/');
  73. });
  74. it.skip('should merge colons in paths correctly', function () {
  75. urljoin('http://example.org/', ':foo:', 'bar')
  76. .should.eql('http://example.org/:foo:/bar');
  77. });
  78. it.skip('should merge just a simple path without URL correctly', function() {
  79. urljoin('/', 'test')
  80. .should.eql('/test');
  81. });
  82. it('should merge a simple path with a number correctly', function() {
  83. urljoin('http://blabla.com/', 1)
  84. .should.eql('http://blabla.com/1');
  85. });
  86. it.skip('should merge slashes in protocol correctly', function () {
  87. urljoin('http://example.org', 'a')
  88. .should.eql('http://example.org/a');
  89. urljoin('http:', '//example.org', 'a')
  90. .should.eql('http://example.org/a');
  91. urljoin('http:///example.org', 'a')
  92. .should.eql('http://example.org/a');
  93. urljoin('file:///example.org', 'a')
  94. .should.eql('file:///example.org/a');
  95. //this one is broken
  96. urljoin('file:example.org', 'a')
  97. .should.eql('file://example.org/a');
  98. urljoin('file:/', 'example.org', 'a')
  99. .should.eql('file://example.org/a');
  100. urljoin('file:', '/example.org', 'a')
  101. .should.eql('file://example.org/a');
  102. urljoin('file:', '//example.org', 'a')
  103. .should.eql('file://example.org/a');
  104. });
  105. });