Code coverage report for nock/lib/match_body.js

Statements: 96.15% (25 / 26)      Branches: 96.15% (25 / 26)      Functions: 100% (1 / 1)      Lines: 96% (24 / 25)      Ignored: none     

All files » nock/lib/ » match_body.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52    1 1   1   214 175   39   39         39   39 1     38 31       38 38 7 7 4     3 2     2 1         38 2     36    
'use strict';
 
var deepEqual = require('deep-equal');
var qs = require('querystring');
 
module.exports =
function matchBody(spec, body) {
  if (typeof spec === 'undefined') {
    return true;
  }
  var options = this || {};
 
  Iif (Buffer.isBuffer(body)) {
    body = body.toString();
  }
 
  //strip line endings from both so that we get a match no matter what OS we are running on
  body = body.replace(/\r?\n|\r/g, '');
 
  if (spec instanceof RegExp) {
    return body.match(spec);
  }
 
  if (typeof spec === "string") {
    spec = spec.replace(/\r?\n|\r/g, '');
  }
 
  // try to transform body to json
  var json;
  if (typeof spec === 'object' || typeof spec === 'function') {
    try { json = JSON.parse(body);} catch(err) {}
    if (json !== undefined) {
      body = json;
    }
    else
      if (options.headers) {
        var contentType = options.headers['Content-Type'] ||
                          options.headers['content-type'];
 
        if (contentType && contentType.match(/application\/x-www-form-urlencoded/)) {
          body = qs.parse(body);
        }
      }
  }
 
  if (typeof spec === "function") {
    return spec.call(this, body);
  }
 
  return deepEqual(spec, body, { strict: true });
};