mirror of
https://github.com/jimeh/heartb.it.git
synced 2026-02-19 04:46:40 +00:00
added a simple test for the host_redirect middleware using mocha and should.js
This commit is contained in:
11
Makefile
11
Makefile
@@ -1,9 +1,11 @@
|
||||
.SILENT:
|
||||
.PHONY: build watch
|
||||
.PHONY: build watch docs test
|
||||
|
||||
COFFEE_SRC = src
|
||||
COFFEE_OUT = .
|
||||
BIN = ./node_modules/.bin
|
||||
COFFEE_SRC = ./src
|
||||
COFFEE_OUT = ./
|
||||
REPORTER = spec
|
||||
TEST_DIR = ./test
|
||||
|
||||
build:
|
||||
$(BIN)/coffee -c -o $(COFFEE_OUT) $(COFFEE_SRC)
|
||||
@@ -13,3 +15,6 @@ watch:
|
||||
|
||||
docs:
|
||||
$(BIN)/docco $(shell find $(COFFEE_SRC) -name '*.coffee')
|
||||
|
||||
test:
|
||||
$(BIN)/mocha -R $(REPORTER) $(TEST_DIR)/*.js
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
"coffeekup": "0.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"docco": "0.3.0"
|
||||
"docco": "*",
|
||||
"mocha": "*",
|
||||
"should": "*"
|
||||
}
|
||||
}
|
||||
|
||||
28
src/test/host_redirect.test.coffee
Normal file
28
src/test/host_redirect.test.coffee
Normal file
@@ -0,0 +1,28 @@
|
||||
require 'should'
|
||||
|
||||
host_rewrite = require '../middleware/host_redirect'
|
||||
|
||||
describe 'host_redirect', ->
|
||||
|
||||
redirect_map =
|
||||
'www.foo.com': 'http://foo.com/',
|
||||
'img.foo.com': 'http://images.foo.com/'
|
||||
|
||||
redirector = host_rewrite(redirect_map)
|
||||
|
||||
describe 'when request does not match any entry in map', ->
|
||||
it 'next() is called to pass on the request', (done) ->
|
||||
req = { header: -> 'www.bar.com' }
|
||||
res = {}
|
||||
next = -> done()
|
||||
redirector(req, res, next)
|
||||
|
||||
describe 'when request matches an entry in map', ->
|
||||
it 'req.redirect() is called with the new URL', (done) ->
|
||||
req = { header: -> 'img.foo.com' }
|
||||
res =
|
||||
redirect: (url) ->
|
||||
url.should.equal(redirect_map[req.header()])
|
||||
done()
|
||||
next = ->
|
||||
redirector(req, res, next)
|
||||
50
test/host_redirect.test.js
Normal file
50
test/host_redirect.test.js
Normal file
@@ -0,0 +1,50 @@
|
||||
(function() {
|
||||
var host_rewrite;
|
||||
|
||||
require('should');
|
||||
|
||||
host_rewrite = require('../middleware/host_redirect');
|
||||
|
||||
describe('host_redirect', function() {
|
||||
var redirect_map, redirector;
|
||||
redirect_map = {
|
||||
'www.foo.com': 'http://foo.com/',
|
||||
'img.foo.com': 'http://images.foo.com/'
|
||||
};
|
||||
redirector = host_rewrite(redirect_map);
|
||||
describe('when request does not match any entry in map', function() {
|
||||
return it('next() is called to pass on the request', function(done) {
|
||||
var next, req, res;
|
||||
req = {
|
||||
header: function() {
|
||||
return 'www.bar.com';
|
||||
}
|
||||
};
|
||||
res = {};
|
||||
next = function() {
|
||||
return done();
|
||||
};
|
||||
return redirector(req, res, next);
|
||||
});
|
||||
});
|
||||
return describe('when request matches an entry in map', function() {
|
||||
return it('req.redirect() is called with the new URL', function(done) {
|
||||
var next, req, res;
|
||||
req = {
|
||||
header: function() {
|
||||
return 'img.foo.com';
|
||||
}
|
||||
};
|
||||
res = {
|
||||
redirect: function(url) {
|
||||
url.should.equal(redirect_map[req.header()]);
|
||||
return done();
|
||||
}
|
||||
};
|
||||
next = function() {};
|
||||
return redirector(req, res, next);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
}).call(this);
|
||||
Reference in New Issue
Block a user