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

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