test.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * Usage: node test.js
  3. */
  4. var wurl = require('./wurl.js'),
  5. assert = require('assert');
  6. function strictEqual(a, b) {
  7. console.log('Test: ' + a + ' === ' + b);
  8. assert.strictEqual.apply(null, arguments);
  9. }
  10. // Test URLs.
  11. var url = 'http://rob:abcd1234@www.domain.com/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese',
  12. urlHttps = 'https://rob:abcd1234@www.domain.com/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese',
  13. urlIp = 'https://rob:abcd1234@1.2.3.4/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese';
  14. /*strictEqual( wurl('{}', url), {
  15. 'auth': 'rob:abcd1234',
  16. 'domain': 'domain.com',
  17. 'file': 'index.html',
  18. 'fileext': 'html',
  19. 'filename': 'index',
  20. 'hash': 'test=hash&chucky=cheese',
  21. 'hostname': 'www.domain.com',
  22. 'pass': 'abcd1234',
  23. 'path': '/path/index.html',
  24. 'port': '80',
  25. 'protocol': 'http',
  26. 'query': 'query1=test&silly=willy',
  27. 'sub': 'www',
  28. 'tld': 'com',
  29. 'user': 'rob'
  30. });*/
  31. strictEqual( wurl('tld', 'http://sub.www.domain.co.uk'), 'co.uk' );
  32. strictEqual( wurl('tld', 'http://www.domain.org.uk'), 'org.uk' );
  33. strictEqual( wurl('tld', 'http://domain.la'), 'la' );
  34. strictEqual( wurl('tld', 'http://in'), undefined );
  35. strictEqual( wurl('tld', 'http://.asia'), 'asia' );
  36. strictEqual( wurl('tld', 'http://.cao.uk'), undefined );
  37. strictEqual( wurl('tld', 'http://'), undefined );
  38. strictEqual( wurl('tld', 'http://domain.zoo'), undefined );
  39. strictEqual( wurl('tld', url), 'com' );
  40. strictEqual( wurl('domain', 'http://sub.www.domain.co.uk'), 'domain.co.uk' );
  41. strictEqual( wurl('domain', 'http://www.domain.org.uk'), 'domain.org.uk' );
  42. strictEqual( wurl('domain', 'http://domain.la'), 'domain.la' );
  43. strictEqual( wurl('domain', 'http://in'), undefined );
  44. strictEqual( wurl('domain', 'http://.asia'), undefined );
  45. strictEqual( wurl('domain', 'http://.cao.uk'), undefined );
  46. strictEqual( wurl('domain', 'http://'), undefined );
  47. strictEqual( wurl('domain', 'http://domain.zoo'), undefined );
  48. strictEqual( wurl('domain', url), 'domain.com' );
  49. strictEqual( wurl('domain', 'https://test.testshi.cn/test.html' ), 'testshi.cn' );
  50. strictEqual( wurl('sub', 'http://sub.www.domain.co.uk'), 'sub.www' );
  51. strictEqual( wurl('sub', 'http://www.domain.org.uk'), 'www' );
  52. strictEqual( wurl('sub', 'http://domain.la'), undefined );
  53. strictEqual( wurl('sub', 'http://in'), undefined );
  54. strictEqual( wurl('sub', 'http://.asia'), undefined );
  55. strictEqual( wurl('sub', 'http://.cao.uk'), undefined );
  56. strictEqual( wurl('sub', 'http://'), undefined );
  57. strictEqual( wurl('sub', 'http://domain.zoo'), undefined );
  58. strictEqual( wurl('sub', url), 'www' );
  59. strictEqual( wurl( 'hostname', url ), 'www.domain.com' );
  60. strictEqual( wurl( 'hostname', urlIp ), '1.2.3.4' );
  61. //strictEqual( wurl( '.', url ), ['www', 'domain', 'com'] );
  62. strictEqual( wurl( '.0', url ), undefined );
  63. strictEqual( wurl( '.1', url ), 'www' );
  64. strictEqual( wurl( '.2', url ), 'domain' );
  65. strictEqual( wurl( '.-1', url ), 'com' );
  66. strictEqual( wurl( 'auth', url ), 'rob:abcd1234' );
  67. strictEqual( wurl( 'user', url ), 'rob' );
  68. strictEqual( wurl( 'email', 'mailto:rob@websanova.com' ), 'rob@websanova.com' );
  69. strictEqual( wurl( 'pass', url ), 'abcd1234' );
  70. strictEqual( wurl( 'port', url ), '80' );
  71. strictEqual( wurl( 'port', url.toUpperCase() ), '80' );
  72. strictEqual( wurl( 'port', 'http://example.com:80' ), '80' );
  73. strictEqual( wurl( 'port', urlHttps ), '443' );
  74. strictEqual( wurl( 'port', urlHttps.toUpperCase() ), '443' );
  75. strictEqual( wurl( 'port', 'https://example.com:443' ), '443' );
  76. strictEqual( wurl( 'port', 'http://domain.com:400?poo=a:b' ), '400' );
  77. strictEqual( wurl( 'port', 'https://domain.com:80' ), '80' );
  78. strictEqual( wurl( 'port', 'http://domain.com:443' ), '443' );
  79. strictEqual( wurl( 'port', 'http://domain.com' ), '80' );
  80. strictEqual( wurl( 'port', 'https://domain.com' ), '443' );
  81. strictEqual( wurl( 'protocol', 'http://domain.com' ), 'http' );
  82. strictEqual( wurl( 'protocol', 'http://domain.com:80' ), 'http' );
  83. strictEqual( wurl( 'protocol', 'http://domain.com:443' ), 'http' );
  84. strictEqual( wurl( 'protocol', 'domain.com' ), 'http' );
  85. strictEqual( wurl( 'protocol', 'domain.com:80' ), 'http' );
  86. strictEqual( wurl( 'protocol', 'domain.com:443' ), 'https' );
  87. strictEqual( wurl( 'protocol', 'https://domain.com:443' ), 'https' );
  88. strictEqual( wurl( 'protocol', 'https://domain.com:80' ), 'https' );
  89. strictEqual( wurl( 'protocol', 'mailto:rob@websanova.com' ), 'mailto' );
  90. strictEqual( wurl( 'path', url ), '/path/index.html' );
  91. strictEqual( wurl( 'path', 'http://www.domain.com/first/second' ), '/first/second' );
  92. strictEqual( wurl( 'path', 'http://www.domain.com/first/second/' ), '/first/second/' );
  93. strictEqual( wurl( 'path', 'http://www.domain.com:8080/first/second' ), '/first/second' );
  94. strictEqual( wurl( 'path', 'http://www.domain.com:8080/first/second/' ), '/first/second/' );
  95. strictEqual( wurl( 'path', 'http://www.domain.com/first/second?test=foo' ), '/first/second' );
  96. strictEqual( wurl( 'path', 'http://www.domain.com/first/second/?test=foo' ), '/first/second/' );
  97. strictEqual( wurl( 'path', 'http://www.domain.com/path#anchor' ), '/path' );
  98. strictEqual( wurl( 'path', 'http://www.domain.com/path/#anchor' ), '/path/' );
  99. strictEqual( wurl( 'path', 'http://www.domain.com' ), '' );
  100. strictEqual( wurl( 'path', 'http://www.domain.com/' ), '/' );
  101. strictEqual( wurl( 'path', 'http://www.domain.com#anchor' ), '' );
  102. strictEqual( wurl( 'path', 'http://www.domain.com/#anchor' ), '/' );
  103. strictEqual( wurl( 'path', 'http://www.domain.com?test=foo' ), '' );
  104. strictEqual( wurl( 'path', 'http://www.domain.com/?test=foo' ), '/' );
  105. strictEqual( wurl( 'path', 'http://www.domain.com:80' ), '' );
  106. strictEqual( wurl( 'path', 'http://www.domain.com:80/' ), '/' );
  107. strictEqual( wurl( 'path', 'http://www.domain.com:80#anchor' ), '' );
  108. strictEqual( wurl( 'path', 'http://www.domain.com:80/#anchor' ), '/' );
  109. strictEqual( wurl( 'path', 'http://www.domain.com:80?test=foo' ), '' );
  110. strictEqual( wurl( 'path', 'http://www.domain.com:80/?test=foo' ), '/' );
  111. strictEqual( wurl( 'file', url ), 'index.html' );
  112. strictEqual( wurl( 'filename', url ), 'index' );
  113. strictEqual( wurl( 'fileext', url ), 'html' );
  114. //strictEqual( wurl( '/', url ), ['path', 'index.html'] );
  115. strictEqual( wurl( '0', url ), undefined );
  116. strictEqual( wurl( '-4', url ), undefined );
  117. strictEqual( wurl( '1', url ), 'path' );
  118. strictEqual( wurl( 1, url ), 'path' );
  119. strictEqual( wurl( '2', url ), 'index.html' );
  120. strictEqual( wurl( '3', url ), undefined );
  121. strictEqual( wurl( '-1', url ), 'index.html' );
  122. strictEqual( wurl( '1', 'http://www.domain.com/first/second' ), 'first' );
  123. strictEqual( wurl( '1', 'http://www.domain.com/first/second/' ), 'first' );
  124. strictEqual( wurl( '-1', 'http://www.domain.com/first/second?test=foo' ), 'second' );
  125. strictEqual( wurl( '-1', 'http://www.domain.com/first/second/?test=foo' ), '' );
  126. strictEqual( wurl( '-2', 'http://www.domain.com/first/second/?test=foo' ), 'second' );
  127. strictEqual( wurl( 'query', url ), 'query1=test&silly=willy' );
  128. //strictEqual( wurl( '?', url ), {'query1': 'test', 'silly': 'willy'} );
  129. strictEqual( wurl( '?silly', url ), 'willy' );
  130. strictEqual( wurl( '?poo', url ), undefined );
  131. strictEqual( wurl( '?poo', 'http://domain.com?poo=' ), '' );
  132. strictEqual( wurl( '?poo', 'http://domain.com/?poo' ), '' );
  133. strictEqual( wurl( '?poo', 'http://domain.com?poo' ), '' );
  134. strictEqual( wurl( '?poo', 'http://domain.com?' ), undefined );
  135. strictEqual( wurl( '?poo', 'http://domain.com' ), undefined );
  136. strictEqual( wurl( '?poo', 'http://domain.com?poo=a+b' ), 'a b' );
  137. strictEqual( wurl( '?poo', 'http://domain.com?poo=javascript%20decode%20uri%20%2B%20sign%20to%20space' ), 'javascript decode uri + sign to space' );
  138. strictEqual( wurl( '?key', 'http://domain.com?key=value=va?key2=value' ), 'value=va?key2=value');
  139. strictEqual( wurl( '?poo', 'http://domain.com:400?poo=a:b' ), 'a:b' );
  140. strictEqual( wurl( '?poo', 'http://domain.com:400? & & &' ), undefined );
  141. strictEqual( wurl( '?field[0]', 'http://domain.com?field[0]=zero&field[1]=one' ), 'zero' );
  142. //strictEqual( wurl( '?field', 'http://domain.com?field[0]=zero&field[1]=one&var=test' ), ['zero', 'one'] );
  143. //strictEqual( wurl( '?field', 'http://domain.com?field[0]=zero&field[3]=one&var=test' ), ['zero', undefined, undefined, 'one'] );
  144. strictEqual( wurl( '?var', 'http://domain.com?field[0]=zero&field[3]=one&var=test' ), 'test' );
  145. //strictEqual( wurl( '?', 'http://domain.com?field[0]=zero&field[1]=one&var=test' ), {'field': ['zero', 'one'], 'var': 'test'} );
  146. strictEqual( wurl( 'hash', url ), 'test=hash&chucky=cheese' );
  147. //strictEqual( wurl( '#', url ), {'chucky': 'cheese', 'test': 'hash'} );
  148. strictEqual( wurl( '#chucky', url ), 'cheese' );
  149. strictEqual( wurl( '#poo', url ), undefined );
  150. strictEqual( wurl( '#poo', 'http://domain.com#poo=' ), '' );
  151. strictEqual( wurl( '#poo', 'http://domain.com/#poo' ), '' );
  152. strictEqual( wurl( '#poo', 'http://domain.com#poo' ), '' );
  153. strictEqual( wurl( '#poo', 'http://domain.com#' ), undefined );
  154. strictEqual( wurl( '#poo', 'http://domain.com' ), undefined );
  155. strictEqual( wurl( '#field[0]', 'http://domain.com#field[0]=zero&field[1]=one' ), 'zero' );
  156. //strictEqual( wurl( '#field', 'http://domain.com#field[0]=zero&field[1]=one&var=test' ), ['zero', 'one'] );
  157. //strictEqual( wurl( '#field', 'http://domain.com#field[0]=zero&field[3]=one&var=test' ), ['zero', undefined, undefined, 'one'] );
  158. strictEqual( wurl( '#var', 'http://domain.com#field[0]=zero&field[3]=one&var=test' ), 'test' );
  159. //strictEqual( wurl( '#', 'http://domain.com#field[0]=zero&field[1]=one&var=test' ), {'field': ['zero', 'one'], 'var': 'test'} );