var server = require('./server')
  , events = require('events')
  , stream = require('stream')
  , assert = require('assert')
  , request = require('../index')
  ;

var s = server.createServer();
var expectedBody = "waited";
var remainingTests = 5;

s.listen(s.port, function () {
  // Request that waits for 200ms
  s.on('/timeout', function (req, resp) {
    setTimeout(function(){
      resp.writeHead(200, {'content-type':'text/plain'})
      resp.write(expectedBody)
      resp.end()
    }, 200);
  });

  // Scenario that should timeout
  var shouldTimeout = {
    url: s.url + "/timeout",
    timeout:100
  }


  request(shouldTimeout, function (err, resp, body) {
    assert.equal(err.code, "ETIMEDOUT");
    checkDone();
  })


  // Scenario that shouldn't timeout
  var shouldntTimeout = {
    url: s.url + "/timeout",
    timeout:300
  }

  request(shouldntTimeout, function (err, resp, body) {
    assert.equal(err, null);
    assert.equal(expectedBody, body)
    checkDone();
  })

  // Scenario with no timeout set, so shouldn't timeout
  var noTimeout = {
    url: s.url + "/timeout"
  }

  request(noTimeout, function (err, resp, body) {
    assert.equal(err);
    assert.equal(expectedBody, body)
    checkDone();
  })

  // Scenario with a negative timeout value, should be treated a zero or the minimum delay
  var negativeTimeout = {
    url: s.url + "/timeout",
    timeout:-1000
  }

  request(negativeTimeout, function (err, resp, body) {
    assert.equal(err.code, "ETIMEDOUT");
    checkDone();
  })

  // Scenario with a float timeout value, should be rounded by setTimeout anyway
  var floatTimeout = {
    url: s.url + "/timeout",
    timeout: 100.76
  }

  request(floatTimeout, function (err, resp, body) {
    assert.equal(err.code, "ETIMEDOUT");
    checkDone();
  })

  function checkDone() {
    if(--remainingTests == 0) {
      s.close();
      console.log("All tests passed.");
    }
  }
})


@@ -243,9 +243,9 @@
 s = 
-5
+6
 ;%0A%0As
@@ -730,16 +730,239 @@
 %0A  %7D)%0A%0A%0A
+  var shouldTimeoutWithEvents = %7B%0A    url: s.url + %22/timeout%22,%0A    timeout:100%0A  %7D%0A%0A  request(shouldTimeoutWithEvents)%0A    .on('error', function (err) %7B%0A      assert.equal(err.code, %22ETIMEDOUT%22);%0A      checkDone();%0A    %7D)%0A%0A
   // Sce

Test case for #304.