import http from 'http'
import request from 'request'
import createProxy from '../../src/lib/proxy'
import tap from 'tap'

function setup (options, cb) {
  if (typeof options === 'function') {
    cb = options
    options = {}
  }

  const server = http.createServer()

  const serverStarted = () => {
    createProxy(options, proxyStarted)
  }

  const proxyStarted = (err, proxy) => {
    if (err) tap.bailout('can not create proxy...')
    server.on('close', proxy.close.bind(proxy))

    return cb(null, {
      request: request.defaults({'proxy': 'http://127.0.0.1:' + proxy.address().port}),
      server
    })
  }

  server.listen(0, serverStarted)
}

export default { setup }

@@ -372,17 +372,19 @@
 rr, prox
-y
+ies
 ) =%3E %7B%0A 
@@ -461,17 +461,22 @@
 e', prox
-y
+ies%5B0%5D
 .close.b
@@ -483,17 +483,80 @@
 ind(prox
-y
+ies%5B0%5D))%0A    server.on('close', proxies%5B1%5D.close.bind(proxies%5B1%5D
 ))%0A%0A    
@@ -641,17 +641,22 @@
 ' + prox
-y
+ies%5B0%5D
 .address

Test - Update proxy tests.