-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename to `requestid`; add test, add examples
- Loading branch information
Vladimir Varankin
committed
Dec 15, 2014
1 parent
716b6fc
commit 8778aad
Showing
6 changed files
with
82 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
/node_modules | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"config" : { "port": "3010" }, | ||
"devDependencies": { | ||
"connect": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var http = require('http'), | ||
connect = require('connect'), | ||
requestId = require('../'), | ||
app = connect(); | ||
|
||
app | ||
.use(requestId.createMiddleware()) | ||
.use(function(req, res, next) { | ||
var reqId = requestId.get(); | ||
res.end('Request ID: ' + reqId); | ||
next(); | ||
}); | ||
|
||
http.createServer(app).listen(process.env.npm_package_config_port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var assert = require('assert'), | ||
http = require('http'), | ||
request = require('supertest'), | ||
requestId = require('./'); | ||
|
||
describe('request-id', function() { | ||
describe('createMiddleware()', function() { | ||
it('should return middleware function', function() { | ||
var middleware = requestId.createMiddleware(); | ||
assert.equal(typeof middleware, 'function'); | ||
}); | ||
}); | ||
|
||
describe('get()', function() { | ||
var server; | ||
|
||
before(function() { | ||
var requestIdMiddleware = requestId.createMiddleware(), | ||
asyncMiddleware = function(req, res, next) { | ||
setTimeout(function() { // NOTE: emulate some asynchronous middleware chain | ||
process.nextTick(next) | ||
}, 10); | ||
}; | ||
|
||
server = http.createServer(function(req, res) { | ||
requestIdMiddleware(req, res, function() { | ||
asyncMiddleware(req, res, function() { | ||
res.end(requestId.get()); | ||
}) | ||
}); | ||
}); | ||
}); | ||
|
||
var reqIdPrefix = '123x123-', | ||
requestsMax = 3; | ||
|
||
it('should return nothing if no request-id set', function(done) { | ||
request(server) | ||
.get('/') | ||
.expect('', done); | ||
}); | ||
|
||
Array.apply(null, { length : requestsMax }).forEach(function(_, i) { | ||
it('should return request-id for current request (' + i + ')', function(done) { | ||
var newId = reqIdPrefix + i; | ||
request(server) | ||
.get('/') | ||
.set('X-Request-Id', newId) | ||
.expect(newId, done); | ||
}); | ||
}); | ||
}); | ||
}); |