util.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. (function (module, io, should) {
  7. module.exports = {
  8. 'parse uri': function () {
  9. var http = io.util.parseUri('http://google.com')
  10. , https = io.util.parseUri('https://www.google.com:80')
  11. , query = io.util.parseUri('google.com:8080/foo/bar?foo=bar');
  12. http.protocol.should().eql('http');
  13. http.port.should().eql('');
  14. http.host.should().eql('google.com');
  15. https.protocol.should().eql('https');
  16. https.port.should().eql('80');
  17. https.host.should().eql('www.google.com');
  18. query.port.should().eql('8080');
  19. query.query.should().eql('foo=bar');
  20. query.path.should().eql('/foo/bar');
  21. query.relative.should().eql('/foo/bar?foo=bar');
  22. },
  23. 'unique uri': function () {
  24. var protocol = io.util.parseUri('http://google.com')
  25. , noprotocol = io.util.parseUri('google.com')
  26. , https = io.util.parseUri('https://google.com')
  27. , path = io.util.parseUri('https://google.com/google.com/com/?foo=bar');
  28. if ('object' == typeof window) {
  29. io.util.uniqueUri(protocol).should().eql('http://google.com:3000');
  30. io.util.uniqueUri(noprotocol).should().eql('http://google.com:3000');
  31. } else {
  32. io.util.uniqueUri(protocol).should().eql('http://google.com:80');
  33. io.util.uniqueUri(noprotocol).should().eql('http://google.com:80');
  34. }
  35. io.util.uniqueUri(https).should().eql('https://google.com:443');
  36. io.util.uniqueUri(path).should().eql('https://google.com:443');
  37. },
  38. 'chunk query string': function () {
  39. io.util.chunkQuery('foo=bar').should().be.a('object');
  40. io.util.chunkQuery('foo=bar').foo.should().eql('bar');
  41. },
  42. 'merge query strings': function () {
  43. var base = io.util.query('foo=bar', 'foo=baz')
  44. , add = io.util.query('foo=bar', 'bar=foo')
  45. base.should().eql('?foo=baz');
  46. add.should().eql('?foo=bar&bar=foo');
  47. io.util.query('','').should().eql('');
  48. io.util.query('foo=bar', '').should().eql('?foo=bar');
  49. io.util.query('', 'foo=bar').should().eql('?foo=bar');
  50. },
  51. 'request': function () {
  52. var type = typeof io.util.request();
  53. type.should().eql('object');
  54. },
  55. 'is array': function () {
  56. io.util.isArray([]).should().be_true;
  57. io.util.isArray({}).should().be_false;
  58. io.util.isArray('str').should().be_false;
  59. io.util.isArray(new Date).should().be_false;
  60. io.util.isArray(true).should().be_false;
  61. io.util.isArray(arguments).should().be_false;
  62. },
  63. 'merge, deep merge': function () {
  64. var start = {
  65. foo: 'bar'
  66. , bar: 'baz'
  67. }
  68. , duplicate = {
  69. foo: 'foo'
  70. , bar: 'bar'
  71. }
  72. , extra = {
  73. ping: 'pong'
  74. }
  75. , deep = {
  76. level1:{
  77. foo: 'bar'
  78. , level2: {
  79. foo: 'bar'
  80. , level3:{
  81. foo: 'bar'
  82. , rescursive: deep
  83. }
  84. }
  85. }
  86. }
  87. // same structure, but changed names
  88. , deeper = {
  89. foo: 'bar'
  90. , level1:{
  91. foo: 'baz'
  92. , level2: {
  93. foo: 'foo'
  94. , level3:{
  95. foo: 'pewpew'
  96. , rescursive: deep
  97. }
  98. }
  99. }
  100. };
  101. io.util.merge(start, duplicate);
  102. start.foo.should().eql('foo');
  103. start.bar.should().eql('bar');
  104. io.util.merge(start, extra);
  105. start.ping.should().eql('pong');
  106. start.foo.should().eql('foo');
  107. io.util.merge(deep, deeper);
  108. deep.foo.should().eql('bar');
  109. deep.level1.foo.should().eql('baz');
  110. deep.level1.level2.foo.should().eql('foo');
  111. deep.level1.level2.level3.foo.should().eql('pewpew');
  112. },
  113. 'defer': function (next) {
  114. var now = +new Date;
  115. io.util.defer(function () {
  116. ((new Date - now) >= ( io.util.webkit ? 100 : 0 )).should().be_true();
  117. next();
  118. })
  119. },
  120. 'indexOf': function () {
  121. var data = ['socket', 2, 3, 4, 'socket', 5, 6, 7, 'io'];
  122. io.util.indexOf(data, 'socket', 1).should().eql(4);
  123. io.util.indexOf(data, 'socket').should().eql(0);
  124. io.util.indexOf(data, 'waffles').should().eql(-1);
  125. }
  126. };
  127. })(
  128. 'undefined' == typeof module ? module = {} : module
  129. , 'undefined' == typeof io ? require('socket.io-client') : io
  130. , 'undefined' == typeof should ? require('should') : should
  131. );