PageRenderTime 35ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/test/http.test.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 146 lines | 119 code | 23 blank | 4 comment | 4 complexity | 7146241ab65c99036937acbd719c6396 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /**
  2. * Module dependencies.
  3. */
  4. var assert = require('assert')
  5. , http = require('http');
  6. var server = http.createServer(function(req, res){
  7. if (req.method === 'GET') {
  8. if (req.url === '/delay') {
  9. setTimeout(function(){
  10. res.writeHead(200, {});
  11. res.end('delayed');
  12. }, 200);
  13. } else {
  14. var body = JSON.stringify({ name: 'tj' });
  15. res.writeHead(200, {
  16. 'Content-Type': 'application/json; charset=utf8',
  17. 'Content-Length': body.length
  18. });
  19. res.end(body);
  20. }
  21. } else {
  22. var body = '';
  23. req.setEncoding('utf8');
  24. req.on('data', function(chunk){ body += chunk });
  25. req.on('end', function(){
  26. res.writeHead(200, {});
  27. res.end(req.url + ' ' + body);
  28. });
  29. }
  30. });
  31. var delayedServer = http.createServer(function(req, res){
  32. res.writeHead(200);
  33. res.end('it worked');
  34. });
  35. var oldListen = delayedServer.listen;
  36. delayedServer.listen = function(){
  37. var args = arguments;
  38. setTimeout(function(){
  39. oldListen.apply(delayedServer, args);
  40. }, 100);
  41. };
  42. module.exports = {
  43. 'test assert.response(req, res, fn)': function(beforeExit){
  44. var calls = 0;
  45. assert.response(server, {
  46. url: '/',
  47. method: 'GET'
  48. },{
  49. body: '{"name":"tj"}',
  50. status: 200,
  51. headers: {
  52. 'Content-Type': 'application/json; charset=utf8'
  53. }
  54. }, function(res){
  55. ++calls;
  56. assert.ok(res);
  57. });
  58. beforeExit(function(){
  59. assert.equal(1, calls);
  60. })
  61. },
  62. 'test assert.response(req, fn)': function(beforeExit){
  63. var calls = 0;
  64. assert.response(server, {
  65. url: '/foo'
  66. }, function(res){
  67. ++calls;
  68. assert.ok(res.body.indexOf('tj') >= 0, 'Test assert.response() callback');
  69. });
  70. beforeExit(function(){
  71. assert.equal(1, calls);
  72. });
  73. },
  74. 'test assert.response() delay': function(beforeExit){
  75. var calls = 0;
  76. assert.response(server,
  77. { url: '/delay', timeout: 1500 },
  78. { body: 'delayed' },
  79. function(){
  80. ++calls;
  81. });
  82. beforeExit(function(){
  83. assert.equal(1, calls);
  84. });
  85. },
  86. 'test assert.response() regexp': function(beforeExit){
  87. var calls = 0;
  88. assert.response(server,
  89. { url: '/foo', method: 'POST', data: 'foobar' },
  90. { body: /^\/foo foo(bar)?/ },
  91. function(){
  92. ++calls;
  93. });
  94. beforeExit(function(){
  95. assert.equal(1, calls);
  96. });
  97. },
  98. 'test assert.response() regexp headers': function(beforeExit){
  99. var calls = 0;
  100. assert.response(server,
  101. { url: '/' },
  102. { body: '{"name":"tj"}', headers: { 'Content-Type': /^application\/json/ } },
  103. function(){
  104. ++calls;
  105. });
  106. beforeExit(function(){
  107. assert.equal(1, calls);
  108. });
  109. },
  110. // [!] if this test doesn't pass, an uncaught ECONNREFUSED will display
  111. 'test assert.response() with deferred listen()': function(beforeExit){
  112. var calls = 0;
  113. assert.response(delayedServer,
  114. { url: '/' },
  115. { body: 'it worked' },
  116. function(){
  117. ++calls;
  118. });
  119. beforeExit(function(){
  120. assert.equal(1, calls);
  121. });
  122. }
  123. };