var test = require('tape')
var budo = require('../')

var request = require('request')
var entry = 'test/fixtures/app.js'

test('default should serve on 9966', port(9966))
test('should serve on specified port', port(3000, { port: 3000 }))

test('should serve on --dir', function (t) {
  t.plan(2)
  var app = budo(entry, { dir: __dirname })
    .on('connect', function (ev) {
      request.get({
        uri: ev.uri + 'fixtures/text.txt'
      }, function (err, resp, body) {
        if (err) t.fail(err)
        t.equal(body.toString(), 'foobar', 'text matches')
        app.close()
      })
    })
    .on('exit', function () {
      t.ok(true, 'closed')
    })
    .on('error', t.fail.bind(t))
})

test('favicon.ico should have status code 200', function (t) {
  t.plan(2)
  var app = budo(entry, { dir: __dirname })
    .on('connect', function (ev) {
      request.get({
        uri: ev.uri + 'favicon.ico'
      }, function (err, resp) {
        if (err) t.fail(err)
        t.equal(resp.statusCode, 200)
        app.close()
      })
    })
    .on('exit', function () {
      t.ok(true, 'closed')
    })
    .on('error', t.fail.bind(t))
})

function port (expected, opt) {
  return function (t) {
    t.plan(2)
    var app = budo(entry, opt)
      .on('connect', function (ev) {
        t.ok(ev.port, expected, 'serves on ' + expected)
        app.close()
      })
      .on('exit', function () {
        t.ok(true, 'closed')
      })
      .on('error', t.fail.bind(t))
  }
}

@@ -45,16 +45,63 @@
 e('../')
+%0Avar defaultHtml = require('simple-html-index')
 %0A%0Avar re
@@ -741,16 +741,777 @@
 t))%0A%7D)%0A%0A
+test('support defaultIndex stream', function (t) %7B%0A  t.plan(3)%0A%0A  function html (opt) %7B%0A    return defaultHtml(%7B%0A      entry: opt.entry,%0A      title: 'foobar',%0A      css: 'main.css'%0A    %7D)%0A  %7D%0A%0A  var app = budo(entry, %7B dir: __dirname, defaultIndex: html %7D)%0A    .on('connect', function (ev) %7B%0A      request.get(%7B%0A        uri: ev.uri%0A      %7D, function (err, resp, body) %7B%0A        if (err) t.fail(err)%0A        t.equal(resp.statusCode, 200)%0A        t.equal(body, '%3C!doctype html%3E%3Chead%3E%3Ctitle%3Efoobar%3C/title%3E%3Cmeta charset=%22utf-8%22%3E%3Clink rel=%22stylesheet%22 href=%22main.css%22%3E%3C/head%3E%3Cbody%3E%3Cscript src=%22app.js%22%3E%3C/script%3E%3C/body%3E%3C/html%3E')%0A        app.close()%0A      %7D)%0A    %7D)%0A    .on('exit', function () %7B%0A      t.ok(true, 'closed')%0A    %7D)%0A    .on('error', t.fail.bind(t))%0A%7D)%0A%0A
 test('fa

add test for defaultIndex