github.com/cloudcredo/cloudrocker@v0.0.0-20160108110610-1320f8cc2dfd/sample-apps/node/node_modules/logfmt/test/request_logger_test.js (about)

     1  var logfmt = require('../logfmt'),
     2      assert = require('assert');
     3  
     4  //avoid test bleeding
     5  var logfmt = new logfmt;
     6  
     7  var OutStream = require('./outstream');
     8  
     9  suite('logfmt.requestLogger', function(){
    10    setup(function(){
    11      logfmt.stream = new OutStream;
    12    })
    13  
    14    test("timing logs on res.end()", function(done){
    15      var mockReq = {method: 'GET'}
    16      var mockRes = {statusCode: 200}
    17      mockRes.end = function(data, encoding){}
    18      var next = function(){
    19        assert.equal('', logfmt.stream.logline);
    20        done();
    21      };
    22  
    23      var logger = logfmt.requestLogger(function(req,res){
    24        return {
    25          method: req.method,
    26          "status": res.statusCode
    27        }
    28      });
    29      logger(mockReq, mockRes, next)
    30      mockRes.end()
    31      var expectation = /method=GET status=200 elapsed=\dms\n/
    32      var actual = logfmt.stream.logline;
    33      assert(expectation.test(actual), actual);
    34    })
    35  
    36    test("immediate option logs before next()", function(done){
    37      var mockReq = {method: 'GET'}
    38      var mockRes = {statusCode: 200}
    39      var next = function(){
    40        assert.equal('method=GET status=200\n', logfmt.stream.logline);
    41        done()
    42      };
    43  
    44      var logger = logfmt.requestLogger({immediate: true}, function(req,res){
    45        return {
    46          method: req.method,
    47          "status": res.statusCode
    48        }
    49      });
    50      logger(mockReq, mockRes, next)
    51    })
    52  
    53    test("can just send options", function(done){
    54      var mockReq = {method: 'GET'}
    55      mockReq.header = function(){
    56        return 'foo';
    57      }
    58      var mockRes = {statusCode: 200}
    59      mockRes.get = function(){
    60        return 'foo';
    61      }
    62      var next = function(){
    63        var actual = logfmt.parse(logfmt.stream.logline);
    64        assert.equal('foo', actual.ip);
    65        assert.equal('foo', actual.content_type);
    66        assert.equal('foo', actual.content_length);
    67        done()
    68      };
    69  
    70      var logger = logfmt.requestLogger({immediate: true})
    71      logger(mockReq, mockRes, next)
    72    })
    73  
    74    test("elapsed option renames elapsed key", function(done){
    75      var mockReq = {method: 'GET'}
    76      var mockRes = {statusCode: 200}
    77      mockRes.end = function(data, encoding){}
    78      var next = function(){
    79        assert.equal('', logfmt.stream.logline);
    80        done()
    81      };
    82  
    83      var logger = logfmt.requestLogger({elapsed: 'time'}, function(req,res){
    84        return {
    85          method: req.method,
    86          "status": res.statusCode
    87        }
    88      });
    89      logger(mockReq, mockRes, next)
    90      mockRes.end()
    91      var expectation = /method=GET status=200 time=\dms\n/
    92      var actual = logfmt.stream.logline;
    93      assert(expectation.test(actual), actual);
    94    })
    95  
    96    test("empty defaults to commonLogger", function(done){
    97      var mockReq = {method: 'GET'}
    98      mockReq.path = '/bar'
    99      mockReq.ip = '1.0.0.1'
   100      mockReq.header = function(h){
   101        return 'foo'
   102      }
   103      var mockRes = {statusCode: 200}
   104      var headers = {
   105        "content-type": 'foo/foo',
   106        "content-length": 123
   107      }
   108      mockRes.get = function(h){
   109        return headers[h];
   110      }
   111      mockRes.end = function(data, encoding){}
   112      var next = function(){
   113        assert.equal('', logfmt.stream.logline);
   114      };
   115      var logger = logfmt.requestLogger();
   116      logger(mockReq, mockRes, next)
   117      mockRes.end()
   118      var actual = logfmt.parse(logfmt.stream.logline);
   119      assert.equal(actual.path, '/bar');
   120      assert.equal(actual.ip, '1.0.0.1');
   121      assert.equal(actual.content_type, 'foo/foo');
   122      assert.equal(actual.content_length, '123');
   123      assert(/\dms/.test(actual.elapsed), 'includes elapsed')
   124      assert(actual.time)
   125      done();
   126    })
   127  
   128  
   129    test("emits x-request-id header as request_id if present", function(done){
   130      var mockReq = {method: 'GET'}
   131      mockReq.path = '/bar'
   132      mockReq.ip = '1.0.0.1'
   133      var mockRes = {statusCode: 200}
   134      var headers = {
   135        "x-request-id": '56e29d80-fb82-454c-b538-7af3e9d0b18c'
   136      }
   137      mockReq.header = function(h){
   138        return headers[h];
   139      }
   140      mockRes.get = function(h){
   141        return headers[h];
   142      }
   143      mockRes.end = function(data, encoding){}
   144      var next = function(){
   145        assert.equal('', logfmt.stream.logline);
   146      };
   147      var logger = logfmt.requestLogger();
   148      logger(mockReq, mockRes, next)
   149      mockRes.end()
   150      var actual = logfmt.parse(logfmt.stream.logline);
   151      assert.equal(actual.request_id, '56e29d80-fb82-454c-b538-7af3e9d0b18c');
   152      done();
   153    })
   154  
   155    test("commonFormatter uses correct path with express", function(){
   156      var mockReq = {method: 'GET'}
   157      mockReq.originalUrl = '/bar'
   158      mockReq.ip = '1.0.0.1'
   159      mockReq.header = function(h){ return 'foo' }
   160      var mockRes = {statusCode: 200}
   161      mockRes.get = function(){ return 'foo' }
   162      var actual = logfmt.requestLogger.commonFormatter(mockReq, mockRes);
   163      assert.equal('/bar', actual.path);
   164    })
   165  
   166    test("commonFormatter uses correct path", function(){
   167      var mockReq = {method: 'GET'}
   168      mockReq.path = function(){ return '/bar' }
   169      mockReq.ip = '1.0.0.1'
   170      mockReq.header = function(h){ return 'foo' }
   171      var mockRes = {statusCode: 200}
   172      mockRes.get = function(){ return 'foo' }
   173      var actual = logfmt.requestLogger.commonFormatter(mockReq, mockRes);
   174      assert.equal('/bar', actual.path);
   175    })
   176  
   177    test("commonFormatter uses correct path w. vanilla http", function(){
   178      var mockReq = {method: 'GET'}
   179      mockReq.url = '/bar'
   180      mockReq.ip = '1.0.0.1'
   181      var mockRes = {statusCode: 200}
   182      var actual = logfmt.requestLogger.commonFormatter(mockReq, mockRes);
   183      assert.equal('/bar', actual.path);
   184    })
   185  
   186    test("commonFormatter uses correct ip", function(){
   187      var mockReq = {method: 'GET'}
   188      mockReq.path = '/bar'
   189      var headers = {
   190        "x-forwarded-for": '10.0.1.1'
   191      }
   192      mockReq.header = function(h){
   193        return headers[h] || 'foo';
   194      }
   195      var mockRes = {statusCode: 200}
   196      mockRes.get = function(){
   197        return 'foo';
   198      }
   199      var actual = logfmt.requestLogger.commonFormatter(mockReq, mockRes);
   200      assert.equal('10.0.1.1', actual.ip);
   201    })
   202  
   203    test("requestLogger works with namespace", function(done){
   204      var mockReq    = {method: 'GET'}
   205      mockReq.header = function(){ return 'foo'; }
   206      var mockRes    = {statusCode: 200}
   207      mockRes.get    = function(){ return 'foo'; }
   208      var next = function(){
   209        var actual = logfmt.parse(logfmt.stream.logline);
   210        assert.equal('namespacetest', actual.ns);
   211        done()
   212      };
   213  
   214      var logger = logfmt.namespace({ns:'namespacetest'}).requestLogger({immediate: true})
   215      logger(mockReq, mockRes, next)
   216    })
   217  })