Permalink
Browse files

Reworked the middleware to use a parser, add HTTP parser, and test.

  • Loading branch information...
1 parent 58c14b0 commit 6c2b526f34ced2d959dfe984acb831521eff76ba @mattrobenolt mattrobenolt committed Mar 18, 2012
Showing with 46 additions and 20 deletions.
  1. +5 −19 lib/middleware/connect.js
  2. +20 −1 lib/parsers.js
  3. +21 −0 test/raven.parsers.js
View
@@ -1,24 +1,10 @@
-var Client = require('../client').Client
- , url = require('url');
+var raven = require('../client'),
+ parsers = require('../parsers');
-module.exports = function raven(client) {
- client = (client instanceof Client) ? client : new Client(client);
+module.exports = function connectMiddleware(client) {
+ client = (client instanceof raven.Client) ? client : new raven.Client(client);
return function(err, req, res, next) {
- var kwargs = {
- 'sentry.interfaces.Http': {
- method: req.method,
- query_string: url.parse(req.url).query,
- headers: req.headers,
- cookies: req.cookies || '<unavailable: use cookieParser middleware>',
- data: req.body || '<unavailable: use bodyParser middleware>',
- url: (function build_absolute_url() {
- var protocol = req.socket.encrypted ? 'https' : 'http',
- host = req.headers.host || '<no host>';
- return protocol+'://'+host+req.url;
- }()),
- env: process.env
- }
- };
+ var kwargs = parsers.parseRequest(req);
client.createFromError(err, kwargs, function(result) {
res.sentry = client.getIdent(result);
next(err, req, res);
View
@@ -1,4 +1,5 @@
-var utils = require('./utils');
+var utils = require('./utils')
+ url = require('url');
module.exports.parseText = function parseText(message, kwargs) {
kwargs = kwargs || {};
@@ -29,3 +30,21 @@ module.exports.parseQuery = function parseQuery(query, engine, kwargs) {
};
return kwargs;
};
+
+module.exports.parseRequest = function parseRequest(req, kwargs) {
+ kwargs = kwargs || {};
+ kwargs['sentry.interfaces.Http'] = {
+ method: req.method,
+ query_string: url.parse(req.url).query,
+ headers: req.headers,
+ cookies: req.cookies || '<unavailable: use cookieParser middleware>',
+ data: req.body || '<unavailable: use bodyParser middleware>',
+ url: (function build_absolute_url() {
+ var protocol = req.socket.encrypted ? 'https' : 'http',
+ host = req.headers.host || '<no host>';
+ return protocol+'://'+host+req.url;
+ }()),
+ env: process.env
+ };
+ return kwargs;
+};
View
@@ -31,6 +31,27 @@ describe('raven.parsers', function(){
});
});
+ describe('#parseRequest()', function(){
+ it('should parse a request object', function(){
+ var mockReq = {
+ method: 'GET',
+ url: '/some/path?key=value',
+ headers: {
+ host: 'mattrobenolt.com'
+ },
+ body: '',
+ cookies: {},
+ socket: {
+ encrypted: true
+ }
+ };
+ var parsed = raven.parsers.parseRequest(mockReq);
+ parsed.should.have.property('sentry.interfaces.Http');
+ parsed['sentry.interfaces.Http'].url.should.equal('https://mattrobenolt.com/some/path?key=value');
+ parsed['sentry.interfaces.Http'].env.NODE_ENV.should.equal('test');
+ });
+ });
+
describe('#parseError()', function(){
it('should parse plain Error object', function(done){
raven.parsers.parseError(new Error(), {}, function(parsed){

0 comments on commit 6c2b526

Please sign in to comment.