PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 32ms 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
  1
  2/**
  3 * Module dependencies.
  4 */
  5
  6var assert = require('assert')
  7  , http = require('http');
  8
  9var server = http.createServer(function(req, res){
 10  if (req.method === 'GET') {
 11    if (req.url === '/delay') {
 12      setTimeout(function(){
 13        res.writeHead(200, {});
 14        res.end('delayed');
 15      }, 200);
 16    } else {
 17      var body = JSON.stringify({ name: 'tj' });
 18      res.writeHead(200, {
 19        'Content-Type': 'application/json; charset=utf8',
 20        'Content-Length': body.length
 21      });
 22      res.end(body);
 23    }
 24  } else {
 25    var body = '';
 26    req.setEncoding('utf8');
 27    req.on('data', function(chunk){ body += chunk });
 28    req.on('end', function(){
 29      res.writeHead(200, {});
 30      res.end(req.url + ' ' + body);
 31    });
 32  }
 33});
 34
 35var delayedServer = http.createServer(function(req, res){
 36  res.writeHead(200);
 37  res.end('it worked');
 38});
 39
 40var oldListen = delayedServer.listen;
 41delayedServer.listen = function(){
 42  var args = arguments;
 43  setTimeout(function(){
 44    oldListen.apply(delayedServer, args);
 45  }, 100);
 46};
 47
 48module.exports = {
 49  'test assert.response(req, res, fn)': function(beforeExit){
 50    var calls = 0;
 51
 52    assert.response(server, {
 53      url: '/',
 54      method: 'GET'
 55    },{
 56      body: '{"name":"tj"}',
 57      status: 200,
 58      headers: {
 59        'Content-Type': 'application/json; charset=utf8'
 60      }
 61    }, function(res){
 62      ++calls;
 63      assert.ok(res);
 64    });
 65    
 66    beforeExit(function(){
 67      assert.equal(1, calls);
 68    })
 69  },
 70
 71  'test assert.response(req, fn)': function(beforeExit){
 72    var calls = 0;
 73
 74    assert.response(server, {
 75      url: '/foo'
 76    }, function(res){
 77      ++calls;
 78      assert.ok(res.body.indexOf('tj') >= 0, 'Test assert.response() callback');
 79    });
 80
 81    beforeExit(function(){
 82      assert.equal(1, calls);
 83    });
 84  },
 85  
 86  'test assert.response() delay': function(beforeExit){
 87    var calls = 0;
 88
 89    assert.response(server,
 90      { url: '/delay', timeout: 1500 },
 91      { body: 'delayed' },
 92      function(){
 93        ++calls;
 94      });
 95
 96    beforeExit(function(){
 97      assert.equal(1, calls);
 98    });
 99  },
100
101  'test assert.response() regexp': function(beforeExit){
102    var calls = 0;
103
104    assert.response(server,
105      { url: '/foo', method: 'POST', data: 'foobar' },
106      { body: /^\/foo foo(bar)?/ },
107      function(){
108        ++calls;
109      });
110    
111    beforeExit(function(){
112      assert.equal(1, calls);
113    });
114  },
115  
116  'test assert.response() regexp headers': function(beforeExit){
117    var calls = 0;
118
119    assert.response(server,
120      { url: '/' },
121      { body: '{"name":"tj"}', headers: { 'Content-Type': /^application\/json/ } },
122      function(){
123        ++calls;
124      });
125    
126    beforeExit(function(){
127      assert.equal(1, calls);
128    });
129  },
130
131  // [!] if this test doesn't pass, an uncaught ECONNREFUSED will display
132  'test assert.response() with deferred listen()': function(beforeExit){
133    var calls = 0;
134
135    assert.response(delayedServer,
136      { url: '/' },
137      { body: 'it worked' },
138      function(){
139        ++calls;
140      });
141
142    beforeExit(function(){
143      assert.equal(1, calls);
144    });
145  }
146};