var EventEmitter = require('events').EventEmitter

var onFinished = require('..')
var should = require('should')

function createThingie() {
  var ee = new EventEmitter
  ee.socket = new EventEmitter
  ee.socket.writable = true
  return ee
}

describe('on socket error', function () {
  it('should execute the callback on socket error', function () {
    var thingie = createThingie()
    var called = false
    onFinished(thingie, function (err) {
      called = true
      err.message.should.equal('boom')
    })
    thingie.socket.emit('error', new Error('boom'))
    called.should.be.true
  })

  it('should not execute the callback if response is finished', function () {
    var thingie = createThingie()
    var called = false
    onFinished(thingie, function () {
      called = true
    })
    thingie.emit('finish')
    try {
      // throws if there are no listeners
      thingie.socket.emit('error', new Error('boom'))
      throw new Error('alksdjflaksjdf')
    } catch (err) {
      if (err.message !== 'boom')
        throw new Error('wtf')
    }
    called.should.be.true
  })
})

describe('when the socket is not writable', function () {
  it('should execute the callback immediately', function (done) {
    var thingie = createThingie()
    thingie.socket.writable = false
    onFinished(thingie, function (err) {
      done()
    })
  })
})

describe('when the socket closes', function () {
  it('should execute the callback', function (done) {
    var thingie = createThingie()
    onFinished(thingie, done)
    thingie.socket.emit('close')
  })
})

describe('when the request finishes', function () {
  it('should execute the callback', function (done) {
    var thingie = createThingie()
    onFinished(thingie, done)
    thingie.emit('finish')
  })
})

describe('when an emitter emits a non-error', function () {
  it('should ignore the error', function (done) {
    var thingie = createThingie()
    onFinished(thingie, done)
    thingie.socket.emit('close', false)
  })
})

@@ -44,16 +44,43 @@
 mitter%0A%0A
+var http = require('http')%0A
 var onFi
@@ -1610,28 +1610,36 @@
 hen 
-the request finishes
+an emitter emits a non-error
 ', f
@@ -1664,36 +1664,32 @@
 'should 
-execute the callback
+ignore the error
 ', funct
@@ -1781,75 +1781,604 @@
 gie.
-emit('finish')%0A  %7D)%0A%7D)%0A%0Adescribe('when an emitter emits a non-error
+socket.emit('close', false)%0A  %7D)%0A%7D)%0A%0Adescribe('http', function () %7B%0A  describe('when the request finishes', function () %7B%0A    it('should execute the callback', function (done) %7B%0A      var server = http.createServer(function (req, res) %7B%0A        onFinished(res, done)%0A        setTimeout(res.end.bind(res), 0)%0A      %7D)%0A      server.listen(function () %7B%0A        var port = this.address().port%0A        http.get('http://127.0.0.1:' + port, function (res) %7B%0A          res.resume()%0A          res.on('close', server.close.bind(server))%0A        %7D)%0A      %7D)%0A    %7D)%0A  %7D)%0A%0A  describe('when the request aborts
 ', f
@@ -2382,32 +2382,34 @@
 , function () %7B%0A
+  
   it('should ign
@@ -2405,32 +2405,36 @@
 'should 
-ignore the error
+execute the callback
 ', funct
@@ -2454,38 +2454,84 @@
     
+  
 var 
-thingie = createThingie()%0A
+client%0A      var server = http.createServer(function (req, res) %7B%0A    
     
@@ -2533,39 +2533,35 @@
      onFinished(
-thingie
+res
 , done)%0A    thin
@@ -2560,42 +2560,246 @@
     
-thingie.socket.emit('close', false
+    setTimeout(client.abort.bind(client), 0)%0A      %7D)%0A      server.listen(function () %7B%0A        var port = this.address().port%0A        client = http.get('http://127.0.0.1:' + port)%0A        client.on('error', function () %7B%7D)%0A      %7D)%0A    %7D
 )%0A  

add some http tests