index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. try {
  2. var url = require('is-url');
  3. } catch (e) {
  4. var url = require('..');
  5. }
  6. var assert = require('assert');
  7. describe('is-url', function () {
  8. describe('valid', function () {
  9. it('http://google.com', function () {
  10. assert(url('http://google.com'));
  11. });
  12. it('https://google.com', function () {
  13. assert(url('https://google.com'));
  14. });
  15. it('ftp://google.com', function () {
  16. assert(url('ftp://google.com'));
  17. });
  18. it('http://www.google.com', function () {
  19. assert(url('http://www.google.com'));
  20. });
  21. it('http://google.com/something', function () {
  22. assert(url('http://google.com/something'));
  23. });
  24. it('http://google.com?q=query', function () {
  25. assert(url('http://google.com?q=query'));
  26. });
  27. it('http://google.com#hash', function () {
  28. assert(url('http://google.com#hash'));
  29. });
  30. it('http://google.com/something?q=query#hash', function () {
  31. assert(url('http://google.com/something?q=query#hash'));
  32. });
  33. it('http://google.co.uk', function () {
  34. assert(url('http://google.co.uk'));
  35. });
  36. it('http://www.google.co.uk', function () {
  37. assert(url('http://www.google.co.uk'));
  38. });
  39. it('http://google.cat', function () {
  40. assert(url('http://google.cat'));
  41. });
  42. it('https://d1f4470da51b49289906b3d6cbd65074@app.getsentry.com/13176', function () {
  43. assert(url('https://d1f4470da51b49289906b3d6cbd65074@app.getsentry.com/13176'));
  44. });
  45. it('http://0.0.0.0', function () {
  46. assert(url('http://0.0.0.0'));
  47. });
  48. it('http://localhost', function () {
  49. assert(url('http://localhost'));
  50. });
  51. it('postgres://u:p@example.com:5702/db', function () {
  52. assert(url('postgres://u:p@example.com:5702/db'));
  53. });
  54. it('redis://:123@174.129.42.52:13271', function () {
  55. assert(url('redis://:123@174.129.42.52:13271'));
  56. });
  57. it('mongodb://u:p@example.com:10064/db', function () {
  58. assert(url('mongodb://u:p@example.com:10064/db'));
  59. });
  60. it('ws://chat.example.com/games', function () {
  61. assert(url('ws://chat.example.com/games'));
  62. });
  63. it('wss://secure.example.com/biz', function () {
  64. assert(url('wss://secure.example.com/biz'));
  65. });
  66. it('http://localhost:4000', function () {
  67. assert(url('http://localhost:4000'));
  68. });
  69. it('http://localhost:342/a/path', function () {
  70. assert(url('http://localhost:342/a/path'));
  71. });
  72. it('//google.com', function () {
  73. assert(url('//google.com'));
  74. });
  75. });
  76. describe('invalid', function () {
  77. it('http://', function () {
  78. assert(!url('http://'));
  79. });
  80. it('http://google', function () {
  81. assert(!url('http://google'));
  82. });
  83. it('http://google.', function () {
  84. assert(!url('http://google.'));
  85. });
  86. it('google', function () {
  87. assert(!url('google'));
  88. });
  89. it('google.com', function () {
  90. assert(!url('google.com'));
  91. });
  92. it('empty', function () {
  93. assert(!url(''));
  94. });
  95. it('undef', function () {
  96. assert(!url(undefined));
  97. });
  98. it('object', function () {
  99. assert(!url({}));
  100. });
  101. it('re', function () {
  102. assert(!url(/abc/));
  103. });
  104. });
  105. describe('redos', function () {
  106. it('redos exploit', function () {
  107. // Invalid. This should be discovered in under 1 second.
  108. var attackString = 'a://localhost' + '9'.repeat(100000) + '\t';
  109. var before = process.hrtime();
  110. assert(!url(attackString), 'attackString was valid');
  111. var elapsed = process.hrtime(before);
  112. assert(elapsed[0] < 1, 'attackString took ' + elapsed[0] + ' > 1 seconds');
  113. });
  114. });
  115. });