'use strict'

var test = require('tape')
var http = require('http')
var enableDestroy = require('server-destroy')
var reverseHttp = require('./')

test('default values', function (t) {
  t.plan(4)

  var server = http.createServer(function (req, res) {
    t.fail('Unexpected HTTP request')
  })

  enableDestroy(server)

  server.listen(function () {
    var n = 0
    var rserver = reverseHttp({ port: server.address().port }, function (req, res) {
      res.end()
      switch (++n) {
        case 1:
          t.equal(req.method, 'GET')
          t.equal(req.url, '/foo')
          break
        case 2:
          rserver.close(server.destroy.bind(server))
          t.equal(req.method, 'GET')
          t.equal(req.url, '/bar')
          break
      }
    })

    rserver.on('error', function (err) {
      t.error(err)
    })
  })

  server.on('upgrade', function (req, socket, head) {
    socket.write('HTTP/1.1 101 Switching Protocols\r\n' +
                 'Upgrade: PTTH/1.0\r\n' +
                 'Connection: Upgrade\r\n' +
                 '\r\n')

    socket.write('GET /foo HTTP/1.1\r\n\r\n')

    setTimeout(function () {
      socket.write('GET /bar HTTP/1.1\r\n\r\n')
    }, 50)
  })
})

test('reconnect', function (t) {
  t.plan(5)

  var n = 0
  var server = http.createServer(function (req, res) {
    t.fail('Unexpected HTTP request')
  })

  enableDestroy(server)

  server.listen(function () {
    var rserver = reverseHttp({ port: server.address().port }, function (req, res) {
      res.end()

      t.equal(req.method, 'GET')
      t.equal(req.url, '/foo')

      if (n === 2) {
        rserver.close(server.destroy.bind(server))
        t.ok(true)
      }
    })

    rserver.on('error', function (err) {
      t.error(err)
    })
  })

  server.on('upgrade', function (req, socket, head) {
    if (++n === 1) {
      setTimeout(function () {
        socket.destroy()
      }, 50)
    }

    socket.write('HTTP/1.1 101 Switching Protocols\r\n' +
                 'Upgrade: PTTH/1.0\r\n' +
                 'Connection: Upgrade\r\n' +
                 '\r\n')

    socket.write('GET /foo HTTP/1.1\r\n\r\n')
  })
})

test('remote server goes offline', function (t) {
  t.plan(1)

  var server = http.createServer(function (req, res) {
    t.fail('Unexpected HTTP request')
  })

  enableDestroy(server)

  server.listen(function () {
    var rserver = reverseHttp({ port: server.address().port })
    rserver.on('error', function (err) {
      t.equal(err.code, 'ECONNREFUSED')
    })
  })

  server.on('upgrade', function (req, socket, head) {
    socket.write('HTTP/1.1 101 Switching Protocols\r\n' +
                 'Upgrade: PTTH/1.0\r\n' +
                 'Connection: Upgrade\r\n' +
                 '\r\n')

    setTimeout(function () {
      server.destroy()
    }, 50)
  })
})

test('respond', function (t) {
  t.plan(1)

  var server = http.createServer(function (req, res) {
    t.fail('Unexpected HTTP request')
  })
  var rserver

  enableDestroy(server)

  server.listen(function () {
    rserver = reverseHttp({ port: server.address().port }, function (req, res) {
      res.writeHead(418)
      res.write('foo')
      res.end()
    })
  })

  server.on('upgrade', function (req, socket, head) {
    socket.write('HTTP/1.1 101 Switching Protocols\r\n' +
                 'Upgrade: PTTH/1.0\r\n' +
                 'Connection: Upgrade\r\n' +
                 '\r\n')

    setTimeout(function () {
      socket.write('GET /foo HTTP/1.1\r\n\r\n')
    }, 50)

    socket.on('data', function (chunk) {
      var lines = chunk.toString().split('\r\n')
      t.equal(lines[0], 'HTTP/1.1 418 I\'m a teapot')
      rserver.close(server.destroy.bind(server))
    })
  })
})

@@ -294,33 +294,8 @@
 %7D)%0A%0A
-  enableDestroy(server)%0A%0A
   se
@@ -312,32 +312,32 @@
 n(function () %7B%0A
+
     var n = 0%0A  
@@ -578,33 +578,32 @@
 se 2:%0A          
-r
 server.close(ser
@@ -591,32 +591,45 @@
    server.close(
+)%0A          r
 server.destroy.b
@@ -622,37 +622,25 @@
 rver.destroy
-.bind(server)
+(
 )%0A          
@@ -791,32 +791,60 @@
 rror(err)%0A    %7D)
+%0A%0A    enableDestroy(rserver)
 %0A  %7D)%0A%0A  server.
@@ -1365,33 +1365,8 @@
 %7D)%0A%0A
-  enableDestroy(server)%0A%0A
   se
@@ -1583,25 +1583,24 @@
 ) %7B%0A        
-r
 server.close
@@ -1592,32 +1592,43 @@
    server.close(
+)%0A        r
 server.destroy.b
@@ -1621,37 +1621,25 @@
 rver.destroy
-.bind(server)
+(
 )%0A        t.
@@ -1721,32 +1721,60 @@
 rror(err)%0A    %7D)
+%0A%0A    enableDestroy(rserver)
 %0A  %7D)%0A%0A  server.
@@ -3170,32 +3170,59 @@
 es.end()%0A    %7D)%0A
+    enableDestroy(rserver)%0A
   %7D)%0A%0A  server.o
@@ -3539,16 +3539,18 @@
 ocket.on
+ce
 ('data',
@@ -3674,25 +3674,24 @@
 pot')%0A      
-r
 server.close
@@ -3691,16 +3691,25 @@
 r.close(
+)%0A      r
 server.d
@@ -3718,21 +3718,9 @@
 troy
-.bind(server)
+(
 )%0A  

Fix tests on Linux