'use strict';

var http = require("http");
var url = require("url");
var Promise = require("bluebird");

function HttpRequestService() {

    this.post = function (requestUrl, contents) {
        return executeRequest('POST', requestUrl, contents, 201);
    };

    this.get = function (requestUrl) {
        return executeRequest('GET', requestUrl, null, 200);
    };

    function executeRequest(method, requestUrl, contents, expectedStatus) {
        var parsedUrl = url.parse(requestUrl);
        if (contents) {
            var contentString = JSON.stringify(contents);
        }
        var req = null;

        return new Promise(function (resolve, reject) {
            var options = {
                withCredentials: false,
                hostname: parsedUrl.hostname,
                port: parsedUrl.port,
                path: parsedUrl.path,
                method: method
            };

            if (contents) {
                options.headers = {
                    "Content-Type": "application/json",
                    "Content-Length": contentString.length
                }
            }

            req = http.request(options, function (res) {
                if (res.statusCode === expectedStatus) {
                    var responseContent = "";
                    res.on("data", function (chunk) {
                        responseContent += chunk;
                    });
                    res.once("end", function () {
                        res.removeAllListeners("data");
                        if (res.headers["content-type"].indexOf("application/json") === 0) {
                            resolve(JSON.parse(responseContent));
                        }
                        else {
                            resolve(responseContent);
                        }
                    });
                }
                else {
                    reject(new Error(res.statusCode));
                }
            });

            if (contents) {
                req.write(contentString);
            }
            req.end();
        }).cancellable().catch(Promise.CancellationError, function (e) {
            req.abort();
            throw e;
        });
    }
}

module.exports = HttpRequestService;

@@ -1537,66 +1537,31 @@
 (res
-.headers%5B%22content-type%22%5D.indexOf(%22application/json%22) === 0
+ponseContentIsJson(res)
 ) %7B%0A
@@ -2106,24 +2106,28 @@
             
+    
 req.abort();
@@ -2139,16 +2139,20 @@
         
+    
 throw e;
@@ -2152,32 +2152,36 @@
 hrow e;%0A        
+ 
+   
 %7D);%0A    %7D%0A%7D%0A%0Amod
@@ -2177,16 +2177,273 @@
    %7D%0A%7D%0A%0A
+/**%0A * Does this response contain JSON?%0A *%0A * @param response%0A * @returns %7Bboolean%7D%0A */%0Afunction responseContentIsJson(response) %7B%0A    return response.headers%5B%22content-type%22%5D &&%0A        response.headers%5B%22content-type%22%5D.indexOf(%22application/json%22) === 0;%0A%7D%0A%0A
 module.e

Make HttpRequestService handle no conten-type header