All files / server/test normalize.js

100% Statements 13/13
100% Branches 16/16
100% Functions 1/1
100% Lines 13/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    30x     151x 1x 1x 1x       151x   151x 4x       151x 150x       151x 7x       151x    
 
// 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
  if (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
  if (!/^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;
};