uri.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. describe('uri', function() {
  2. beforeEach(function() {
  3. $([
  4. '<form class="form-horizontal" id="uriForm">',
  5. '<div id="msg"></div>',
  6. '<div class="form-group">',
  7. '<input type="text" name="uri" data-bv-uri />',
  8. '</div>',
  9. '</form>'
  10. ].join('\n')).appendTo('body');
  11. $('#uriForm').bootstrapValidator();
  12. this.bv = $('#uriForm').data('bootstrapValidator');
  13. this.$uri = this.bv.getFieldElements('uri');
  14. });
  15. afterEach(function() {
  16. $('#uriForm').bootstrapValidator('destroy').remove();
  17. });
  18. var validGlobalURIs = [
  19. 'http://foo.com/blah_blah',
  20. 'http://foo.com/blah_blah',
  21. 'http://foo.com/blah_blah/',
  22. 'http://foo.com/blah_blah_(wikipedia)',
  23. 'http://foo.com/blah_blah_(wikipedia)_(again)',
  24. 'http://www.example.com/wpstyle/?p=364',
  25. 'https://www.example.com/foo/?bar=baz&inga=42&quux',
  26. 'http://✪df.ws/123',
  27. 'http://userid:password@example.com:8080',
  28. 'http://userid:password@example.com:8080/',
  29. 'http://userid@example.com',
  30. 'http://userid@example.com/',
  31. 'http://userid@example.com:8080',
  32. 'http://userid@example.com:8080/',
  33. 'http://userid:password@example.com',
  34. 'http://userid:password@example.com/',
  35. 'http://142.42.1.1/',
  36. 'http://142.42.1.1:8080/',
  37. 'http://➡.ws/䨹',
  38. 'http://⌘.ws',
  39. 'http://⌘.ws/',
  40. 'http://foo.com/blah_(wikipedia)#cite-1',
  41. 'http://foo.com/blah_(wikipedia)_blah#cite-1',
  42. 'http://foo.com/unicode_(✪)_in_parens',
  43. 'http://foo.com/(something)?after=parens',
  44. 'http://☺.damowmow.com/',
  45. 'http://code.google.com/events/#&product=browser',
  46. 'http://j.mp',
  47. 'ftp://foo.bar/baz',
  48. 'http://foo.bar/?q=Test%20URL-encoded%20stuff',
  49. 'http://مثال.إختبار',
  50. 'http://例子.测试',
  51. 'http://उदाहरण.परीक्षा',
  52. "http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
  53. 'http://1337.net',
  54. 'http://a.b-c.de',
  55. 'http://223.255.255.254'
  56. ];
  57. var invalidGlobalURIs = [
  58. 'http://',
  59. 'http://.',
  60. 'http://..',
  61. 'http://../',
  62. 'http://?',
  63. 'http://??',
  64. 'http://??/',
  65. 'http://#',
  66. 'http://##',
  67. 'http://##/',
  68. 'http://foo.bar?q=Spaces should be encoded',
  69. '//',
  70. '//a',
  71. '///a',
  72. '///',
  73. 'http:///a',
  74. 'foo.com',
  75. 'rdar://1234',
  76. 'h://test',
  77. 'http:// shouldfail.com',
  78. ':// should fail',
  79. 'http://foo.bar/foo(bar)baz quux',
  80. 'ftps://foo.bar/',
  81. 'http://-error-.invalid/',
  82. 'http://a.b--c.de/',
  83. 'http://-a.b.co',
  84. 'http://a.b-.co',
  85. 'http://.www.foo.bar/',
  86. 'http://www.foo.bar./',
  87. 'http://.www.foo.bar./'
  88. ];
  89. var localURIs = [
  90. 'http://intranetsite',
  91. 'http://intranetsite/test',
  92. 'http://intranetsite:80',
  93. 'http://intranetsite:80/test',
  94. 'http://user:pass@intranetsite',
  95. 'http://user:pass@intranetsite/test',
  96. 'http://user:pass@intranetsite:80',
  97. 'http://user:pass@intranetsite:80/test',
  98. 'http://10.1.1.0',
  99. 'http://10.1.1.255',
  100. 'http://10.1.1.1',
  101. 'http://10.1.1.254',
  102. 'http://127.0.0.1',
  103. 'http://192.168.0.1',
  104. 'http://0.0.0.0',
  105. 'http://224.1.1.1',
  106. 'http://1.1.1.1.1',
  107. 'http://123.123.123',
  108. 'http://3628126748'
  109. ];
  110. it('Valid URIs (allowLocal=false)', function() {
  111. var me = this;
  112. $.each(validGlobalURIs, function(index, uri) {
  113. me.bv.resetForm();
  114. me.$uri.val(uri);
  115. me.bv.validate();
  116. expect(me.bv.isValid()).toBeTruthy();
  117. });
  118. });
  119. it('Invalid URIs (allowLocal=false)', function() {
  120. var me = this;
  121. $.each(invalidGlobalURIs.concat(localURIs), function(index, uri) {
  122. me.bv.resetForm();
  123. me.$uri.val(uri);
  124. me.bv.validate();
  125. expect(me.bv.isValid()).toEqual(false);
  126. });
  127. });
  128. it('Valid URIs (allowLocal=true)', function() {
  129. var me = this;
  130. me.bv.updateOption('uri', 'uri', 'allowLocal', true);
  131. $.each(validGlobalURIs.concat(localURIs), function(index, uri) {
  132. me.bv.resetForm();
  133. me.$uri.val(uri);
  134. me.bv.validate();
  135. expect(me.bv.isValid()).toBeTruthy();
  136. });
  137. });
  138. it('Invalid URIs (allowLocal=true)', function() {
  139. var me = this;
  140. me.bv.updateOption('uri', 'uri', 'allowLocal', true);
  141. $.each(invalidGlobalURIs, function(index, uri) {
  142. me.bv.resetForm();
  143. me.$uri.val(uri);
  144. me.bv.validate();
  145. expect(me.bv.isValid()).toEqual(false);
  146. });
  147. });
  148. });