'use strict';

var parseUrl = require('url').parse;
var Promise = require('promise');
var concat = require('concat-stream');
var Response = require('http-response-object');
var handleQs = require('./lib/handle-qs.js');

module.exports = doRequest;
module.exports._request = require('http-basic');
function doRequest(method, url, options, callback) {
  var result = new Promise(function (resolve, reject) {
    // check types of arguments

    if (typeof method !== 'string') {
      throw new TypeError('The method must be a string.');
    }
    if (typeof url !== 'string') {
      throw new TypeError('The URL/path must be a string.');
    }
    if (typeof options === 'function') {
      callback = options;
      options = {};
    }
    if (options === null || options === undefined) {
      options = {};
    }
    if (typeof options !== 'object') {
      throw new TypeError('Options must be an object (or null).');
    }
    if (typeof callback !== 'function') {
      callback = undefined;
    }

    method = method.toUpperCase();
    options.headers = options.headers || {};

    // handle query string
    if (options.qs) {
      url = handleQs(url, options.qs);
    }

    // handle json body
    if (options.json) {
      options.body = JSON.stringify(options.json);
      options.headers['content-type'] = 'application/json';
    }

    var req = module.exports._request(method, url, {
      headers: options.headers,
      followRedirects: true,
      gzip: true,
      cache: options.cache
    }, function (err, res) {
      if (err) return reject(err);
      res.body.on('error', reject);
      res.body.pipe(concat(function (body) {
        resolve(new Response(res.statusCode, res.headers, Array.isArray(body) ? new Buffer(0) : body));
      }));
    });

    if (req) {
      req.end(options.body ? options.body : new Buffer(0));
    }
  });
  result.getBody = function (encoding) {
    return result.then(function (res) { return res.getBody(encoding); });
  };
  return result.nodeify(callback);
}

@@ -8,16 +8,48 @@
 rict';%0A%0A
+var assert = require('assert');%0A
 var pars
@@ -1368,24 +1368,394 @@
 on';%0A    %7D%0A%0A
+    var body = options.body ? options.body : new Buffer(0);%0A    if (typeof body === 'string') body = new Buffer(body);%0A    assert(Buffer.isBuffer(body), 'body should be a Buffer or a String');%0A    if (!Object.keys(options.headers).some(function (name) %7B return name.toLowerCase() === 'content-length'; %7D)) %7B%0A      options.headers%5B'content-length'%5D = body.length;%0A    %7D%0A%0A
     var req 
@@ -2203,51 +2203,12 @@
 end(
-options.body ? options.body : new Buffer(0)
+body
 );%0A 

Add content-length header