Code coverage report for nock/lib/match_body.js

Statements: 96.55% (28 / 29)      Branches: 91.67% (22 / 24)      Functions: 100% (1 / 1)      Lines: 96.43% (27 / 28)      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 53 54 55 56 57 58    1 1   1   180 147   33   33         33   33 1     32 27       32 32 5 5 3     2 1     1 1         32 1     31 31 30   1        
'use strict';
 
var deepEqual = require('assert').deepEqual;
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'];
 
        Eif (contentType.match(/application\/x-www-form-urlencoded/)) {
          body = qs.parse(body);
        }
      }
  }
 
  if (typeof spec === "function") {
    return spec.call(this, body);
  }
 
  try {
    deepEqual(spec, body);
    return true;
  } catch(err) {
    return false;
  }
 
};