Skip to content

Commit

Permalink
tests: use assert instead of should
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Nov 23, 2014
1 parent 1bda9f9 commit 856450d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"parseurl": "~1.3.0"
},
"devDependencies": {
"after": "0.8.1",
"istanbul": "0.3.2",
"mocha": "~2.0.1",
"should": "~4.0.0",
"supertest": "~0.15.0"
},
"files": [
Expand Down
88 changes: 41 additions & 47 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

var after = require('after');
var assert = require('assert');
var http = require('http');
var fs = require('fs');
var path = require('path');
var request = require('supertest');
var should = require('should');
var serveIndex = require('..');

var fixtures = path.join(__dirname, '/fixtures');
Expand All @@ -13,7 +14,7 @@ var skipRelative = ~relative.indexOf('..') || path.resolve(relative) === relativ

describe('serveIndex(root)', function () {
it('should require root', function () {
serveIndex.should.throw(/root path required/)
assert.throws(serveIndex, /root path required/)
})

it('should serve text/html without Accept header', function (done) {
Expand Down Expand Up @@ -146,7 +147,7 @@ describe('serveIndex(root)', function () {
.end(function (err, res) {
if (err) throw err;
var urls = res.text.split(/<a href="([^"]*)"/).filter(function(s, i){ return i%2; });
urls.should.eql([
assert.deepEqual(urls, [
'/%23directory',
'/collect',
'/g%23%20%253%20o%20%252525%20%2537%20dir',
Expand Down Expand Up @@ -198,23 +199,17 @@ describe('serveIndex(root)', function () {

request(server)
.get('/')
.expect(200, function (err, res) {
if (err) return done(err)
res.text.should.not.containEql('.hidden')
done()
});
.expect(bodyDoesNotContain('.hidden'))
.expect(200, done)
});

it('should filter hidden files', function (done) {
var server = createServer('test/fixtures', {'hidden': false})

request(server)
.get('/')
.expect(200, function (err, res) {
if (err) return done(err)
res.text.should.not.containEql('.hidden')
done()
});
.expect(bodyDoesNotContain('.hidden'))
.expect(200, done)
});

it('should not filter hidden files', function (done) {
Expand All @@ -228,61 +223,51 @@ describe('serveIndex(root)', function () {

describe('with "filter" option', function () {
it('should custom filter files', function (done) {
var seen = false
var cb = after(2, done)
var server = createServer(fixtures, {'filter': filter})

function filter(name) {
if (name.indexOf('foo') === -1) return true
seen = true
cb()
return false
}

request(server)
.get('/')
.expect(200, function (err, res) {
if (err) return done(err)
seen.should.be.true
res.text.should.not.containEql('foo')
done()
});
.expect(bodyDoesNotContain('foo'))
.expect(200, cb)
});

it('should filter after hidden filter', function (done) {
var seen = false
var server = createServer(fixtures, {'filter': filter, 'hidden': false})

function filter(name) {
seen = seen || name.indexOf('.') === 0
if (name.indexOf('.') === 0) {
done(new Error('unexpected hidden file'))
}

return true
}

request(server)
.get('/')
.expect(200, function (err, res) {
if (err) return done(err)
seen.should.be.false
done()
});
.expect(200, done)
});

it('should filter directory paths', function (done) {
var seen = false
var cb = after(3, done)
var server = createServer(fixtures, {'filter': filter})

function filter(name, index, list, dir) {
if (path.normalize(dir) === path.normalize(path.join(fixtures, '/users'))) {
seen = true
cb()
}
return true
}

request(server)
.get('/users')
.expect(200, function (err, res) {
if (err) return done(err)
seen.should.be.true
done()
});
.expect(200, cb)
});
});

Expand All @@ -306,10 +291,7 @@ describe('serveIndex(root)', function () {

describe('when using custom handler', function () {
describe('exports.html', function () {
var orig = serveIndex.html
after(function () {
serveIndex.html = orig
})
alterProperty(serveIndex, 'html', serveIndex.html)

it('should get called with Accept: text/html', function (done) {
var server = createServer()
Expand Down Expand Up @@ -404,10 +386,7 @@ describe('serveIndex(root)', function () {
});

describe('exports.plain', function () {
var orig = serveIndex.plain
after(function () {
serveIndex.plain = orig
})
alterProperty(serveIndex, 'plain', serveIndex.plain)

it('should get called with Accept: text/plain', function (done) {
var server = createServer()
Expand All @@ -425,10 +404,7 @@ describe('serveIndex(root)', function () {
});

describe('exports.json', function () {
var orig = serveIndex.json
after(function () {
serveIndex.json = orig
})
alterProperty(serveIndex, 'json', serveIndex.json)

it('should get called with Accept: application/json', function (done) {
var server = createServer()
Expand Down Expand Up @@ -604,6 +580,18 @@ describe('serveIndex(root)', function () {
});
});

function alterProperty(obj, prop, val) {
var prev

beforeEach(function () {
prev = obj[prop]
obj[prop] = val
})
afterEach(function () {
obj[prop] = prev
})
}

function createServer(dir, opts) {
dir = dir || fixtures

Expand All @@ -616,3 +604,9 @@ function createServer(dir, opts) {
})
})
}

function bodyDoesNotContain(text) {
return function (res) {
assert.equal(res.text.indexOf(text), -1)
}
}

0 comments on commit 856450d

Please sign in to comment.