const getPort = require('get-server-port')
const test = require('tape')
const http = require('http')

const serverRouter = require('./')

test('should assert input types', function (t) {
  t.plan(2)
  t.throws(serverRouter.bind(null, {}))
  t.doesNotThrow(serverRouter.bind(null))
})

test('register a single callback on GET', function (t) {
  t.plan(1)
  const router = serverRouter()

  router.on('/foo', function (req, res) {
    t.pass('called')
    res.end()
    server.close()
  })

  const server = http.createServer(router).listen()
  http.get('http://localhost:' + getPort(server) + '/foo')
})

test('register an index path', function (t) {
  t.plan(1)
  const router = serverRouter()

  router.on('/', function (req, res) {
    t.pass('called')
    res.end()
    server.close()
  })

  const server = http.createServer(router).listen()
  http.get('http://localhost:' + getPort(server) + '/')
})

test('register a default path', function (t) {
  t.plan(1)
  const router = serverRouter('/404')

  router.on('/404', function (req, res) {
    t.pass('called')
    res.end()
    server.close()
  })

  const server = http.createServer(router).listen()
  http.get('http://localhost:' + getPort(server) + '/foo')
})

test('register multiple callbacks', function (t) {
  t.plan(1)
  const router = serverRouter()
  router.on('/foo', {
    get: function (req, res) {
      t.pass('called')
      res.end()
      server.close()
    }
  })

  const server = http.createServer(router).listen()
  http.get('http://localhost:' + getPort(server) + '/foo')
})

test('should call params', function (t) {
  t.plan(1)
  const router = serverRouter()
  router.on('/:bar', {
    get: function (req, res, params) {
      t.equal(params.bar, 'foo', 'params are equal')
      res.end()
      server.close()
    }
  })

  const server = http.createServer(router).listen()
  http.get('http://localhost:' + getPort(server) + '/foo')
})

test('should return a value', function (t) {
  t.plan(1)
  const router = serverRouter()
  router.on('/foo', {
    get: function (req, res, params) {
      res.end()
      server.close()
      return 'foo'
    }
  })

  const server = http.createServer(function (req, res) {
    const foo = router(req, res)
    t.equal(foo, 'foo', 'returns a value')
  }).listen()
  http.get('http://localhost:' + getPort(server) + '/foo')
})

test('nest routers', function (t) {
  t.plan(1)
  const r2 = serverRouter()
  r2.on('/bar', function (req, res) {
    res.end()
    server.close()
    return 'foo'
  })

  const r1 = serverRouter()
  r1.on('/foo', r2)

  const server = http.createServer(function (req, res) {
    const foo = r1(req, res)
    t.equal(foo, 'foo', 'returns a value')
  }).listen()
  http.get('http://localhost:' + getPort(server) + '/foo/bar')
})

@@ -2493,28 +2493,37 @@
 e()%0A    
-return 'foo'
+t.pass('path called')
 %0A  %7D)%0A%0A 
@@ -2609,177 +2609,952 @@
 ver(
-function (req, res) %7B%0A    const foo = r1(req, res)%0A    t.equal(foo, 'foo', 'returns a value')%0A  %7D).listen()%0A  http.get('http://localhost:' + getPort(server) + '/foo/bar'
+r1).listen()%0A  http.get('http://localhost:' + getPort(server) + '/foo/bar')%0A%7D)%0A%0Atest('nest routers with partials', function (t) %7B%0A  t.plan(5)%0A  const r2 = serverRouter()%0A  var check1 = false%0A  var check2 = false%0A  r2.on('/', function (req, res) %7B%0A    check1 = true%0A    res.end()%0A  %7D)%0A  r2.on('/:sub', function (req, res, params) %7B%0A    t.equal(params.sub, 'bar', 'params match')%0A    check2 = true%0A    res.end()%0A  %7D)%0A%0A  const r1 = serverRouter()%0A  r1.on('/foo', r2)%0A%0A  const server = http.createServer(r1).listen()%0A  const url1 = 'http://localhost:' + getPort(server) + '/foo'%0A  http.get(url1, function (res) %7B%0A    t.equal(res.statusCode, 200, 'status ok')%0A    t.equal(check1, true, 'first path was called')%0A%0A    const url2 = 'http://localhost:' + getPort(server) + '/foo/bar'%0A    http.get(url2, function (res) %7B%0A      t.equal(res.statusCode, 200, 'status ok')%0A      t.equal(check2, true, 'second path was called')%0A      server.close()%0A    %7D)%0A  %7D
 )%0A%7D)

add nested router test