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

     1  var logfmt = require('../logfmt'),
     2      through = require('through'),
     3      stream = require('stream'),
     4      assert = require('assert');
     5  
     6  //avoid test bleeding
     7  var logfmt = new logfmt;
     8  
     9  suite('logfmt.bodyParserStream', function() {
    10  
    11    test("skips parsing when req._body is true", function(){
    12  
    13      var mockReq = new stream.Readable;
    14      mockReq.header = function(){
    15        return 'application/logplex-1';
    16      }
    17      mockReq._read = function(){};
    18      mockReq.push('hello=kitty');
    19      mockReq.push(null);
    20  
    21      mockReq._body = true;
    22  
    23      var next = function(err){
    24        assert.equal(mockReq.body, undefined)
    25      };
    26  
    27      var parser = logfmt.bodyParserStream();
    28      parser(mockReq, null, next)
    29    })
    30  
    31    test("skips parsing when contentType does not match", function(){
    32      var mockReq = new stream.Readable;
    33      mockReq.header = function(){
    34        return 'application/foo';
    35      }
    36      mockReq._read = function(){};
    37      mockReq.push('hello=kitty');
    38      mockReq.push(null);
    39  
    40      var next = function(err){
    41        assert.equal(mockReq.body, undefined)
    42      };
    43  
    44      var parser = logfmt.bodyParserStream();
    45      parser(mockReq, null, next)
    46  
    47    })
    48  
    49    test("converts body lines to object read stream", function(done){
    50      var mockReq = new stream.Readable;
    51      var data;
    52  
    53      mockReq.header = function(){
    54        return 'application/logplex-1';
    55      }
    56      mockReq._read = function(){};
    57      mockReq.push('hello=kitty');
    58      mockReq.push(null);
    59      var next = function(){};
    60  
    61      var parser = logfmt.bodyParserStream();
    62      parser(mockReq, null, next)
    63  
    64      mockReq.body.on('readable', function(){
    65        var chunk = mockReq.body.read();
    66        if (chunk !== null) {
    67          data = chunk;
    68        }
    69      })
    70  
    71      mockReq.body.on('end', function() {
    72        assert.deepEqual(data, { hello: 'kitty' });
    73        done();
    74      });
    75    })
    76  
    77  
    78    test("accepts contentType option", function(done){
    79      var mockReq = new stream.Readable;
    80      var data;
    81      mockReq.header = function(){
    82        return 'foo';
    83      }
    84      mockReq._read = function(){};
    85      mockReq.push('hello=kitty');
    86      mockReq.push(null);
    87      var next = function(){};
    88  
    89      var parser = logfmt.bodyParserStream({contentType: 'foo'});
    90      parser(mockReq, null, next)
    91  
    92      mockReq.body.on('readable', function(){
    93        var chunk = mockReq.body.read();
    94        if (chunk !== null) {
    95          data = chunk;
    96        }
    97      })
    98  
    99      mockReq.body.on('end', function() {
   100        assert.deepEqual(data, { hello: 'kitty' })
   101        done();
   102      });
   103    })
   104  
   105    test("parses all the lines", function(done){
   106      var mockReq = new stream.Readable;
   107      mockReq.header = function(){
   108        return 'application/logplex-1';
   109      }
   110      mockReq._read = function(){};
   111      mockReq.push('hello=kitty\n');
   112      mockReq.push('foo=bar\n');
   113      mockReq.push('path=/\n');
   114      mockReq.push(null);
   115      var next = function(){};
   116  
   117      var parser = logfmt.bodyParserStream();
   118      parser(mockReq, null, next)
   119  
   120      var matches = [{path: '/'}, {foo: 'bar'}, {hello: 'kitty'}];
   121  
   122      mockReq.body.pipe(through(function(data){
   123        assert.deepEqual(data, matches.pop())
   124      },function(){
   125        done()
   126      }))
   127    })
   128  
   129  })