All files / server/test normalize.js

76.92% Statements 10/13
68.75% Branches 11/16
100% Functions 1/1
76.92% Lines 10/13
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    11x     97x             97x   97x 2x       97x 97x       97x 5x       97x    
 
// Make an object with the options as expected by `request()` library
module.exports = (method = 'GET', url = '/', port = 3000, options) => {
 
  // Make sure it's a simple object
  Iif (typeof options === 'string') {
    options = options.replace(/\/$/, '');
    url = url.replace(/^\//, '');
    options = { url: options + '/' + url };
  }
 
  // Assign independent parts
  options = Object.assign({}, { headers: {} }, { url, method }, options);
 
  if (options && options.body && typeof options.body === 'string') {
    options.headers['Content-Type'] = 'text/plain';
  }
 
  // Make sure it has a right URL or localhost otherwise
  Eif (!/^https?:\/\//.test(options.url)) {
    options.url = `http://localhost:${port}${options.url}`;
  }
 
  // Set it to send a JSON when appropriate
  if (options.body && typeof options.body === 'object') {
    options.json = true;
  }
 
  // Finally return the fully formed object
  return options;
};