create-server
Advanced tools
Comparing version 0.0.6 to 0.0.7
41
index.js
'use strict'; | ||
var listen = require('connected') | ||
, parse = require('url').parse | ||
, path = require('path') | ||
@@ -95,20 +96,32 @@ , fs = require('fs'); | ||
// | ||
if (+options.redirect) require('http').createServer(function handle(req, res) { | ||
res.statusCode = 404; | ||
if (+options.redirect) { | ||
var redirect = require('http').createServer(function handle(req, res) { | ||
res.statusCode = 404; | ||
if (req.headers.host) { | ||
res.statusCode = 301; | ||
res.setHeader( | ||
'Location', | ||
'http'+ (secure ? 's' : '') +'://'+ req.headers.host + req.url | ||
if (req.headers.host) { | ||
var url = parse('http://'+ req.headers.host); | ||
res.statusCode = 301; | ||
res.setHeader( | ||
'Location', | ||
'http'+ (secure ? 's' : '') +'://'+ url.hostname +':'+ port + req.url | ||
); | ||
} | ||
if (secure) res.setHeader( | ||
'Strict-Transport-Security', | ||
'max-age=8640000; includeSubDomains' | ||
); | ||
} | ||
if (secure) res.setHeader( | ||
'Strict-Transport-Security', | ||
'max-age=8640000; includeSubDomains' | ||
); | ||
res.end(''); | ||
}).listen(+options.redirect); | ||
res.end(''); | ||
}).listen(+options.redirect); | ||
// | ||
// Close the redirect server when the main server is closed. | ||
// | ||
server.once('close', function close() { | ||
try { redirect.close(); } | ||
catch (e) {} | ||
}); | ||
} | ||
@@ -115,0 +128,0 @@ // |
{ | ||
"name": "create-server", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Create a pre-configured HTTP server", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha $(find test -name '*.test.js')" | ||
"coverage": "istanbul cover node_modules/.bin/_mocha -- --reporter spec", | ||
"test": "mocha $(find test -name '*.test.js')", | ||
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter spec" | ||
}, | ||
@@ -34,6 +36,8 @@ "repository": { | ||
"assume": "0.0.x", | ||
"mocha": "1.20.x", | ||
"istanbul": "0.3.x", | ||
"mocha": "1.21.x", | ||
"pre-commit": "0.0.x", | ||
"spdy": "1.26.x" | ||
"request": "2.45.x", | ||
"spdy": "1.29.x" | ||
} | ||
} |
@@ -5,2 +5,3 @@ # create-server | ||
[![NPM version](https://badge.fury.io/js/create-server.svg)](http://badge.fury.io/js/create-server) | ||
[![Coverage Status](https://img.shields.io/coveralls/primus/create-server.svg)](https://coveralls.io/r/primus/create-server?branch=master) | ||
@@ -7,0 +8,0 @@ I've found my self writing this particular piece of snippet over and over again. |
describe('create server', function () { | ||
'use strict'; | ||
var assume = require('assume') | ||
var request = require('request') | ||
, assume = require('assume') | ||
, create = require('../') | ||
@@ -14,14 +15,24 @@ , port = 1024 | ||
afterEach(function (next) { | ||
if (server) try { server.close(next); } | ||
catch (e) { process.nextTick(next); } | ||
else process.nextTick(next); | ||
if (!server) return next(); | ||
try { | ||
server.close(function () { | ||
// | ||
// We can't use `server.close(next);` directly because in node > 0.10 | ||
// the callback receives an error when the server is not running. | ||
// | ||
next(); | ||
}); | ||
} catch (e) { | ||
process.nextTick(next); | ||
} | ||
server = undefined; | ||
}); | ||
it('is exported as function', function () { | ||
it('is exported as a function', function () { | ||
assume(create).to.be.a('function'); | ||
}); | ||
it('starts a HTTP server when given port number', function (next) { | ||
it('creates a HTTP server that listens on the given port (number)', function (next) { | ||
server = create(++port, { listening: function (err) { | ||
@@ -37,19 +48,50 @@ if (err) return next(err); | ||
it('proxies the error to the listener', function (next) { | ||
server = create(80, { listening: function (err) { | ||
if (!err) throw new Error('Port 80 should be restricted, we are not root'); | ||
it('creates a HTTP server that listens on the given port (object)', function (next) { | ||
server = create({ port: ++port }, { listening: function (err) { | ||
if (err) return next(err); | ||
assume(server.address().port).to.equal(port); | ||
next(); | ||
}}); | ||
assume(server).to.be.instanceOf(http.Server); | ||
}); | ||
it('creates a HTTP server with the given port property', function (next) { | ||
server = create({ port: ++port }, { listening: function (err) { | ||
if (err) return next(err); | ||
it('proxies errors to the listener', function (next) { | ||
server = create(80, { listening: function (err) { | ||
if (!err) throw new Error('Port 80 should be restricted, we are not root'); | ||
assume(server.address().port).to.equal(port); | ||
next(); | ||
}}); | ||
}); | ||
assume(server).to.be.instanceOf(http.Server); | ||
it('uses the provided event listeners and callbacks', function (next) { | ||
var complete = false; | ||
server = create({ port: ++port }, { | ||
close: next, | ||
http: function () { | ||
complete = true; | ||
}, | ||
listening: function (err) { | ||
if (err) return next(err); | ||
assume(complete).to.equal(true); | ||
assume(server.address().port).to.equal(port); | ||
request('http://localhost:'+ port, function (err, res, body) { | ||
if (err) return next(err); | ||
assume(res.headers['content-type']).to.equal('text/plain'); | ||
assume(body).to.equal(''); | ||
server.close(); | ||
}); | ||
}, | ||
request: function (req, res) { | ||
res.writeHead(200, { | ||
'Content-Length': 0, | ||
'Content-Type': 'text/plain' | ||
}); | ||
res.end(''); | ||
} | ||
}); | ||
}); | ||
@@ -66,8 +108,38 @@ | ||
it('creates a HTTPS server when given certs', function (next) { | ||
it('allows to disable the automatic invocation of the listen method', function (next) { | ||
server = create({ | ||
listen: false, | ||
port: ++port | ||
}, { listening: function () { | ||
assume(server.address().port).to.equal(port); | ||
server.close(function () { | ||
server = create({ listen: false, port: ++port }); | ||
server.once('listening', function () { | ||
assume(server.address().port).to.equal(port); | ||
next(); | ||
}); | ||
server.listen(port); | ||
}); | ||
}}); | ||
server.listen(port); | ||
}); | ||
it('throws an error if the required options for HTTPS are not provided', function (next) { | ||
try { | ||
server = create(); | ||
} catch (err) { | ||
assume(err.message).to.contain('SSL key or certificate'); | ||
next(); | ||
} | ||
}); | ||
it('creates a HTTPS server if the key and cert options are provided', function (next) { | ||
server = create({ | ||
port: ++port, | ||
root: __dirname, | ||
cert: './ssl/server.crt', | ||
key: './ssl/server.key', | ||
cert: 'ssl/server.crt', | ||
key: 'ssl/server.key', | ||
listening: next | ||
@@ -79,8 +151,8 @@ }); | ||
it('creates a SPDY server when given the SPDY boolean', function (next) { | ||
it('creates a SPDY server if the spdy boolean option is set', function (next) { | ||
server = create({ | ||
port: ++port, | ||
root: __dirname, | ||
cert: './ssl/server.crt', | ||
key: './ssl/server.key', | ||
cert: 'ssl/server.crt', | ||
key: 'ssl/server.key', | ||
spdy: true, | ||
@@ -94,2 +166,57 @@ listening: next | ||
}); | ||
it('optionally creates a redirect server (HTTP to HTTPS)', function (next) { | ||
var redirect = ++port + 1; | ||
server = create({ | ||
port: port, | ||
redirect: redirect, | ||
root: __dirname, | ||
ca: ['ssl/ca.crt'], | ||
cert: 'ssl/server.crt', | ||
key: 'ssl/server.key' | ||
}, function (err) { | ||
if (err) return next(err); | ||
assume(server.address().port).to.equal(port); | ||
request({ | ||
uri: 'http://localhost:'+ redirect, | ||
followRedirect: false | ||
}, function (err, res, body) { | ||
if (err) return next(err); | ||
assume(res.headers.location).to.equal('https://localhost:'+ port +'/'); | ||
assume(res.headers['strict-transport-security']).to.contain('max-age'); | ||
assume(body).to.equal(''); | ||
next(); | ||
}); | ||
}); | ||
assume(server).to.be.instanceOf(https.Server); | ||
}); | ||
it('optionally creates a redirect server (HTTP to HTTP)', function (next) { | ||
var redirect = ++port + 1; | ||
server = create({ | ||
port: port, | ||
redirect: redirect | ||
}, function (err) { | ||
if (err) return next(err); | ||
assume(server.address().port).to.equal(port); | ||
request({ | ||
uri: 'http://localhost:'+ redirect, | ||
followRedirect: false | ||
}, function (err, res, body) { | ||
if (err) return next(err); | ||
assume(res.headers.location).to.equal('http://localhost:'+ port +'/'); | ||
assume(body).to.equal(''); | ||
next(); | ||
}); | ||
}); | ||
assume(server).to.be.instanceOf(http.Server); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24116
13
569
65
6