Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make tests passing on windows

  • Loading branch information...
commit 34b6389116a3897c28efb83ef0b7f6369cd6e616 1 parent 17d8b2e
@eldargab authored
View
16 test/middleware.js
@@ -1,13 +1,14 @@
var sinon = require('sinon')
var Next = require('./support/next')
+var Onpath = require('./support/onpath')
var Middleware = require('../lib/middleware')
describe('Middleware', function () {
- var m, next, req, res, handler
+ var m, next, req, res, onpath
beforeEach(function () {
- handler = sinon.spy()
- m = Middleware('root', handler)
+ onpath = Onpath()
+ m = Middleware('root', onpath)
next = Next()
req = { method: 'GET'}
res = {}
@@ -20,17 +21,20 @@ describe('Middleware', function () {
it('Should pass path, req, res and next to the handler', function () {
test('/article')
- handler.calledWithExactly('root/article', req, res, next).should.be.true
+ onpath.handles('root/article')
+ onpath.arg(1).should.equal(req)
+ onpath.arg(2).should.equal(res)
+ onpath.arg(3).should.equal(next)
})
it('Should support urlencoded paths', function () {
test('/path%20with%20spaces.md')
- handler.calledWith('root/path with spaces.md')
+ onpath.handles('root/path with spaces.md')
})
it('Should respond with 403 Forbidden when traversing root', function () {
test('/malicious/%2e%2e/%2e%2e/path')
next.error(403)
- handler.called.should.be.false
+ onpath.notCalled()
})
})
View
31 test/path-handler.js
@@ -1,33 +1,23 @@
var sinon = require('sinon')
var Fs = require('./support/fake-fs')
var Next = require('./support/next')
+var Onpath = require('./support/onpath')
var Lookup = require('../lib/path-lookup')
-function Handler () {
- var spy = sinon.spy()
-
- spy.handles = function (file) {
- this.calledOnce.should.be.true
- this.calledWith(file).should.be.true
- }
-
- spy.notCalled = function () {
- this.called.should.be.false
- }
-
- return spy
-}
-
describe('Path handler', function () {
var lookup, next, fs, req, res, h, options
+ function test (path) {
+ lookup(path, req, res, next)
+ }
+
beforeEach(function () {
fs = new Fs
options = {
fsStat: function (p, cb) { fs.stat(p, cb) },
fsDir: function (p, cb) { fs.readdir(p, cb) },
}
- h = Handler()
+ h = Onpath()
lookup = Lookup(h, options)
next = Next()
req = {}
@@ -40,10 +30,6 @@ describe('Path handler', function () {
])
})
- function test (path) {
- lookup(path, req, res, next)
- }
-
it('Should be able to lookup file extension', function () {
test('root/article')
next.notCalled()
@@ -58,7 +44,9 @@ describe('Path handler', function () {
it('Should pass filename, req, res, next to the handler', function () {
test('root/article')
- h.calledWithExactly('root/article.md', req, res, next)
+ h.arg(1).should.equal(req)
+ h.arg(2).should.equal(res)
+ h.arg(3).should.equal(next)
})
it('Should support index files for dirs', function () {
@@ -93,6 +81,7 @@ describe('Path handler', function () {
var readdir = sinon.stub(fs, 'readdir')
var error = new Error
readdir.withArgs('readdir/error').yields(error)
+ readdir.withArgs('readdir\\error').yields(error)
test('readdir/error')
next.calledWithExactly(error).should.be.true
h.notCalled()
View
1  test/support/fake-fs.js
@@ -46,6 +46,7 @@ Fs.prototype.readdir = function (dir, cb) {
}
Fs.prototype._getPath = function (path) {
+ path = path.replace('\\', '/') // windows support
var segs = path.split('/')
var item = this.root
for (var i = 0; i < segs.length; i++) {
View
27 test/support/onpath.js
@@ -0,0 +1,27 @@
+var N = require('path').normalize
+var sinon = require('sinon')
+var should = require('should')
+
+module.exports = function () {
+ var spy = sinon.spy()
+
+ spy.handles = function (file) {
+ this.calledOnce.should.be.true
+
+ var args = this.firstCall.args
+ should.exist(args[0])
+ var actual = N(args[0])
+ var expected = N(file)
+ actual.should.equal(expected)
+ }
+
+ spy.arg = function (i) {
+ return this.firstCall.args[i]
+ }
+
+ spy.notCalled = function () {
+ this.called.should.be.false
+ }
+
+ return spy
+}
Please sign in to comment.
Something went wrong with that request. Please try again.